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 setFormValue(obj, val){
	if(obj){
		obj.value = val;
	}
}
function showIt(name){
	var atag  = document.getElementById(name);	
	if (atag){
		atag.style.display = 'block';			
	}
}

function hideIt(name){
	var atag  = document.getElementById(name);	
	if (atag){
		atag.style.display = 'none';			
	}
}
function setElmtClass(name, classname){
	var atag  = document.getElementById(name);	
	if (atag){
		atag.className = classname;			
	}
}
function getInt(name){
	var intVal =parseInt(name, 10);
	if(isNaN(intVal)){
		return -1;
	}
	return intVal;
}

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 isExistById(id){
	return $('#'+id).length == 0? false: true;
}
function isExistByName(name){
	return $(name).length == 0? false: true;
}


function checkGeneralEmpty(name, msgtext){
	var obj = document.loanform[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 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 checkDyZip(value){	
	if(value.length ==5){
		if(checkzip(value) == 0){				
			return false;
		}
	}
	return true;
}

function checkzip(zipnum){
	var numExp = /[^0-9]/;
	if(numExp.test(zipnum)||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 checkPhoneArea(phonenum){
	var numExp = /[^0-9]/;
	if(numExp.test(phonenum) || phonenum.length!=3){
		return 0;
	}
	
	var areacode = new Array(
	"201", "202", "203", "204", "205", "206", "207", "208", "209", "210",
	"212", "213", "214", "215", "216", "217", "218", "219", "224", "225",
	"228", "229", "231", "234", "239", "240", "248", "250", "251", "252",
	"253", "254", "256", "260", "262", "267", "269", "270", "276", "281",
	"289", "301", "302", "303", "304", "305", "306", "307", "308", "309",
	"310", "312", "313", "314", "315", "316", "317", "318", "319", "320",
	"321", "323", "325", "330", "331", "334", "336", "337", "339", "340",
	"347", "351", "352", "360", "361", "386", "401", "402", "403", "404",
	"405", "406", "407", "408", "409", "410", "412", "413", "414", "415",
	"416", "417", "418", "419", "423", "424", "425", "432", "434", "435",
	"440", "443", "450", "469", "478", "479", "480", "484", "501", "502",
	"503", "504", "505", "506", "507", "508", "509", "510", "512", "513",
	"514", "515", "516", "517", "518", "519", "520", "530", "540", "541",
	"551", "557", "559", "561", "562", "563", "567", "570", "571", "573",
	"574", "580", "585", "586", "601", "602", "603", "604", "605", "606",
	"607", "608", "609", "610", "612", "613", "614", "615", "616", "617",
	"618", "619", "620", "623", "626", "630", "631", "636", "641", "646",
	"647", "650", "651", "660", "661", "662", "671", "678", "682", "701",
	"702", "703", "704", "705", "706", "707", "708", "709", "712", "713",
	"714", "715", "716", "717", "718", "719", "720", "724", "727", "731",
	"732", "734", "740", "754", "757", "760", "763", "765", "770", "772",
	"773", "774", "775", "778", "780", "781", "785", "786", "787", "801",
	"802", "803", "804", "805", "806", "807", "808", "810", "812", "813",
	"814", "815", "816", "817", "818", "819", "828", "830", "831", "832",
	"843", "845", "847", "848", "850", "856", "857", "858", "859", "860",
	"862", "863", "864", "865", "867", "870", "878", "901", "902", "903",
	"904", "905", "906", "907", "908", "909", "910", "912", "913", "914",
	"915", "916", "917", "918", "919", "920", "925", "928", "931", "936",
	"937", "939", "940", "941", "947", "949", "951", "952", "954", "956",
	"970", "971", "972", "973", "978", "979", "980", "985", "989");
	
	if(!areacode.contains(phonenum)){
		return 0;
	}
	return 1;
}


function printDollar(given){
	given =""+given;
	if(given.indexOf(".")>0){
		given = given.substring(0, given.indexOf("."));
	}	
	var strNum ="";
	var size = given.length;
	var time =0;
	for(i=size-1; i>=0; i--){
		if(time !=0){
			if(time%3 ==0){
				strNum = ","+strNum;
			}
		}
		strNum =  given.charAt(i)+ strNum;
		time++;
	}
	return strNum;
}
function getStateCode(stateid){

	var statecode = new Array( 
		"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", 
		"GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME",
		"MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
		"NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", 
		"SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", 
		"WY");
	var found = -1;
	
	for(i=0; i<statecode.length; i++){
		if(stateid ==statecode[i]){
			found = i;
			break;
		}
	}
	return found;	
}


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=no,dependent=no,directories=no,width=655,height=640,x=50,y=50");
	}
}
