// 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 switchClass(obj, strClassName){
	obj.className = strClassName;
}

function copyrightYear(startYear){
	d = new Date();
	if(startYear != d.getFullYear()){
		return startYear + " - " + d.getFullYear();
	} else {
		return startYear;
	}
}

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

	_url = '/store_search_condition.php?search=' + f.search.value;
	xmlHttp.open('GET', _url, true);
	xmlHttp.send(null);

	return false;
}

function validZIP(val){
	re = new RegExp(/^\d{5,5}$/);
	if(val.match(re)){
		return true;
	} else {
		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 UpdateArticlesCall(){
	if(xmlHttp){
		xmlHttp.open("GET", 'updatecontent.php?action=1', true);
		xmlHttp.onreadystatechange = handleRequest;
		xmlHttp.send(null);
	}
}

function handleRequest(){
	if(xmlHttp.readyState == 4){
		// loading finished
		if(xmlHttp.responseText.indexOf('invalid') == -1){
			document.location = 'news.php?reset=1&action=new';
		}
	}
}
