isIE = (navigator.userAgent.indexOf("MSIE") > -1)? true:false;
var CN = (isIE)? "children":"childNodes";
var PN = (isIE)? "parentElement":"parentNode";
var sElem = (isIE)? "srcElement":"target";
var tgName = (isIE)? "tagName":"nodeName";
var CNindx = (isIE)? 0:1;
var kCode = (isIE)? "keyCode":"which";
//document.oncontextmenu = new Function("return false");

Array.prototype.isIn = isInArray;

function isInArray(val) {
	for (var ii=0; ii<this.length; ii++) {
		if (this[ii] == val) {
			return ii;
			break;
		}
	}
	return -1;
}
//window.onerror = errHandler;

if (document.addEventListener) {  
	document.addEventListener("onmousemove", mover, true);
}
else if (document.attachEvent) {  
	document.attachEvent("onmousemove", function(evt) {mover(evt)});
}
function decHTMLStr(str) {
	reQuot = /&quot;/g;
	reStT = /&lt;/;
	reEnT = /&gt;/;
	reSQuot = /&lsquo;/g;
	str = str.replace(reQuot, "\"");
	str = str.replace(reSQuot, "'");
	str = str.replace(reStT, "<");
	str = str.replace(reEnT, ">");
	return str;
}
function encHTMLStr(str) {
	reQuot = /\"/g;
	reStT = /</;
	reEnT = />/;
	reSQuot = /'/;
	str = str.replace(reQuot, "&quot;");
	str = str.replace(reSQuot, "&lsquo;");
	str = str.replace(reStT, "&lt;");
	str = str.replace(reEnT, "&gt;");
	return str;
}
function mover(evnt) {
	if (evnt[sElem][tgName] == "A") {
		window.status = evnt[sElem].innerHTML.replace("<IMG src=\"/bc/images/@.gif\" border=0>","@");
		return true;
	}
	else if (evnt[sElem][tgName] == "IMG") {
		window.status = evnt[sElem].alt;
		return true;
	}
}
function stripAmp(str) {
	var amp = /\&amp;/g;
	return str.replace(amp, "&");
}
function gEl(obj) {
	return document.getElementById(obj);
}
function gElt(obj) {
	return document.getElementsByTagName(obj);
}

function wd(str) {
	gEl("debugger").innerHTML += (str + "<br>");
}

function clearWD() {
	gEl("debugger").innerHTML = "";
}
function showProps(elem) {
	wdPop("<b style='color:blue'>SHOW PROPERTIES</b>");
	for (x in elem) {
		try {
			if (elem[x]) {
				wdPop(x + ": " + elem[x]);
			}
		}
		catch (err) {
			wdPop("<b style='color:red'>ERROR:</b> " + x);
		}
	}
}
function moverTR(tr) {
	if (tr.className != "selTD") {
		tr.className = "mover";
	}
}
function moutTR(tr) {
	if (tr.className != "selTD") {
		tr.className = "mout";
	}
}
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
/******** AJAX **************/
var http_request = false;
function getHttpRequest() {
	if (window.XMLHttpRequest) { //Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { //IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if (!http_request) {
		alert("Giving up: (Cannot create an XMLHTTP instance");
		return false;
	}
	return true;
}
function makeRequest(url, mthd) { 
	http_request = false;
	getHttpRequest();
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				formatRequest(http_request.responseText, mthd);
			}
			else {
				formatRequest("ERROR: There was a problem locating the requested resource.");
			}
		}
	}
	http_request.open('GET', url, true);
	http_request.send(null);
}
function formatRequest(txt, mthd) {
	switch (mthd) {
		case "updateBusiness":
			updateBusiness(txt);
			break;
		case "showStates":
			showStates(txt);
			break;
		case "showTowns":
			showTowns(txt);
			break;
	}
}
function getStates(cntry) {
	gEl("stateImg").src = "/bc/images/ajax-loader.gif";
	makeRequest("getLocation.asp?cntry=" + cntry, "showStates");
}
function showStates(str) {
	if (str.length > 0 && str.indexOf("~") > -1) {
		sel = document.srchForm.srchState;
		while (sel.options.length > 1) {
			sel.options[1] = null;
		}
		optsArr = str.split("~");
		for (var i=0; i<optsArr.length; i++) {
			if (optsArr[i].length > 0) {
				var opt = new Option(optsArr[i],optsArr[i]);
				sel.options[sel.options.length] = opt;
			}
		}
		gEl("stateTR").style.display = (isIE)? "inline":"table-row";
	}
	else {
		gEl("stateTR").style.display = "none";
	}
	gEl("stateImg").src = "images/blank.gif";
	gEl("townTR").style.display = "none";
}
function getTowns(stat, cntry) {
	gEl("townImg").src = "/bc/images/ajax-loader.gif";
	makeRequest("getLocation.asp?state=" + stat + "&cntry=" + cntry, "showTowns");
}
function showTowns(str) {
	if (str.length > 0 && str.indexOf("~") > -1) {
		sel = document.srchForm.srchTown;
		while (sel.options.length > 1) {
			sel.options[1] = null;
		}
		optsArr = str.split("~");
		for (var i=0; i<optsArr.length; i++) {
			if (optsArr[i].length > 0) {
				var opt = new Option(optsArr[i],optsArr[i]);
				sel.options[sel.options.length] = opt;
			}
		}
		gEl("townTR").style.display = (isIE)? "inline":"table-row";
	}
	else {
		gEl("townTR").style.display = "none";
	}
	gEl("townImg").src = "images/blank.gif";
}
var debugWin = null;
function wdPop(str) {
	if (!top.debugWin || top.debugWin.closed) {
		top.debugWin = window.open("","debugWin","target=debugWin,width=600,height=400,scrollbars=1");
		top.debugWin.document.open();
		top.debugWin.document.write("<html><head><title>Debugger</title><style>body {font-family:verdana; font-size:xx-small } </style></head><body><a href='javascript:void(0)' onclick=\"document.getElementById('debugger').innerHTML = '';\">Clear</a><br><div id=debugger></div></body></html>");
		top.debugWin.document.close();
	}
	top.debugWin.document.getElementById("debugger").innerHTML += (str + "<br>");
}
var cWin = null;
function errHandler(msg, url, lno) {
	url = "errHandler.asp?msg=" + msg + "&page=" + location.pathname + "&lno=" + lno + "&brow=" + navigator.userAgent + "&dateStr=" + new Date();
	if (cWin) {
		cWin.close();
	}
	cWin = window.open(url,"cWin","target=cWin,width=500,height=400,scrollbars=1");
	return null;
}
function validateEmail(theVal) {
	var tmp = "INVALID E-MAIL ADDRESS\n\n";
	var ShowErr = false;
	myNameRE = /\w{1,}\.*\w*\@[A-Za-z0-9-_]{1,}\.\w{2,3}\.*\w*/;
	var Err = myNameRE.exec(theVal);
	if(Err != theVal) {
		tmp += "E-mail Address is not in the correct format.\n";
		ShowErr = true;
	}
	else {
		var ValArray = theVal.split("@");
		var Address = ValArray[1].split(".");
		if (Address.length == 2) {
			if(Address[1].length > 3 || Address[1].length < 2) { 
				tmp += "Address suffix should be two or three characters eg: .com, .us\n";
				ShowErr = true;
			}
		}
		else if(Address.length == 3) {
			if(Address[1].length > 3 || Address[1].length < 2) {
				tmp += "Address suffix 1 should be two or three characters\n";
				ShowErr = true;
			}
			if(Address[2].length > 3 || Address[2].length < 2) {
				tmp += "Address suffix 2 should be two or three characters\n";
				ShowErr = true;
			}
		}
	}
	if (ShowErr) {
		return tmp;
		//return false;
	}
	return false;
}
function validateURL(theURL) {
	var tomatch= /https?:\/\/[A-Za-z0-9\.-_]{3,}\.[A-Za-z0-9\.-_]{2,3}/
	if (tomatch.test(theURL)){
		return false;
	}
	else {
		if (theURL.indexOf("http:") == -1) {
			return "Please enter the full URL, including the protocol i.e http://";
		}
		else {
			return "The format of the URL you have entered is incorrect.";
		}
	}
}
function stripURL(theURL) {
	/*
	rehttp = /https?:\/\//;
	theURL = theURL.replace(rehttp, "");
	if (theURL.indexOf("/") > -1) {
		theURL = theURL.substring(0, theURL.lastIndexOf("/"));
	}
	*/
	return theURL;
}
function showInFrme() {
	W = gElt("body")[0].offsetWidth;
	gEl("inFrme").style.left = (W - 750) / 2;
	gEl("inFrme").style.top = gElt("body")[0].scrollTop + 100;
	gEl("inFrme").style.display = (isIE)? "inline":"block";
	gEl("cardBack").style.left = ((W - 750) / 2) - 7;
	gEl("cardBack").style.height = gEl("cardBox").offsetHeight;
	gEl("cardBack").style.top = gElt("body")[0].scrollTop + 107;
	gEl("cardBack").style.display = (isIE)? "inline":"block";
	gEl("innerFrmBack").style.display = (isIE)? "inline":"block";
	gEl("innerFrmBack").style.height = gElt("body")[0].scrollHeight;
	//gElt("body")[0].style.overflow = "visible";
}
function hideInFrme() {
	gEl("inFrme").style.display = "none";
	gEl("cardBack").style.display = "none";
	gEl("innerFrmBack").style.display = "none";
	//gElt("body")[0].style.overflow = "auto";
}
function openContact(lnk) {
	window.open(lnk.href, "_blank", "target=_blank,width=600,height=450");
	return false;
}
function updateBusiness(str) {
	gEl("lum").style.display = "none";
	retArr = str.split("~");
	frm = document.cdForm;
	if (retArr.length == 6) {
		frm.bussName.value = retArr[0];
		frm.bussDesc.value = retArr[1];
		frm.busSubCatID.value = retArr[2];
		frm.bussID.value = retArr[4];
		frm.bussName.disabled = true;
		frm.bussDesc.disabled = true;
		frm.busSubCatID.disabled = true;
		gEl("currContacts").innerHTML = retArr[5];
	}
	else {
		frm.bussName.value = "";
		frm.bussDesc.value = "";
		frm.busSubCatID.value = "";
		frm.bussID.value = "";
		frm.bussName.disabled = false;
		frm.bussDesc.disabled = false;
		frm.busSubCatID.disabled = false;
		gEl("currContacts").innerHTML = "None";
	}
}
function doSearch() {
	if (document.srchForm.s.value.length == 0 && document.srchForm.srchCat.value.length == 0) {
		alert("Please enter a 'Find' value or select a Business Category");
		return false;
	}
	return true;
}
function checkForm(frm) {
	if (frm.btnLogin) {
		return true;
	}
	else {
		mandFields = ["bussURL","bussName","busSubCatID","fullName","bussPos","bussCountryID","targetArea","email","pwd","pwd2"];
		if (gEl("townTD").innerHTML.indexOf("*") > -1) {
			mandFields.push("bussTown");
		}
		if (gEl("stateTD").innerHTML.indexOf("*") > -1) {
			mandFields.push("bussState");
		}
		for (var i=0; i<mandFields.length; i++) {
			if (document.cdForm[mandFields[i]].value.length == 0) {
				alert("Please provide a value for all required fields");
				return false;
				break;
			}
		}
		return true;
	}
}
function setMandatory(tar) {
	if (tar == "Local") {
		gEl("townTD").innerHTML = "<b class=mand>*</b><label>City/Towm</label>";
		gEl("stateTD").innerHTML = "<b class=mand>*</b><label>State/Province</label>";
	}
	else if (tar == "Regional") {
		gEl("townTD").innerHTML = "<label>City/Towm</label>";
		gEl("stateTD").innerHTML = "<b class=mand>*</b><label>State/Province</label>";
	}
	else {
		gEl("townTD").innerHTML = "<label>City/Towm</label>";
		gEl("stateTD").innerHTML = "<label>State/Province</label>";
	}
}