var j = xcls.extend({
	addedProducts : new Array(),
	checkUsername : function(str, withAjax) {
		if (str.length == 0) return;
		if (str.length > 3) {
			if (!withAjax) return true;
			var aj = new ajax("lib/ajax.php?checkForUsername="+str);
			aj.call();
		} else {
			_$('userNameResult').style.display=''; 
			_$('userNameResultTD').innerHTML="<font color='red'>! Username can't be less than 4 characters</font>";
			return false;
		}
	},
	checkPassword : function(str, withAjax) {
		if ((withAjax) && (str.length == 0)) return false;
		if (str.length > 3) {
			_$('passResult').style.display='none';
			return true;
		} else {
			_$('passResult').style.display=''; 
			_$('passResultTD').innerHTML="<font color='red'>! Password can't be less than 4 characters</font>";
			return false;
		}
	},
	checkPasswordAgain : function(str, withAjax) {
		if ((withAjax) && (str.length == 0)) return false;
		if (str == document.forms['registerForm'].elements['password'].value) {
			_$('passAgainResult').style.display='none';
			return true;
		} else {
			_$('passAgainResult').style.display=''; 
			_$('passAgainResultTD').innerHTML="<font color='red'>! Your passwords do not match</font>";
			return false;
		}
	},
	checkThisMail : function(str, withAjax) {
		if ((withAjax) && (str.length == 0)) return false;
		if (valid.isEmail(str)) {
			if (!withAjax) {
				_$('emailResult').style.display='none';
				return true;
			}
			var aj = new ajax("lib/ajax.php?checkForEmail="+str);
			aj.call();
		} else {
			_$('emailResult').style.display=''; 
			_$('emailResultTD').innerHTML="<font color='red'>! Your email is not valid.</font>";
			return false;
		}
	},
	checkAll : function() {
		var frm = document.forms["registerForm"];
		if ((Page.checkUsername(frm["username"].value, false)) && (Page.checkPassword(frm['password'].value, false)) && (Page.checkPasswordAgain(frm['passwordConfirm'].value, false)) && (Page.checkThisMail(frm['email'].value, false))) {
			return true;
		}
		return false;
	},
	checkoutCheck : function() {
		var frm = document.forms["paypal"].elements;
		if (((frm["realname"].value.length) < 3) || ((frm["city"].value.length) < 2) || ((frm["address1"].value.length) < 3) || ((frm["phoneNumber"].value.length) < 3)) {
			alert("You have to fill your name, phone number, city and address information in order to complete the transaction");
			return;
		}
		frm["payer_name"].value = frm["realname"].value;
		if ((Page.checkUsername(frm["username"].value, false)) && (Page.checkThisMail(frm['email'].value, false))) {
			var aj = new ajax('lib/ajax.php'); 
			aj.formTag('paypal'); 
			aj.call(); 
			this.disabled=true; this.value='Please Wait...'
		}
	},
	contactCheck : function() {
		var frm = document.forms["contactForm"];
		if (!valid.isEmail(frm["email"].value)) {
			alert("Please enter a valid e-mail address");
			return false;
		}
		if ((frm['name'].value.length < 4) || (frm['message'].value.length<4) || (frm['securityCode'].value.length != 4)) {
			alert("Please fill all the form elements appropirately");
			return false;
		}
		return true;
	},
	
	transactionComplete : true,
	userLoggedIN : false,
	
	cardOptions : function(mode, id) {
		if (!Page.transactionComplete) return;
		Page.transactionComplete = false;
		var quantity = (arguments.length > 2) ? arguments[2] : 1;
		if (quantity == 0) {
			Page.transactionComplete = true;
			alert("This product is not in stock");
			return;
		}
		var aj = new ajax('lib/ajax.php?shoppingCard=1&'+mode+'='+id+'&quantity='+quantity);
		aj.formMethod = 'get';
		aj.write('shoppingCard');
	},
	
	addToCartMultiple : function() {
		var frm = document.forms["accessories"].elements;
		var selectedProducts = new Array();
		for (var i=0; i<frm.length; i++) {
			if ((frm[i].name.indexOf('prod_')!=-1) && (frm[i].checked)) {
				selectedProducts.push(['prod', frm[i].name.replace('prod_', '')]);
			}
		}
		for (var i=0; i<frm.length; i++) {
			if ((frm[i].name=="shipping") && (frm[i].checked)) {
				selectedProducts.push(['shipping', frm[i].value]);
			}
		}
		if (selectedProducts.join('|').indexOf('prod')==-1) {
			alert("You Haven't selected any product...");
			return;
		}
		var aj = new ajax("lib/ajax.php?shoppingCard=1&addMultiple="+selectedProducts.join('|'));
		aj.formMethod="get";
		aj.write("shoppingCard");
	},
	
	decreaseQuantity : function(objName) {
		var frm = null;
		try {
			frm = document.forms['accessories'].elements;
		} catch (e) { frm = document.forms['paypal'].elements; }
		var obj = frm[objName];
		if (parseInt(obj.value,10) == 0) return;
		if (parseInt(obj.value-1,10)<1) obj.value = 1; else obj.value = parseInt(obj.value)-1;
		Page.quantityChanged();
	},
	
	increaseQuantity : function(objName) {
		var frm = null;
		try {
			frm = document.forms['accessories'].elements;
		} catch (e) { frm = document.forms['paypal'].elements; }
		var obj = frm[objName];
		var newValue = parseInt(obj.value,10)+1;
		var pID = objName.replace('quantity_','');
		var stock = parseInt(frm['instock_'+pID].value,10);
		var rem = 0;
		for (var j=0; j<Page.addedProducts.length; j++) {
			if (Page.addedProducts[j][0] == pID) {
				var rem = parseInt(Page.addedProducts[j][1],10);
			}
		}
		if (newValue+rem > stock) return;
		obj.value = parseInt(obj.value)+1;
		Page.quantityChanged();
	},
	
	adjustQuantity : function() {
		try {
			var frm = null;
			try {
				frm = document.forms['accessories'].elements;
			} catch (e) { frm = document.forms['paypal'].elements; }
			for (var i=0; i<frm.length; i++) {
				if (frm[i].name.indexOf('quantity_')!=-1) {
					var pID = frm[i].name.replace('quantity_', '');
					var stock = parseInt(frm["instock_"+frm[i].name.replace('quantity_', '')].value,10);
					for (var j=0; j<Page.addedProducts.length; j++) {
						if (Page.addedProducts[j][0] == pID) {
							var rem = parseInt(stock -Page.addedProducts[j][1],10);
							frm[i].value = (rem < 1) ? 0 : 1;
						}
					}
				}
			}
		} catch (e) {}
	},
	
	quantityChanged : function() {
		var frm = null;
		try {
			frm = document.forms['accessories'].elements;
		} catch (e) {
			var totalPrice = 0;
			frm = document.forms['paypal'].elements; 
			for (var i=0; i<frm.length; i++) {
				if (frm[i].name.indexOf("actualPrice_")!=-1) {
					var prdID = parseInt(frm[i].name.replace('actualPrice_',''),10);
					var quantity = parseInt(frm["quantity_"+prdID].value,10);
					var price = (parseFloat(frm[i].value)*quantity).toFixed(2);
					document.getElementById('displayPrice_'+prdID).innerHTML =(price)+" USD";
					totalPrice = totalPrice + parseFloat(price);
				}
			}
			frm["amount"].value = totalPrice.toFixed(2);
			try {
				var shPrice = frm["shippy"].options[frm["shippy"].selectedIndex].text.split("-")[1].replace(" USD", "").replace(",",".");
				shPrice = parseFloat(shPrice);
				totalPrice += shPrice;
				frm["shipping"].value = shPrice.toFixed(2);
			} catch (e) {
				frm["shipping"].value = 0;
			}
			frm["totalPrice"].value = totalPrice.toFixed(2);
		}
	},
	
	RowClick : function(obj) {
		alert(obj.bgColor);
	},
	
	LocateMe : function(url) {
		var aj = new ajax(url);
		aj.write("productDetails");
	}
});

var Page = new j();

function _$(id) {
	return document.getElementById(id);
}
