// Hide script from old browsers
// Make sure file is not hosted in a different frameset

if(top.location.href != window.location.href){
	top.location.href = window.location.href;
}

var xmlHttp = httpGetObject();

function openWindow(url, w, h){
	var windowprops = "width=" + w + ",height=" + h +",left=10,top=10,scrollbars=no,status=yes,toolbar=no,location=no,directories=no,resizable=yes";
	popup = window.open(url, 'newWin', windowprops);
	popup.focus();
	return popup;
}

function openWindowWithScroll(url, w, h){
	var windowprops = "width=" + w + ",height=" + h +",left=10,top=10,scrollbars=yes,status=yes,toolbar=no,location=no,directories=no,resizable=yes";
	popup = window.open(url, 'newWin', windowprops);
	popup.focus();
	return popup;
}

function CheckAll(bChecked){
	var tg = document.getElementsByTagName("input");
	for(i = 0; i < tg.length; i++){
		if(tg.item(i).name == "action[]"){
			tg.item(i).checked = bChecked;
		}
	}
}

function switchClass(obj,strClassName){
	obj.className = strClassName;
}

function gotoURL(strUrl){
	location = strUrl;
}

function copyrightYear(startYear){
	d = new Date();
	if(startYear != d.getFullYear()){
		return startYear + " - " + d.getFullYear();
	} else {
		return startYear;
	}
}

function validZIP(val){
	re = new RegExp(/^\d{5,5}$/);
	if(val.match(re)){
		return true;
	} else {
		return false;
	}
}

function validateSearch(f){
	if(f.search.value == ''){
		alert('Please enter search condition.');
		f.search.focus();
		return false;
	}

	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			if(xmlHttp.status == 200){
				document.location = '/Search_Results';
			}
		}
	}

	var url = '/store_search_condition.php?search=' + f.search.value;

	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);

	return false;
}

function validState(str){
	re = /^[a-zA-Z]{2}$/;
	if(str == ''){
		return false;
	} else if(re.test(str)){
		return true;
	} else {
		return false;
	}
}

function existingZIP(val){
	if(xmlHttp){
		xmlUrl = '/existingzip.php?zipcode=' + val;
		xmlHttp.open('GET', xmlUrl, false);
		xmlHttp.send(null);
		return !!parseInt(xmlHttp.responseText);
	} else {
		alert('httpGetObject does not exist');
		return false;
	}
}

function validAreaCode(str){
	areacode = str.substring(0,3);
	if(existingAreaCode(areacode)){
		return true;
	} else {
		return false;
	}
}

function existingAreaCode(val){
	if(xmlHttp){
		xmlUrl = '/existingareacode.php?areacode=' + val;
		xmlHttp.open('GET', xmlUrl, false);
		xmlHttp.send(null);
		return !!parseInt(xmlHttp.responseText);
	} else {
		alert('httpGetObject does not exist');
		return false;
	}
}

function isInt(val){
	val = val.toString();
	if(val == ''){
		return false;
	} else {
		return (!isNaN(val) && val.indexOf('.', 0) == -1);
	}
}

function fixNum(el){
	el.value = el.value.replace(/\D/g, '');
}

/**
 * Trim string functons
 */
function RTrim(str){
	var w_space = String.fromCharCode(32);
	var v_length = str.length;
	var strTemp = '';
	if(v_length < 0){
		return '';
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(str.charAt(iTemp) == w_space){
		} else {
			strTemp = str.substring(0, iTemp + 1);
			break;
		}
		iTemp = iTemp - 1;
	}
	return strTemp;
}

function LTrim(str){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return '';
	}
	var v_length = str.length;
	var strTemp = '';

	var iTemp = 0;

	while(iTemp < v_length){
		if(str.charAt(iTemp) == w_space){
		} else {
			strTemp = str.substring(iTemp, v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}

function Trim(str){
	if(str.length < 1){
		return '';
	}
	str = RTrim(str);
	str = LTrim(str);
	if(str == ''){
		return '';
	} else {
		return str;
	}
}
