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)
}

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 reBuildRadio(name, value){
	var obj = document.UserForm[name];
	for(var i =0; i<document.UserForm[name].length; i++){		
		if(obj[i].value == value){
			obj[i].checked = true;
			break;
		}
	}
}

function setFormValue(name, val){
	var obj = document.UserForm[name];
	if(obj){
		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){
				alert(msgtext);	
				if(obj.length>0){
					obj[0].focus();
				}
				return true;
			}			
		}
		else{
			if(obj.value == ''){
				alert(msgtext);	
				obj.focus();
				return true;
			}
		}
	}
	return false;
}
function getGeneralVal(name){
	var obj = document.UserForm[name];
	if(obj){
		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 setRadioMust(name, objectName, pos){
	if(objectName){
		objectName[pos].checked = true;
	}
}
function setRadioMustNot(name, objectName, pos){
	if(objectName){
		objectName[pos].checked = false;
	}
}
function setSelectDefault(fieldName, value){
	if(fieldName){
		fieldName.value = value;
	}
}
function setSelectDefaultCheck(fieldName, value){
	if(fieldName){
		if(fieldName.value ==''){
			fieldName.value = value;
		}
	}
}
function uncheckRadio(obj, pos){
	if(obj){
		if(obj[pos].checked){					
			obj[pos].checked = false;			
		}
	}	
}

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;
}
//general
function autofocus(field, limit, next, evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
		((evt.which) ? evt.which : 0));
	if (charCode > 31 && field.value.length == limit) {
		field.form.elements[next].focus();
	}
}
function autoTab(element, nextElement) {
	if (element.value.length >= element.maxLength && nextElement != null) {
		var atag  = document.getElementById(nextElement);
		if (atag){
			atag.focus();
		}
	}	
}
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 checkEmptyRadio(obj){
	if(obj){
		for (var i = 0; i < obj.length; i++){
			if(obj[i].checked){					
				return false;
			}
		}
		return true;
	}
	return false;
}


function checkMustNotRadio(obj, pos, msgtext){
	if(obj){
		if(obj[pos].checked){
			alert(msgtext);			
			obj[pos].checked = false;
			return true;
		}
	}
	return false;
}

function getQuestionText(id){
	var atag = document.getElementById("QuestText"+id);
	if(atag){
		return atag.innerHTML;
	}
	return "Please answer the question(s)";
}

function autoTab(element, nextElement) {
	if (element.value.length >= element.maxLength && nextElement != null) {
		var atag  = document.getElementById(nextElement);
		if (atag){
			atag.focus();
		}
	}	
}
function getAgeByYear(val){
	var nowDate = new Date();	
	var now = 2008;
	try{
		now = nowDate.getFullYear();
	}
	catch(err){}
	
	return now - val;
}
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');");
	}
}

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;
}