
function clearinput (sid, text) {
	sinput = document.getElementById(sid);
	sinput.value = (sinput.value == text) ? '' : sinput.value;
	return;
}


function fillinput (sid, text) {
	sinput = document.getElementById(sid);
	sinput.value = (empty(sinput.value)) ? text : sinput.value;
	return;
}

// ---- verifyContact() -------------------------------------------------------------------- //
function verifyContact() {
	var first_name = document.getElementById('first_name').value;
	var last_name = document.getElementById('last_name').value;
	var title = document.getElementById('title').value;
	var company = document.getElementById('company').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	var interest = document.getElementById('interest').value;

	if (!validLength(first_name, "first name")) {
		return false;
	} else if (!validLength(last_name, "last name")) {
		return false;
	} else if (!validLength(title, "title")) {
		return false;
	} else if (!validLength(company, "company")) {
		return false;
	} else if (!email) {
		window.alert("Please enter your email address");
		return false;
	} else if (!checkemail(email)) {
		window.alert("Please enter a valid email address");
		return false;
	} else if (!validLength(phone, "phone")) {
		return false;
	} else if (interest.length < 2) {
		alert("Please enter what you are interested in");
		return false;
	} else {
		return true;
	}
}


// ---- verifyPostresume() -------------------------------------------------------------------- //
function verifyPostresume() {
	var first_name = document.getElementById('first_name').value;
	var last_name = document.getElementById('last_name').value;
	var address = document.getElementById('address').value;
	var phone = document.getElementById('phone').value;
	var email = document.getElementById('email').value;
	var language = document.getElementById('language').value;
    var summary = document.getElementById('summary').value;
	var education = document.getElementById('education').value;
	var history = document.getElementById('history').value;
	var technical = document.getElementById('technical').value;
	var resume = document.getElementById('resume').value;

	if (!validLength(first_name, "first name")) {
		return false;
	} else if (!validLength(last_name, "last name")) {
		return false;
	} else if (!validLength(address, "address")) {
		return false;
	} else if (!validLength(phone, "phone")) {
		return false;
	} else if (!email) {
		alert("Please enter your email address");
		return false;
	} else if (!checkemail(email)) {
		alert("Please enter a valid email address");
		return false;
	} else if (language.length<2) {
		alert("Please enter your language skills");
		return false;
    } else if (summary.length<2) {
		alert("Please enter a summary of your skills");
		return false;
	} else if (education.length<2) {
		alert("Please enter your education information");
		return false;
	} else if (history.length<2) {
		alert("Please enter your work history");
		return false;
	} else if (technical.length<2) {
		alert("Please enter your technical experience");
		return false;
	} else if (empty(resume)) {
		alert("Please select a file to upload as your resume");
		return false;
	} else if (!checkResume(resume)) {
		return false;
	} else {
		return true;
	}
}


function checkResume(x) {
  var ext = x.split('.');
  var e = ext[ext.length-1];
  if (ext.length < 2) {
	  alert("Please select a valid file");
	  return false;
  } else if (e == 'pdf') {
	  return true;
  } else if (e == 'doc') {
	  return true;
  } else {
	  alert("Please select either a pdf or doc file for your resume");
	  return false;
  }
}

//etc.
function isString(x) { return typeof x == 'string'; }


function checkemail(str) {
	var testresults=true;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var emailPat=/^(.+)@(.+)$/; 
	var matchArray=str.match(emailPat); 
	if (matchArray==null) {	testresults=false; }
	// check length for user name and domain
	//else { 
	//	var user=matchArray[1];
	//	var domain=matchArray[2]; 
	//	if (user.length < 1 || doman.length < 1){ testresults=false; }		
	//}
	if (!filter.test(str)) { testresults=false; }

	return testresults;
}


function validLength(x, str) {
	if (x.length < 2) {
		alert("Please enter a valid "+str);
		return false;
	}
	else {
		return true;
	}
}


function validZip(x, t) {
  if ((x.length != 5) || !isInteger(x)) {
    alert("Please enter a valid "+t)
    return false;
  } else {
    return true;
  }
}

function empty(x){
  return (x == '');
}

function isInteger(s) {   
    for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


// ---- State Select ---------------------------------------------------------------------------- //
var postState = '';
var postCountry = 'US';

var state = '\
US:AK:Alaska|\
US:AL:Alabama|\
US:AR:Arkansas|\
US:AS:American Samoa|\
US:AZ:Arizona|\
US:CA:California|\
US:CO:Colorado|\
US:CT:Connecticut|\
US:DC:D.C.|\
US:DE:Delaware|\
US:FL:Florida|\
US:FM:Micronesia|\
US:GA:Georgia|\
US:GU:Guam|\
US:HI:Hawaii|\
US:IA:Iowa|\
US:ID:Idaho|\
US:IL:Illinois|\
US:IN:Indiana|\
US:KS:Kansas|\
US:KY:Kentucky|\
US:LA:Louisiana|\
US:MA:Massachusetts|\
US:MD:Maryland|\
US:ME:Maine|\
US:MH:Marshall Islands|\
US:MI:Michigan|\
US:MN:Minnesota|\
US:MO:Missouri|\
US:MP:Marianas|\
US:MS:Mississippi|\
US:MT:Montana|\
US:NC:North Carolina|\
US:ND:North Dakota|\
US:NE:Nebraska|\
US:NH:New Hampshire|\
US:NJ:New Jersey|\
US:NM:New Mexico|\
US:NV:Nevada|\
US:NY:New York|\
US:OH:Ohio|\
US:OK:Oklahoma|\
US:OR:Oregon|\
US:PA:Pennsylvania|\
US:PR:Puerto Rico|\
US:PW:Palau|\
US:RI:Rhode Island|\
US:SC:South Carolina|\
US:SD:South Dakota|\
US:TN:Tennessee|\
US:TX:Texas|\
US:UT:Utah|\
US:VA:Virginia|\
US:VI:Virgin Islands|\
US:VT:Vermont|\
US:WA:Washington|\
US:WI:Wisconsin|\
US:WV:West Virginia|\
US:WY:Wyoming|\
US:AA:Military Americas|\
US:AE:Military Europe/ME/Canada|\
US:AP:Military Pacific|\
';


function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

function populateState() {
  var selObj = document.getElementById('stateSelect');
  var foundState = false;

  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < stateLineArray.length; loop++) {
    lineArray = stateLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    stateCode    = TrimString(lineArray[1]);
    stateName    = TrimString(lineArray[2]);
  if ('US' == countryCode && countryCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('stateSelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","state");
        inputSel.setAttribute("id","stateSelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('stateSelect');
        selObj.options[0] = new Option('Select State','');
        selObj.selectedIndex = 0;
      }
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }
  // If the country has no states, change the select to a text box
  if ( ! foundState ) {
    parentObj = document.getElementById('stateSelect').parentNode;
    parentObj.removeChild(selObj);
  // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", "stateSelect");
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("name", "state");
    inputEl.setAttribute("size", 20);
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}
