// JavaScript Document
var xhr = false;

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
function smoke(fullname,othermessage) {
	Growl.Smoke({
	title: ''+fullname,
	text: ''+othermessage,
	image: 'images/added_to_cart.png',
	duration: 2
	});
}
function formalert(msg,dur) {
	Growl.Bezel({
		title: 'There\'s an error in your form.<br><br>'+msg,
		image: 'images/error.png',
		duration: dur
	});
}
function AddToCart(product) {
	var url = 'includes/js-addtocart.php?product='+product;
	if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try {	xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } }
	if (xhr) {
		xhr.onreadystatechange = returnAddToCart;
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, but I couldn't create an XMLHttpRequest");
	}
}
function returnAddToCart() { if (xhr.readyState == 4) { if (xhr.status == 200) { var outMsg = xhr.responseText; } else { alert("There was a problem with the request " + xhr.status); }
		var res = outMsg.split('|');
		smoke(res[1],'added to your shopping cart');
		var spc = res[2].split('~');
		var gt = res[3];
		$('spclittle').innerHTML = '<br>';
		var spcval = $('spclittle').innerHTML;
		spc.each(function(item){
			$('spclittle').innerHTML = $('spclittle').innerHTML + '<span style="padding-left:15px;">'+item+'</span><br>';
		});
		$('spclittle').innerHTML = $('spclittle').innerHTML + '<strong id="total" style="padding-left:15px;">TOTAL: &pound;'+gt+'&nbsp;</strong><br /><a href="shopping-cart" class="menuitem">Go to shopping cart</a>';
} }

function UpdateCart(quant,product) {
	var seethis = new RegExp("[1-9]","g");
	if (seethis.test(quant)) {
		var url = 'includes/js-updatecart.php?product='+product+'&q='+quant;
		if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try {	xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } }
		if (xhr) {
			xhr.onreadystatechange = returnUpdateCart;
			xhr.open("GET", url, true);
			xhr.send(null);
		}
		else {
			alert("Sorry, but I couldn't create an XMLHttpRequest");
		}
	}
}
function returnUpdateCart() { if (xhr.readyState == 4) { if (xhr.status == 200) { var outMsg = xhr.responseText; } else { alert("There was a problem with the request " + xhr.status); }
		if (outMsg != "err") {
			var res = outMsg.split("|");
			$('Rtotal'+res[2]).innerHTML = res[0];
			$('grandtotal').innerHTML = res[1];
			if (res[3] > 0 || res[3] != "") {
			} else {
				$('scart').style.display='none';
				$('noprod').style.display='';
			}
		}
} }

function remP(prodid) {
	var askme = confirm("Delete from Shopping Cart?");
	if (askme) {
		$('scart').deleteRow(prodid.parentNode.parentNode.rowIndex);
		var url = 'includes/js-updatecart.php?product='+prodid.id+'&q=0';
		if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try {	xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } }
		if (xhr) {
			xhr.onreadystatechange = returnUpdateCartA;
			xhr.open("GET", url, true);
			xhr.send(null);
		}
		else {
			alert("Sorry, but I couldn't create an XMLHttpRequest");
		}
	}

}
function returnUpdateCartA() { if (xhr.readyState == 4) { if (xhr.status == 200) { var outMsg = xhr.responseText; } else { alert("There was a problem with the request " + xhr.status); }
		if (outMsg != "err") {
			var res = outMsg.split("|");
			if (res[1] == "") { res[1] = '0'; }
			$('grandtotal').innerHTML = res[1];
			if (res[3] == "0" || res[3] == "") {
				/* $('shopper').innerHTML = ''; */
				$('scart').style.display='none';
				$('noprod').style.display='';
			} else {
				/* $('shopper').innerHTML = '('+res[3]+')'; */
			}
		}
} }

function redosearch() {
	var changeS = false;
	if ($('searchtxt').value=='' && !changeS) {
		$('searchtxt').value='search furniture';
		changeS = true;
	}
	if ($('searchtxt').value=='search furniture' && !changeS) {
		$('searchtxt').value='';
		changeS = true;
	}
}

function UpdateCheckout(sel) {
	if (sel == "1") {
		$('button1').style.display = 'none';
		$('button2').style.display = '';
		//$('payingform').action='/prepay';
		$('todo').value='paypal';
	} else {
		$('button1').style.display = '';
		$('button2').style.display = 'none';
		//$('payingform').action='/thankyou';
		$('todo').value='sendorder';
	}
}

function ValidCheckout() {
	var key = 0;
	var testing = true;
	var ids = new Array(
						"name",
						"address",
						"zip",
						"city",
						"email"
						);
	var msg = new Array(
						"Please fill in your name",
						"Please fill in your address",
						"Invalid zip code",
						"Please fill in your city",
						"Invalid email address"
						);
	var tst = new Array(
						"txt",
						"adr",
						"adr",
						"txt",
						"eml"
						);
	var matchReg = "";
	var textReg = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var addrReg = /^[a-zA-Z0-9 \,\.\-]*$/;
	var phoneReg = /^[0-9 ]*$/;
	var emailReg = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var msgRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	ids.each(function(item){
		if (testing) {
			var txtval = $(''+item).value;
			switch (tst[key]) {
				case "txt": matchReg = textReg; break;
				case "adr": matchReg = addrReg; break;
				case "pho": matchReg = phoneReg; break;
				case "eml": matchReg = emailReg; break;
				case "msg": matchReg = msgRegex; break;
			}
			if (!txtval.match(matchReg) || txtval == "") {
				formalert(msg[key],4);
				testing = false;
			}
			key++;
		}
	});
	return testing;
}
function ContactValidation() {
	var key = 0;
	var testing = true;
	var ids = new Array(
						"name",
						"subject",
						"email",
						"phone",
						"validation"
						);
	var msg = new Array(
						"Please fill in your name",
						"Subject can not be empty",
						"Invalid email address",
						"Please fill in your phone",
						"Validation can not be empty"
						);
	var tst = new Array(
						"txt",
						"adr",
						"eml",
						"pho",
						"adr"
						);
	var matchReg = "";
	var textReg = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var addrReg = /^[a-zA-Z0-9 \,\.\-\?]*$/;
	var phoneReg = /^[0-9 \+\-]*$/;
	var emailReg = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var msgRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	ids.each(function(item){
		if (testing) {
			var txtval = $(''+item).value;
			switch (tst[key]) {
				case "txt": matchReg = textReg; break;
				case "adr": matchReg = addrReg; break;
				case "pho": matchReg = phoneReg; break;
				case "eml": matchReg = emailReg; break;
				case "msg": matchReg = msgRegex; break;
			}
			if (!txtval.match(matchReg) || txtval == "") {
				formalert(msg[key],4);
				testing = false;
			}
			key++;
		}
	});
	return testing;
}

function paypal_field(name, id, cssClass, parentElID, FieldVal) {
	/*
	try {
		newInput = document.createElement('<INPUT name="'+name+'">');
	} catch(e) {
		newInput = document.createElement('INPUT');
		newInput.name = name;
	}
	newInput.type = 'hidden';
	newInput.id = id;
	newInput.className = cssClass;
	newInput.value = FieldVal;	
	$(''+parentElID).appendChild(newInput);
	*/
	$(''+parentElID).innerHTML += '<input type="hidden" name="'+name+'" id="'+id+'" value="'+FieldVal+'" />';
}