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;
		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 "doMETA":
			doMETA(txt);
			break;
		case "checkLnk":
			checkLnk(txt);
			break;
		case "recipAdded":
			recipAdded(txt);
			break;
		case "getSiteMETA":
			getSiteMETA(txt);
			break;
		case "updateSubcats":
			updateSubcats(txt);
			break;
		case "updateSubcatPos":
			updateSubcatPos(txt);
			break;
		case "siteStats":
			gEl("siteStatsDiv").innerHTML = txt;
			break;
		case "cookieLoaded":
			cookieLoaded(txt);
			break;
		case "gotPR":
			gotPR(txt);
			break;
	}
}

function getSiteStats(theURL) {
	//makeRequest("http://www.adminstool.com/results.php?url=" + url, "siteStats");
	gEl("siteStatsDiv").innerHTML = "<p class=err><img src='/cd/images/ajax-loader.gif'> Retrieving statistics, please wait...</p>"
	rehttp = /https?:\/\//;
	theURL = theURL.replace(rehttp, "");
	if (theURL.indexOf("/") > -1) {
		theURL = theURL.substring(0, theURL.indexOf("/"));
	}
	//makeRequest("http://www.craigs-directory.com/cd/srcGrab.aspx?siteAdd=http://www.adminstool.com/results.php?url=" + theURL, "siteStats");
	makeRequest("http://www.craigs-directory.com/cd/siteInfo.aspx?siteAdd=" + theURL, "siteStats");
}
function showSubCats(catID) {
	gEl("metaImg").src = "images/ajax-loader.gif";
	makeRequest("/cd/getSubCats.asp?catID=" + catID, "updateSubcats")
}
function updateSubcats(str) {
	sel = document.forms[0].subCat;
	while (sel.options.length > 0) {
		sel.options[0] = null;
	}
	optsArr = str.split("~");
	for (var i=0; i<optsArr.length; i++) {
		if (optsArr[i].length > 0) {
			optsData = optsArr[i].split("|");
			var opt = new Option(optsData[1], optsData[0]);
			sel.options[sel.options.length] = opt;
		}
	}
	gEl("metaImg").src = "images/blank.gif";
	showSitePos(sel.value);
}
function showSitePos(subCatID) {
	if (subCatID.length > 0) {
		gEl("metaImg").src = "images/ajax-loader.gif";
		makeRequest("/cd/getSubCatListPos.asp?subCatID=" + trim(subCatID," "), "updateSubcatPos")
	}
	else {
		gEl("sitePos").innerHTML = "&nbsp";
	}
}
function updateSubcatPos(str) {
	if (!isNaN(str)) {
		txt = "<li>Your website will be listed as link number 1 in the '" + document.forms[0].subCat.options[document.forms[0].subCat.selectedIndex].text + "' sub-category";
		gEl("sitePos").innerHTML = txt;
	}
	else {
		gEl("sitePos").innerHTML = str;
	}
	gEl("metaImg").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) {
	//http://graduate-school-examples-samples.statementsofpurpose.com/
	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 checkSite() {
	gEl("errUrl").style.display = "none";
	if (document.forms[0].newSite.value.length > 0) {
		vURL = validateURL(document.forms[0].newSite.value);
		if (!vURL) {
			gEl("metaImg2").src = "images/ajax-loader.gif";
			makeRequest("/cd/checkURL.asp?url=" + stripURL(document.forms[0].newSite.value), "getSiteMETA");
		}
		else {
			alert(vURL);
			document.forms[0].newSite.focus();
			document.forms[0].newSite.select();
		}
	}
	//else {
	//	alert("Please enter the URL of your web site");
	//}
}
function stripURL(theURL) {
	rehttp = /https?:\/\//;
	theURL = theURL.replace(rehttp, "");
	/*
	if (theURL.indexOf("/") > -1) {
		theURL = theURL.substring(0, theURL.lastIndexOf("/"));
	}
	*/
	return theURL;
}
function doMETA(str) {
	document.forms[0].siteTitle.value = "";
	document.forms[0].metaWords.value = "";
	document.forms[0].metaDesc.value = "";
	var srch1 = "<meta ";
	var srch2 = ">";
	var srchStr = str.toLowerCase();
	var ttl = str.substring(srchStr.indexOf("<title>") + 7, srchStr.indexOf("</title>"));
	if (ttl.length > 0) {
		document.forms[0].siteTitle.value = ttl;
	}
	var x = srchStr.indexOf(srch1);
	while (x > -1) {
		metaTag = str.substring(x, srchStr.indexOf(srch2, x));
		metaSrch = metaTag.toLowerCase();
		if (metaSrch.indexOf("keywords") > -1) {
			document.forms[0].metaWords.value = stripMeta(metaTag);
		}
		if (metaSrch.indexOf("description") > -1) {
			document.forms[0].metaDesc.value = stripMeta(metaTag);
		}
		x = srchStr.indexOf(srch1, x+1);
	}
	//window.open("http://localhost/siteSnap/siteSnap2.aspx?siteAdd=" + document.forms[0].url.value, "siteSnap", "target=siteSnap,width=1024, height=768");
	gEl("metaImg2").src = "images/True.gif";
}
function stripMeta(mCont) {
	ctnt = metaTag.substring(metaTag.toLowerCase().indexOf("content=") + 8);
	reQuot = /\"/g;
	reEnT = />/;
	reSQuot = /'/;
	reFS = /\//;
	ctnt = ctnt.replace(reQuot, "");
	ctnt = ctnt.replace(reSQuot, "");
	ctnt = ctnt.replace(reEnT, "");
	ctnt = ctnt.replace(reFS, "");
	return ctnt;
}
function addCredit(sID) {
	makeRequest("/cd/addCredit.asp?sID=" + sID,"");
}
function doAdd(lnk) {
	addCredit(lnk.id.toString().split("_")[1]);
}
var entryCode = null;
function getCodeValue() {
	/*
	var head = document.getElementsByTagName("head")[0];
	script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "/owf/netComp.aspx";
	head.appendChild(script);
	*/
	makeRequest("/owf/netComp.aspx?c=" + Math.random(),"cookieLoaded");
}
function cookieLoaded(txt) {
	document.forms[0].srvrEntryCode.value = txt;
	showSitePos(document.forms[0].subCat.value);
}
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";
}
function hideInFrme() {
	gEl("inFrme").style.display = "none";
}
function openContact(lnk) {
	window.open(lnk.href, "_blank", "target=_blank,width=600,height=450");
	return false;
}
function showLink(lnk) {
	if (lnk.title) {
		no = lnk.className.substring(3) - 1;
		gEl('linkDesc').innerHTML = lnk.title;
		gEl('linkDesc').style.left = gEl("tblLinks").rows[0].cells[0].clientWidth * no;
	}
}
function clearLink() {
	gEl('linkDesc').innerHTML = "&nbsp;";
}