// Browser sniffing.
var mozilla = (document.getElementById && !document.all);
var nashville = (document.all && !document.getElementById);
var tasman = (document.all && document.getElementById);

//Help Functions (prototype.js required)
      var helpNavOpenItem = null;
      var requestForm = null;
      function help (helpAnchor) {
        hideHelp(helpNavOpenItem);
        if (helpAnchor) {
          var navItem = helpAnchor.parentNode;
          if (navItem) {
            var navID = navItem.id;
            if (navID) {
              var helpNavID = navID.concat("-help");
              if (helpNavID) {
                var helpNavItem
                showHelp($(helpNavID));
              }
            }
          }
        }
      }
      
      function showHelp (node) {
        if (node) {
          node.style.display = "block";
          helpNavOpenItem = node;
          if (requestForm) {
            requestForm.style.color = "#c0c0c0";
          }
        }
      }
      
      function hideHelp (node) {
        if (node) {
          node.style.display = "none";
          helpNavOpenItem = null;
          if (requestForm) {
            requestForm.style.color = "";
          }
        }
      }
      

// Functions.
function GetObject(u) {
  if(nashville) return eval('document.all["' + u+ '"]');
  else if(tasman || mozilla) return document.getElementById(u);
  else return null;
}

function getObjectById(id) {
  return GetObject(id);
}

    function autofill() {
      if (document.getElementById("contact-user-same").checked) {
		document.getElementById("contact-username").value = document.getElementById("contact-name").value;
		document.getElementById("contact-userphone").value = document.getElementById("contact-phone").value;
		document.getElementById("contact-userfax").value = document.getElementById("contact-fax").value;
		document.getElementById("contact-useremail").value = document.getElementById("contact-email").value;	
	  }
    }

function removeTableScrolling() {
  var tablescrollCSS = getObjectById("tablescrollCSS");
  if(tablescrollCSS) {
    tablescrollCSS.disabled = true;
  }
  
  var tableContainer = getObjectById("tableContainer");
  if (tableContainer) {
    tableContainer.style.height = "auto";
  }
}

function popUp(URL) {
  if(myWin) {
    myWin.location = URL;
  } else {
    var myWin = window.open(URL,myWin, "resizable=yes,height=660,width=937,scrollbars=yes,alwaysRaised=yes,toolbar=no,menubar=no,location=no,status=no,personalbar=no");
  }
}

function sendMail(name, domain, display) {
  var user = name;
  var site = domain;
  document.write('<a href=\"mailto:' + user + '@' + site + '\">');
  if (display == "") {
    document.write(user + '@' + site + '</a>');
  } else {
    document.write(display + '</a>');
  }
}

function pop_url(url) {
  window.open(url, "_blank");
}

function pop_url_anchor(anchor) {
  var url = anchor.href;
  pop_url(url);
}

function print_body() {
  var forms = document.getElementsByTagName("form");
  var form = forms[0];
  validate_account(form);
  print_element("bodyTableCell", form.id);
}

function print_element(element_id, form_id) {
  var element = document.getElementById(element_id);
  orig_form_element = document.getElementById(form_id);
  var formRegexp = /-form/;
  var body_id = form_id.replace(formRegexp,"-request");
  var cloned_element = element.cloneNode(true);
  var print_window = open("","print_form","width=600,height=800,menu=yes,scrollbars=yes,");
  var print_document = print_window.document;
  print_document.writeln("<html><head>");
  print_document.writeln("<title>print</title>");
  print_document.writeln("<link rel=\"stylesheet\" href=\"/campusmedia/css/forms.css\" type=\"text/css\" />");
  print_document.writeln("<link rel=\"stylesheet\" href=\"/campusmedia/css/print.css\" type=\"text/css\" />");
  print_document.writeln("</head><body id=\""+ body_id +"\" style=\"margin: 1em;\">");
  print_document.writeln("<strong>Fax Number: 212.995.4250</strong>");
  print_document.writeln(cloned_element.innerHTML);
  print_document.writeln("</body></html>");
  try {
    var html_element = print_document.childNodes[0];
    var body_elements = html_element.getElementsByTagName("body");
    var body_element = body_elements[0];
    var form_elements = body_element.getElementsByTagName("form");
    var form_element = form_elements[0];
    populate_form(orig_form_element, form_element);
  } catch (e) {
  }
  print_document.close();
  print_window.print();
  print_window.close();
}

function populate_form (from_form, to_form) { 
  from_elements = from_form.elements;
  to_elements = to_form.elements;
  for (element in from_elements) {
    if (!isNaN(element)) {
      from_element = from_elements[element];
      to_element = to_elements[element];
      if (from_element.type == "checkbox" || from_element.type == "radio") {
        to_element.checked = from_element.checked;
      } else {
        to_elements[element].value = from_elements[element].value;
      }
    }
  }
}

function validate_account(form) {
  var non_alpha_numeric = /[^a-zA-Z0-9]/;
  var non_alpha_numeric_global = /[^a-zA-Z0-9]/gi;
  //pattern 1 assumes the 
  var pattern1 = /(^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])/;
  try {
    var account_number = form.elements['account'].value;
    account_number = account_number.replace(non_alpha_numeric_global, "");
    var account, fund, organization, program, project; 
    var string_end = (account_number.length);
    if (pattern1.test(account_number)) { 
      account = account_number.slice(0,5);
      fund = account_number.slice(5,7);
      organization = account_number.slice(7,12);
      formatted_account_number = account + '-' + fund + '-' + organization;

      program = account_number.slice(12, 17);
      project = account_number.slice(17, string_end);
      if (program.length > 0) {
        formatted_account_number += '-' + program;
      }
      if (project.length > 0) {
        formatted_account_number += '-' + project;
      }
      form.elements['account'].value = formatted_account_number;
    }
  } catch (e) {
  }
}


