String.prototype.trim = function(){
return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.startsWith = function(str){
return (this.match("^"+str)==str);
}

String.prototype.endsWith = function(str){
return (this.match(str+"$")==str);
}
String.prototype.contains = function(str){
return (this.indexOf(str)>=0);
}
Array.prototype.contains = function(searchStr) {
	return (this.getIndex(searchStr) == -1?  false: true);
}

Array.prototype.getIndexStartsWith = function(searchStr) {
   var pos = -1;
   for (var i=0; i<this.length; i++) {
     if (this[i].startsWith(searchStr)) {
		pos = i;
		break;
	}
   }
   return pos;
}
Array.prototype.getIndex = function(searchStr) {
   var pos = -1;
   for (var i=0; i<this.length; i++) {
     if (this[i]==searchStr) {
		pos = i;
		break;
	}
   }
   return pos;
}
function checkMustNotRadio(obj, pos, msgtext){
	if(obj){
		if(obj[pos].checked){
			alert(msgtext);
			obj[pos].checked = false;
			return true;
		}
	}
	return false;
}

function setSelectMulitViewValue(name, val){
	var obj = document.UserForm[name];
	var isFound = false;
	if(obj){
		if(obj.type == "select-one"){
			for(var i =0; i< obj.length; i++){
				if(obj[i].value == val){
					obj[i].selected = true;
					isFound = true;
					break;
				}
			}

			if(!isFound){
				obj[0].selected = true;
			}
		}
	}
}


function setFormValue(name, val){
	var obj = document.UserForm[name];
	if(obj){
		if(obj.type == "select-one"){
			for(var i =0; i< obj.length; i++){
				if(obj[i].value == val){
					obj[i].selected = true;
					break;
				}
			}
		}
		else if(obj.type == undefined){
			for(var i =0; i< obj.length; i++){
				if(obj[i].value == val){
					obj[i].checked = true;
					break;
				}
			}
		}
		else{
			obj.value = val;
		}
	}
}
function checkGeneralEmpty(name, msgtext){
	var obj = document.UserForm[name];
	if(obj){
		if(obj.type == undefined){
			var flag = true;
			for (var i = 0; i < obj.length; i++){
				if(obj[i].checked){
					flag = false;
					break;
				}
			}
			if(flag){
				if(msgtext !=""){
					alert(msgtext);
					if(obj.length>0){
						obj[0].focus();
					}
				}
				return true;
			}
		}
		else{
			if(obj.value == ''){
				if(msgtext !=""){
					alert(msgtext);
					obj.focus();
				}
				return true;
			}
		}
	}
	return false;
}
function getGeneralVal(name){
	var obj = document.UserForm[name];
	if(obj){
		if(obj.type == "select-one"){
			return obj.value;
		}
		else if(obj.type =="checkbox"){
			if(obj.checked){
				return obj.value;			
			}			
		}
		else if(obj.type == undefined){			
			for (var i = 0; i < obj.length; i++){
				if(obj[i].checked){
					return obj[i].value;
				}
			}
		}		
		else{
			return obj.value;			
		}
	}
	return "";
}

function showItBlock(givenname){
	var atag = document.getElementById(givenname);
	if(atag){
		atag.className = 'show_block';
	}
}

function hideItBlock(givenname){
	var atag = document.getElementById(givenname);
	if(atag){
		atag.className = 'hide_block';
	}
}

function showIt(givenname){
	var atag = document.getElementById(givenname);
	if(atag){
		atag.style.display ="block";
	}
}

function hideIt(givenname){
	var atag = document.getElementById(givenname);
	if(atag){
		atag.style.display ="none";
	}
}

function unloadPage(){
	hideIt("busyIcon");
	showIt("nextButton");
}

function startAnimation(){
	var atag = document.getElementById("busyimage");
	if(atag){
		atag.src = "images/busy.gif";
	}
}

function onKeyPressOnlyNumbers(e){
	var key;
	var keychar;

	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) ||
		(key==13) || (key==27) || (key==37) || (key==39) || (key==46))
	   return true;
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	return false;
}
function trimString(str) {
	if(str == null)
		return '';
	return str.replace(/^\s+|\s+$/g, '') ;
}
function isEmpty(value){
	if(value){
		return value.length ==0? true : false;
	}
	return true;
}
function getQuestionText(id){
	var atag = document.getElementById("QuestText"+id);
	if(atag){
		return atag.innerHTML;
	}
	return "Please answer the question(s)";
}
function setDefaultValues(){
	if( document.UserForm.SOURCE.value !=""){
		if(checkGeneralEmpty("BESTTIMECALL", "")){
			setFormValue("BESTTIMECALL", 'Morning');
		}
		if(checkGeneralEmpty("CITIZEN", "")){
			setFormValue("CITIZEN", 'Yes');
		}
		if(checkGeneralEmpty("FOREIGNCREDIT", "")){
			setFormValue("FOREIGNCREDIT", 'No');
		}
		if(checkGeneralEmpty("EXTRAVARIABLE1", "")){
			setFormValue("EXTRAVARIABLE1", 'No');
		}
		if(checkGeneralEmpty("EXTRAVARIABLE8", "")){
			setFormValue("EXTRAVARIABLE8", '2');
		}
		if(checkGeneralEmpty("EXTRAVARIABLE9", "")){
			setFormValue("EXTRAVARIABLE9", '1');
		}
		if(checkGeneralEmpty("EXTRAVARIABLE5", "")){
			setFormValue("EXTRAVARIABLE5", 'Yes');		
		}
		if(checkGeneralEmpty("EXTRAVARIABLE6", "")){
			setFormValue("EXTRAVARIABLE6", 'Yes');
		}
		if(checkGeneralEmpty("EXTRAVARIABLE7", "")){
			setFormValue("EXTRAVARIABLE7", 'Yes');
		}
		if(checkGeneralEmpty("EXTRAVARIABLE30", "")){
			setFormValue("EXTRAVARIABLE30", 'Yes');		
		}
		if(checkGeneralEmpty("JOININGTIME", "")){
			if(document.UserForm.JOINYEAR){
				setFormValue("JOININGTIME", getCurMonth());
				setFormValue("JOINYEAR", getCurYear());
			}
			else{
				setFormValue("JOININGTIME", '0');
			}
		}

		if(document.UserForm.contact && !document.UserForm.contact.checked){
			if(document.UserForm.SCHOOLID.value=="85" || document.UserForm.SCHOOLID.value=="80"){
			}
			else{
				document.UserForm.contact.checked = true;
			}
		}
	}
}
function showAlert(msg){
	if(msg.length>0){
		var phoneCode ="";
		if(msg.startsWith("w")){
			phoneCode ="daytime ";
			msg = msg.substring(1);
		}
		else if(msg.startsWith("h")){
			phoneCode ="evening ";
			msg = msg.substring(1);
		}
		if(msg == "-1-"){
			alert("Please correct your name and submit it again. Thank You!");
		}
		else if(msg == "-2-"){
			alert("Please correct your street address and submit it again. Thank You!");
			document.UserForm.ADDRESS.focus();
		}
		else if(msg == "-3-"){
			alert("Our system is unable to accept your email address. Please provide an alternative VALID email address. Thank you!");
			document.UserForm.EMAIL.focus();
		}
		else if(msg == "-6-"){
			alert("Please correct your "+phoneCode+"phone number and submit it again. Thank You!");
		}
		else if(msg =="-2-6-"){
			alert("Your "+phoneCode+"phone number is invalid, please correct it!");
		}
		else{
			alert("Invalid input data, please verify all entries and submit it again. Thank you!");
		}

		if(document.UserForm.contact){
			document.UserForm.contact.checked = true;
		}
	}
}
function showAlertZip(oldzip, type){
	var curzip = document.UserForm.ZIP.value;
	if(curzip !="" && (oldzip+"") == curzip){
		switch(type){
			case 1:
				alert("Please enter a valid Zip Code. Thank You!");
				break;
			case 2:
				alert("Please enter a valid Zip Code in the USA. Thank You!");
				break;
			case 3:
				alert("We are sorry, there is no program matched based on your location and/or education level. Thank You!");
				break;
		}
	}
	document.UserForm.ZIP.focus();
}
function checkzip(zipnum){
	var numExp = /[^0-9]/;
	if(numExp.test(zipnum)){
		return 0;
	}

	if(zipnum.length!=5){
		return 0;
	}

	var invalidZip = Array(
		"000", "001", "002", "003", "004", "099", "213", "269", "343", "345",
		"348", "353", "419", "428", "429", "517", "518", "519", "529", "533",
		"536", "552", "568", "578", "579", "589", "621", "632", "642", "643",
		"659", "663", "682", "694", "695", "696", "697", "698", "699", "702",
		"709", "715", "732", "742", "771", "817", "818", "819", "839", "848",
		"849", "851", "854", "858", "861", "862", "866", "867", "868", "869",
		"872", "876", "886", "887", "888", "892", "896", "899", "909", "929",
		"987");
	if(invalidZip.contains(zipnum.substring(0,3))){
		return 0;
	}
	return 1;
}

function setFocus(name){
	var obj = document.UserForm[name];
	if(obj){
		if(obj.type == undefined){
			obj[0].focus();
		}
		else{
			obj.focus();
		}
	}
}
function getInt(val){
	return getIntByDef(val, 0);
}
function getIntByDef(val, def){
	var intval = parseInt(val, 10);
	if(isNaN(intval)){
		return def;
	}
	return intval;
}
function getAgeByYear(val){
	return getCurYear() - val;
}
function getCurYear(){	
	try{
		return (new Date()).getFullYear();
	}
	catch(err){
		return 2010;
	}
}
function getCurMonth(){	
	try{
		return (new Date()).getMonth() + 1;
	}
	catch(err){
		return 1;
	}
}

function STMRCWindow(page, sHeight, swidth) {
	if (swidth && sHeight) {
			window.open(page, "CtrlWindow", ",toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,dependent=no,directories=no,width="+swidth+",height="+sHeight+",x=50,y=50");
	}
	else {
		window.open(page, "CtrlWindow", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,dependent=no,directories=no,width=455,height=540,x=50,y=50");
	}
}
function popUp(URL, swidth, sheight) {
	 var day = new Date();
	 var id = day.getTime();

	if (swidth && sheight) {
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+swidth+",height="+sheight+",left=150,top=200');");
	}
	else{
		eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=620,height=300,left = 150,top = 200');");
	}
}