if (window.ActiveXObject) { 
	var classLiteral = "className"; // Internet Explorer
} else { 
	var classLiteral = "class"; // other (e.g. Firefox)
}

// GLOBAL VARIABLES \\
var gPP_User = '';
var gPP_Site = '';
var gBrowser = navigator.appName;
var gVendor = "";
var gProductList = ""; 	// Global XML representation of the product list 
var gStyle = true; 		// Use this to paint alternate rows in tables
var gName = "";
var gDom = "";
var gStep = 0;
var gCart = new Array();
var gAcct = new eAccounts();
var gSess = '';

function eAccounts() {
	this.name = new Array();
	this.price = new Array();
}

// INITIALIZE EVENT HANDLERS
window.onload = function () {	

	var dd = window.location.href;
	if (dd.indexOf('tst01') < 0) {
		// LIVE
		gPP_User = 'sales101@emailvanity.com';
		gPP_Site = 'https://www.paypal.com/cgi-bin/webscr';
	} else {
		// SANDBOX
		gPP_User = 'seller_1214403079_biz@limbicshift.com';
		gPP_Site = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
	}

	document.getElementsByName("sName")[0].focus();
	
	// REMOVE BULLSHIT CHARACTERS AND CODE
	document.getElementsByName("sName")[0].onkeyup = function() {
		gName = document.getElementsByName("sName")[0].value;
		gName = gName.replace('`', '');
		gName = gName.replace('~', '');
		gName = gName.replace('!', '');
		gName = gName.replace('@', '');
		gName = gName.replace('#', '');
		gName = gName.replace('$', '');
		gName = gName.replace('%', '');
		gName = gName.replace('^', '');
		gName = gName.replace('&', '');
		gName = gName.replace('*', '');
		gName = gName.replace('(', '');
		gName = gName.replace(')', '');
		gName = gName.replace('<', '');
		gName = gName.replace('>', '');
		gName = gName.replace('/', '');
		gName = gName.replace('\\', '');
		gName = gName.replace('|', '');
		gName = gName.replace('{', '');
		gName = gName.replace('}', '');
		gName = gName.replace('[', '');
		gName = gName.replace(']', '');
		gName = gName.replace('?', '');
		gName = gName.replace(':', '');
		gName = gName.replace(';', '');
		gName = gName.replace(',', '');
		gName = gName.replace('.', '');
		gName = gName.replace("'", '');
		gName = gName.replace('"', '');
		gName = gName.replace('+', '');
		gName = gName.replace('=', '');
		document.getElementsByName("sName")[0].value = gName;
	}

	document.getElementsByName("frmSearch")[0].onsubmit = function() {
		showMsg('');

		gName = document.getElementsByName("sName")[0].value.toLowerCase();
		gDom = document.getElementsByName("sDom")[0].value.toLowerCase();

		document.getElementsByName("sName")[0].value = gName;
		document.getElementsByName("sDom")[0].value = gDom
		
		if (gName.length == 0) {
			showMsg('<p align="center">Please enter a name.</p>');
			return false;
		} else if (gDom.length == 0) {
			showMsg('<p align="center">Please enter a domain.</p>');
			return false;
		} else {
			if (!domIsValid(gDom)) {
				showMsg('<p align="center">Invalid domain - please try again.</p>');
				return false;
			} else {
				var sName = "sName=" + gName.toLowerCase();
				goAjax("nameNotRestricted.php", sName, "isNotRestricted", false);
				return false;
			}
		}
	}
		
	document.getElementById("divSearchResults").style.display = "none"; // Hide Search Results
	document.getElementById("divFinalCheckOut").style.display = "none";

	document.getElementById("divMsg").onclick = function () {
		showMsg('');
	}
}    

function isNotRestricted(i) {
	if(i==1) {
		showMsg('<p align="center">The account is unavailable.</p>');
		return false;
	} else {
		return accountSearch();
	}
}

///// SHOW MESSAGE \\\\\
function showMsg(msg) {
	var obj = document.getElementById("divMsg");
	if(msg.length==0) {
		obj.innerHTML = '';
		if(gBrowser == 'Opera') obj.style.visibility = 'hidden'; else obj.style.display = 'none';
	} else {
		obj.innerHTML = msg;
		if(gBrowser == 'Opera') obj.style.visibility = 'visible'; else obj.style.display = 'inline';
	}
}

///// ACCOUNT SEARCH \\\\\
function accountSearch() {	
	document.getElementById("searchMsg").innerHTML = "Searching....";	
	var sAccount = "sName=" + gName + "&sDom=" + gDom;
	goAjax("searchAccounts.php", sAccount, "handleSearchResults", true);
	return false;
}

function handleSearchResults(results) {
	//reset variable for style
	gStyle = true;

	//assign the results to our global variable
	gProductList = results;

	//clear the existing results if any
	var tblResults = document.getElementById("tblResults");

	//clear the existing node
	clearNode(tblResults);
			
	//get the results that were returned
	var total = results.getElementsByTagName("result").length; // the number of results

	if (total == 0) {
		showMsg('<p align="center">The account is unavailable.</p>');
		document.getElementById("divSearchResults").style.display = "none";
		document.getElementById("searchMsg").innerHTML = "";
		name = '--- unavailable ---';
		price = 0;
		appendSearchResult(name, price, tblResults);	
	} else {
		var name = gName + "@" + gDom;
		var currentResult = results.getElementsByTagName("result").item(0);
		var price = currentResult.getElementsByTagName("price").item(0).firstChild.nodeValue;
		appendSearchResult(name, price, tblResults);	
	}
	
	// "ADD TO CART" BUTTON
	var row = document.createElement("tr");		
	row.setAttribute(classLiteral, "total");
	var col = document.createElement("td");

	var btnAdd = document.createElement("button");	
	btnAdd.setAttribute("type","button");
	btnAdd.setAttribute("id","btnAdd");
	btnAdd.setAttribute("name","addtocart");	
	btnAdd.setAttribute(classLiteral,"cartbutton");	
	btnAdd.appendChild(document.createTextNode("Add to cart"));
	
	var rightcol = document.createElement("td");
	rightcol.setAttribute("align","center");
	
	rightcol.appendChild(btnAdd);
	row.appendChild(col);
	row.appendChild(rightcol);
	tblResults.appendChild(row);

	if(total > 0) {	
		// ADD TO CART CLICK
		btnAdd.onclick = function() {
			var addRecord = true;
			for(var i=0; i <= gAcct.name.length; i++) {
				if(name == gAcct.name[i]) {
					addRecord = false;
					break;
				}
			}
			if (addRecord == true) {
				showMsg('');
				gAcct.name.push(name);
				gAcct.price.push(price);
				var rec = 'acct=' + name  + '/' + price;
				return goAjax("cartAdd.php", rec, "handleCartUpdate", true);
			} else {
				showMsg('<p align="center">' + name + ' is already in the shopping cart.</p>');
			}
			return false;
		}
	} else {
		document.getElementById("btnAdd").disabled=true;
	}
	document.getElementById("searchMsg").innerHTML = "";							// hide the 'searching message'
	document.getElementById("divSearchResults").style.display = "inline";	// expose the search results section
	return false;
}

function appendSearchResult(name, price, tblResults) {	
	//get a handle to the table that holds the search results
	var tblResults = document.getElementById("tblResults");	
	var row = document.createElement("tr");
	gStyle = !gStyle;
	if(gStyle) a = "1"; else a="2";
	row.setAttribute(classLiteral,"searchresultstable" + a);	

	// name of account cell
	var namecell = document.createElement("td");
	namecell.setAttribute("id", "namecell-" + name)
	namecell.setAttribute("align","center");
	
	// name of account value
	var prodName = document.createTextNode(name);
	namecell.appendChild(prodName);
	row.appendChild(namecell);

	// price of account cell
	var pricecell = document.createElement("td");
	pricecell.setAttribute("align","center");	
	
	// price of account value
	var nPrice = price;
	var sPrice = Math.round(nPrice);
	pricecell.appendChild(document.createTextNode("$" + sPrice));
	row.appendChild(pricecell);	

	tblResults.appendChild(row);	
}

function appendCartEntry(name, price, tblResults) {
	//new row
	var row = document.createElement("tr");
	gStyle = !gStyle;
	if(gStyle) a = "1"; else a="2";
	row.setAttribute(classLiteral,"searchresultstable" + a);
	
	// ACCOUNT NAME
	var namecell = document.createElement("td"); 		//displays name of the product
	namecell.setAttribute("id", "namecell-" + name); 	//we'll need this cell later in showProductDetails()
	namecell.setAttribute("align","center");	
	
	// ACCOUNT PRICE
	var pricecell = document.createElement("td");
	
	// REMOVE CELL FOR CHECKBOX
	var removecell = document.createElement("td"); 
	removecell.setAttribute(classLiteral,"searchresultstable");	
	removecell.setAttribute("align","center");	

	// CHECKBOX
	var checkbox = document.createElement("input");	
	checkbox.setAttribute("type","checkbox");
	checkbox.setAttribute("value", name);
	
	//add product name
	namecell.appendChild(document.createTextNode(name));
	row.appendChild(namecell);
	
	//add price
	var nPrice = price;
	var sPrice = Math.round(nPrice);
	pricecell.appendChild(document.createTextNode("$" + sPrice));
	pricecell.setAttribute("align","center");	
	row.appendChild(pricecell);	
	
	//add checkbox
	removecell.appendChild(checkbox);	
	row.appendChild(removecell);
		
	tblResults.appendChild(row);
}

function handleCartUpdate(results) {
	gStyle = true;
	
	document.getElementById("divCart").style.display = "inline";
	
	//clear the existing results if any
	var cartcontentstable = document.getElementById("cartcontentstable");
	var child = cartcontentstable.childNodes[0];
	while(child != null)	{
		cartcontentstable.removeChild(child);
		child = cartcontentstable.childNodes[0];
	}
			
	//get the results that were returned
	gSess = results.getElementsByTagName("sess").item(0).firstChild.nodeValue;; 		// get the session id
	var itemsInCart = results.getElementsByTagName("item").length; 						// the number of items in the cart

	var total = 0;
	for(i=0; i < itemsInCart; i++) {
		var currentResult = results.getElementsByTagName("item").item(i);
		var name = currentResult.getElementsByTagName("name").item(0).firstChild.nodeValue;
		var price = currentResult.getElementsByTagName("price").item(0).firstChild.nodeValue;
		total += (price*1);
		appendCartEntry(name, price, cartcontentstable);
	}	

	//add the total and 'add to cart' link
	if(itemsInCart > 0) {	
		//total
		var trow = document.createElement("tr");		

		trow.setAttribute(classLiteral, "total");
		var tcol = document.createElement("td");
		tcol.setAttribute("align","right");
		tcol.appendChild(document.createTextNode("TOTAL:"));
		trow.appendChild(tcol);		
	
		// TOTAL AMOUNT CELL
		var col = document.createElement("td");
		col.setAttribute("align","center");
		var nTotal = total;
		var sTotal = Math.round(nTotal);
		col.appendChild(document.createTextNode("$" + sTotal));
		trow.appendChild(col);
	
		//delete from cart cell
		var col = document.createElement("td");
		col.setAttribute("colspan", "4");
		col.setAttribute("align","center");
		trow.appendChild(col);
		
		var btnRemove = document.createElement("button");	
		btnRemove.setAttribute("type","button");
		btnRemove.setAttribute("name","deletefromcart");
		btnRemove.setAttribute(classLiteral,"cartbutton");
		btnRemove.appendChild(document.createTextNode("Remove"));			
		col.appendChild(btnRemove);	
		trow.appendChild(col);
		
		//checkout button
		col.appendChild(document.createElement("br"));		
		var btnCheckout = document.createElement("button");	
		btnCheckout.setAttribute("type","button");
		btnCheckout.setAttribute("name","checkoutfromcart");
		btnCheckout.setAttribute(classLiteral,"cartbutton");
		btnCheckout.appendChild(document.createTextNode("Checkout"));			
		col.appendChild(btnCheckout);	
					
		//add the total + delete-link row to the table
		cartcontentstable.appendChild(trow);
		
		//event handler for the delete button
		btnRemove.onclick = function() {
			showMsg('');
			return deleteFromCart();
		}
		
		//event handler for the checkout button
		btnCheckout.onclick = function() {
			showMsg("");
			btnRemove.disabled = true;
			document.getElementById("btnSubmit").disabled = true;
			document.getElementById("btnAdd").disabled = true;
			document.getElementById("divPreCheckout").style.display = "inline";
			document.getElementById("sContact").focus();
			btnCheckout.disabled = true;
		}		
	} else {
		showMsg('');
		document.getElementById("divCart").style.display = "none";
		document.getElementById("divPreCheckout").style.display = "none";
		document.getElementById("divFinalCheckOut").style.display = "none";
		document.getElementById("chkVerifyTerms").checked = false;
	}
	return false;
}

function deleteFromCart() {	
	var pCode = document.getElementById("cartcontentstable").getElementsByTagName("input");
	var dCode = '';
	var sCart = "";	
	
	for(i=0; i < pCode.length; i++) {
		if(pCode.item(i).checked) {			
			dCode = pCode.item(i).value; //just need the product code
			for(j=0; j < gAcct.name.length; j++) {
				if(dCode == gAcct.name[j]) {
					gAcct.name.splice(j, 1);
					gAcct.price.splice(j, 1);
				}
			}
			sCart += "|" + dCode;
		}
	}	
	
	if(sCart.length == 0) {
		showMsg("Please select at least one product to delete.");
	} else {	
		sCart = sCart.substring(1);	// trim the first pipe
		sCart = 'acct=' + sCart;
		goAjax("cartDel.php", sCart, "handleCartUpdate", true);
	}
	return false;
}

function clearNode(node) {
	var child = node.childNodes[0];
	while(child != null) {
		node.removeChild(child);
		child = node.childNodes[0];
	}	
}

function domIsValid(s) {
	var eDoms = new Array("badkitty.bz", "badkitty.cc", "badkitty.org", "buddhabuddy.org", "bullriderson.com", 
			  "chitowndiva.com", "chitowndiva.net", "chitowndiva.org", "coolsitename.com", "downtowndiva.org", 
			  "girlygirl.bz", "givesgoodlove.com", "highspeedlowdrag.us", "holybitch.com", "holybitch.net", "holybitch.org", 
			  "killerbabe.net", "killerbabe.org", "leatherandlace.bz", "lighteningbolt.net", "lighteningbolt.org", "lightningbolt.us", 
			  "loveshorses.com", "loveshorses.net", "loveshorses.org", "neolives.com", "neolives.net", "neolives.org", 
			  "risetopower.org", "roguepoet.com", "roguepoet.net", "roguepoet.org", 
			  "sergeantrock.biz", "sergeantrock.net", "sergeantrock.org", "sergeantrock.us", 
			  "sgtrock.us", "soldierboy.us", "soldiergirl.us", "thefox.bz", "theblackflag.net", "theblackflag.org", 
			  "thebunnyboy.org", "theholybitch.com", "theholybitch.net", "theholybitch.org", 
			  "theroguepoet.com", "theroguepoet.net", "theroguepoet.org", 
			  "topcop.bz", "topcop.cc", "truegrit.org", "trugrit.us", 
			  "urbancowgirl.biz", "urbancowgirl.org", "urbancowgirl.us", "urbancowgirl.ws");
	var sFound = false;
	for (i in eDoms) {
		if (s == eDoms[i]) {
			sFound = true;
			break;
		}
	}
	return sFound;
}

function clearFields() {
	showMsg('');
	document.getElementsByName("sDom")[0].value = "";
	document.getElementsByName("sName")[0].value = "";
	document.getElementById("searchMsg").innerHTML="";	
	document.getElementsByName("sName")[0].focus();
}

function termsClicked() {
	var sCont = document.getElementById("sContact").value.toLowerCase();
	var sConf = document.getElementById("sConfirm").value.toLowerCase();

	document.getElementById("sContact").value	= sCont;
	document.getElementById("sConfirm").value	= sConf;

	var box = document.getElementById("chkVerifyTerms");

	if (box.checked == true) {
		if (sCont.length == 0) {
			alert("Please enter a contact email address.");
			document.getElementById("sContact").focus();
			box.checked = false;
		} else if (sConf.length == 0) {
			alert("Please enter a confirmation email address.");
			document.getElementById("sConfirm").focus();
			box.checked = false;
		} else if (sCont != sConf) {
			alert("Contact emails do not match.  Please enter again.");
			document.getElementById("sConfirm").focus();
			box.checked = false;
		} else {
			box.disabled = true;
			document.getElementById("sContact").disabled = true;
			document.getElementById("sConfirm").disabled = true;
			writeForm();
		}
	}
}

function storeCart(s) {
	gVendor = s;
	var sCont = 'sCont=' + document.getElementById("sContact").value + '&sVend=' + gVendor;
	return goAjax("cartStore.php", sCont, "handleCartStore", false);
}

function handleCartStore(i) {
	if(i=='err') {
		alert('There was an error storing the cart.');
	} else {
		return finalChkOut(gVendor);
	}
}

function finalChkOut(s) {
	var frm = document.getElementById('frmChkOut');
	switch (s) {
		case 'paypal':
			frm.action = gPP_Site;
			break;
		case 'google':
			frm.action = 'googletest01.php';
			break;
	}
	frm.submit();
}

function writeForm() {
	var sTop = "";
	var sLin = "";
	var sImg = "";
	var sBot = "";
	var str1 = "";
	var str2 = "";
	var s = "";

	sTop = '<form id="frmChkOut" method="post">' 
		  + '<input type="hidden" name="cmd" value="_cart">'
		  + '<input type="hidden" name="upload" value="1">'
		  + '<input type="hidden" name="business" value="' + gPP_User + '">'
		  + '<input type="hidden" name="custom" value="' + gSess + '">'
		  + '<input type="hidden" name="cancel_return" value="http://www.emailvanity.com/canc01/index.php">';
	for(var i=0; i < gAcct.name.length; i++) {
		s = i + 1;
		s1 = '<input type="hidden" name="item_name_' + s + '" value="' + gAcct.name[i] + '">';
		s2 = '<input type="hidden" name="amount_' + s + '" value="' + gAcct.price[i] + '">';
		s3 = '<input type="hidden" name="quantity_0' + s + '" value="1">';
		s4 = '<input type="hidden" name="on0_' + s + '" value="EMV">';
		s5 = '<input type="hidden" name="os0_' + s + '" value="' + s + '">';
		sLin = sLin + s1 + s2 + s3 + s4 + s5;
	}
	sBot = '</form>';
	sImg = '<input type="image" src="img/paypal.gif" onclick=storeCart("paypal") style="cursor: hand" alt="Pay Pal Button" width="143" height="24" />';
	document.getElementById("divFinalCheckOut").innerHTML = sTop + sLin + sBot + sImg;
	document.getElementById("divFinalCheckOut").style.display = "block";
}