var whitespace = " \t\n\r";
var emptyAlertMessage = "cannot be empty, please supply data for this field."; 
var iF = "cannot be empty, please supply data for this field.";
var iE = "must be a valid email address (like foo@bar.com). Please reenter it.";
var iL = "must be no longer than LEN characters. Please reenter it.";
var gmsg = '';

function trim(inputString) {
  /* replace spacing with empty string */
   var r = inputString.replace(/^\s*/g,"");
  /* clean up string */
   var rr = r;
   for(i = r.length-1;i>=0;i--) {
      var c=r.charAt(i);
      if(whitespace.indexOf(c) != -1)
         rr=r.slice(0,i-r.length);
      else
         break;
   }
   return rr; /* return the trimmed string*/
}

function isEmpty(v, fieldName) {
   if ((v == null) || (v.length == 0)) {
      gmsg += fieldName + " cannot be empty.\n";
      return true;
   }
   return false;
}

function tooLong(s, l) {
	if (s.length > l)
		return true;
	else
		return false;
}

function isbadLength(e) {
	var s = trim(e.value);
	if ((s == null) || (s.length == 0))
		return false;
	else
		return false;
}

function isEmail(v, fieldName) {
	v = trim(v);
	if ((v == null) || (v.length == 0))
		return true;
	return validateEmail(v);
}

function validateEmail(email)
{
   if (! isValidEmailAddress(email) )
   {
      return false;
   }
   return true;
}

function hasInvalidChar(emailAddress)
{
   var invalidChars = "/;:,~!#$%^&*()?"; // this list is not complete
   for (var k = 0; k < invalidChars.length; k++)
   {
      var ch = invalidChars.charAt(k);
      if (emailAddress.indexOf(ch) > -1)
      {
         return true;
      }
   }
   return false;
}

function isValidEmailAddress(emailAddress)
{
   /* Check for empty address or invalid characters */
   if (emailAddress == "" || hasInvalidChar(emailAddress))
   {
      gmsg += "Invalid E-mail Address";
      return false;
   }
   /* check for presence of the @ character */
   var atPos = emailAddress.indexOf("@", 1)
   if (atPos == -1)
   {
   	  gmsg += "Invalid E-mail Address - No @ character";
      return false;
   }
   /* Check that there are no more @ characters */
   if (emailAddress.indexOf("@", atPos + 1) > -1)
   {
   	 gmsg += "Invalid E-mail Address - Multiple @ characters";
     return false;
   }
   /* Check for the presence of a dot somewhere after @ */
   var dotPos = emailAddress.indexOf(".", atPos + 1);
   if (dotPos == -1)
   {
   	  gmsg += "Invalid E-mail Address - No dot (.)";
      return false;
   }
   /* Check for presence of two or more characters after last dot */
   var lastDotPos = emailAddress.lastIndexOf(".");
   if (lastDotPos + 3 >  emailAddress.length)
   {
   	  gmsg += "Invalid E-mail Address - No characters after dot (.)";
      return false;
   }
   return true;
}

function validateApplication() {

  var frm = document.forms['PgPasteResume'];
  
  if (isEmpty(frm.tbFirstName.value, "First Name")) {

  }
  if (isEmpty(frm.tbLastName.value, "Last Name")) {

  }
  if (isEmpty(frm.tbAddress.value, "Address")) {

  }
  if (isEmpty(frm.tbState.value, "State")) {

  }
  if (isEmpty(frm.tbCity.value, "City")) {

  }
  if (isEmpty(frm.tbZipCode.value, "Zip Code")) {

  }
  if (isEmpty(trim(frm.tbResume.value), "Resume")) {

  }
  if (isEmail(frm.tbEmailAddress.value, "Email Address")) {
  
  }
}

function doSubmitApplication()
{
	gmsg = '';
	validateApplication();
	if (gmsg != '') {
	  alert(gmsg);
	  return false;
	} else {
	  document.forms['PgPasteResume'].submit();
	  return true;
	}
}

function validateMailJobPosting() {

  var frm = document.forms['PgPasteResume'];
  
  if (isEmpty(frm.Subject.value, "Subject")) {

  }
  
  if (isEmpty(frm.addressTo.value, "Send To")) {

  }
  
  if (isEmail(frm.addressTo.value, "Send To")) {
  
  }
  
  if (isEmpty(frm.addressFrom.value, "Send From")) {

  }
  
  if (isEmail(frm.addressFrom.value, "Send From")) {
  
  }
  
  if (isEmpty(trim(frm.Comments.value), "Comments")) {

  }
  
}

function doSubmitMailJobPosting()
{
	gmsg = '';
	validateMailJobPosting();
	if (gmsg != '') {
	  alert(gmsg);
	  return false;
	} else {
	  // document.forms['PgPasteResume'].submit();											
	  return true;
	}
}

function displayBatch() {
	var loc = location.search;
	var idx = loc.indexOf('curr_id=');
	bt = 1;
	if (idx > 0) {
	  bt = location.substring(idx + 8);
	}
	
	if (document.all) {
		document.all['batch' + bt].style.display = 'block';
		document.all['batch' + bt].style.visibility = "";
	  } else if (document.getElementById) {
	  	document.getElementById('batch' + bt).style.display = "block";
		document.getElementById('batch' + bt).style.visibility = "";
	  } else if (document.layers) {
		document.ids['batch' + bt].display = "block";
		document.ids['batch' + bt].style.visibility = "";
	 }
}

function openMap(id)
{
	var link;
	if ( id == 'home' ) {
		link = 'http://map.glic.com/scripts/mqinterconnect.exe?link=map&streetaddress=7 Hanover Square&city=New York&state=NY&zip=10004';
	}	
	if ( id == 'midwestern' ) {
		link = 'http://map.glic.com/scripts/mqinterconnect.exe?link=map&streetaddress=2300 East Capital Drive&city=Appleton&state=WI&zip=54911';
	}	
	if ( id == 'northeastern' ) {
	 	link = 'http://map.glic.com/scripts/mqinterconnect.exe?link=map&streetaddress=3900 Burgess Place&city=Bethlehem&state=PA&zip=18017';
	}	
	if ( id == 'western' ) {
		link = 'http://map.glic.com/scripts/mqinterconnect.exe?link=map&streetaddress=777 East Magnesium Road&city=Spokane&state=WA&zip=99208';
	}	
	if ( id == 'berkshire' ) {
		link = 'http://map.glic.com/scripts/mqinterconnect.exe?link=map&streetaddress=700 South Street&city=Pittsfield&state=MA&zip=01201';
	}	
	if ( id == 'boston' ) {
		link =	'http://map.glic.com/scripts/mqinterconnect.exe?link=map&streetaddress=700 Longwater Drive&city=Norwell&state=MA&zip=02061';
	}
	
	window.open(link,'','width=620,height=455,resizable=yes');
}

function showWhatItsUsedFor(userName)
{
	var f = document.forms['PgPasteResume'];
	var fName = f.tbFirstName.value;
	var lName = f.tbLastName.value;
	
	fName = (fName == null) ? "" : fName;
	lName = (lName == null) ? "" : lName;
	
	var first;
	if (fName == "" && lName == "") {
		first = "Please";
	} else {
		first = fName + " " + lName + ", please";
	}
	
	var myWindow = window.open("", "tinyWindow", 'toolbar,width=350,height=300') 
	var line1 = "Guardian has a long-standing commitment to equal opportunity employment and to the individual rights of employees. Providing a non-discriminatory and productive work environment is an integral part of this commitment.<br/><br/>";
	var line2 = first + " take a moment to assist us in information gathering. Federal laws require employers to identify the race/gender of applicants and to invite veterans to <b>voluntarily</b> identify themselves.<br/><br/>";
	var line3 = "If you do not wish to complete this section, it will not adversely affect your application. <b>You can bypass this section by hitting Submit.</b>";
	myWindow.document.write(line1);
	myWindow.document.write(line2);
	myWindow.document.write(line3);
	myWindow.document.bgColor="lightblue"
	myWindow.document.close() 

}

