//Check in stock bafore add to cart.
function addToCartOnSubmit()
{
	var form = document.productForm;
	var digit_filter =/^([0-9])+$/;
	var stock_status = form.cur_stock_status;
	var stock = form.check_stock;
	var qty = form.quantity;
	
	if(stock_status.value == 'Out Of Stock') {
		alert("This product currently out of stock.");
	} else if(parseInt(qty.value) == 0 || qty.value == '') {
		alert("No quantity amount entered.");
		qty.focus();
	} else if(digit_filter.test(qty.value) == false) {
		alert("Quatity, Only 0-9 are allowed.");
		qty.focus();		
	} else if(stock_status.value == 'In Stock'){
		if(parseInt(qty.value) > parseInt(stock.value)) {
			var word = (parseInt(stock.value) <= 1) ? "This product has <stock value> remaining in stock." : "This product has <stock value> remaining in stocks.";
			alert(word.replace(/\<stock value\>/g,stock.value)); 
			qty.focus();
		} else {
			return true;	
		}
	} else {
		return true;	
	}
	
	return false;
}

//Check quantity to order bafore add to cart.
function addPromoToCartOnSubmit()
{
	var form = document.productForm;
	var digit_filter =/^([0-9])+$/;
	var stock = form.min_ord;
	var qty = form.spec_product_qty;
	var currentStock = form.spec_product_qty_current;
	var currentStockStatus = form.spec_product_qty_current_status;
	var digit_check = true;
	
	var tmp_qty = 0;
	if(qty.length > 1) {
		for(var i=0;i<qty.length;i++) {
			qty[i].value = qty[i].value.replace(/^\s+|\s+$/g, '') ;
			if(digit_filter.test(qty[i].value) == false && qty[i].value != '') {
				digit_check = false;
				break;
			}
			else if(parseInt(qty[i].value) > 0) {
				tmp_qty = (tmp_qty + parseInt(qty[i].value));
				if(currentStockStatus[i].value == 'In Stock') {
					if(parseInt(currentStock[i].value) < parseInt(qty[i].value)) {
						var word = (parseInt(currentStock[i].value) <= 1) ? "This product has <stock value> remaining in stock." : "This product has <stock value> remaining in stocks.";
						alert(word.replace(/\<stock value\>/g,currentStock[i].value)); 
						return false;
					}
				}
			}
		}
	}
	else {
		tmp_qty = qty.value;
		if(currentStockStatus.value == 'In Stock') {
			if(parseInt(currentStock.value) < parseInt(qty.value)) {
				var word = (parseInt(currentStock.value) <= 1) ? "This product has <stock value> remaining in stock." : "This product has <stock value> remaining in stocks.";
				alert(word.replace(/\<stock value\>/g,currentStock.value)); 
				return false;
			}
		}
	}
	
	if(digit_check == false) {
		alert("Quatity, Only 0-9 are allowed.");
	} else if(parseInt(stock.value) > tmp_qty) {
		if(parseInt(stock.value) > 1) {
			var msg = "Minimum <stock value> items to add.";
		} else { 
			var msg = "Minimum <stock value> item to add.";
		}
		
		alert(msg.replace(/\<stock value\>/g,stock.value));
	} else {
		return true;	
	}
	
	return false;
}

//Re-calculate item price.
function cartOnSubmit()
{
	var form = document.cartForm;
	var digit_filter =/^([0-9])+$/;
	var qty = form.quantity;
	var stock = form.cur_stock;
	var productId = form.product_id;
	var stock_status = form.cur_stock_status;
	var pass = true;

	if(stock.length > 0)
	{
		for(var no=0;no<stock.length;no++){
			if(parseInt(qty[no].value) == 0 || qty[no].value == '') {
				alert("No quantity amount entered.");
				qty[no].focus();
				pass = false;	
				break;
			} else if(digit_filter.test(qty[no].value) == false) {
				alert("Quatity, Only 0-9 are allowed.");
				qty[no].focus();
				pass = false;	
				break; 	
			} else if(stock_status[no].value == 'Out Of Stock') {
				alert("This product currently out of stock.");
				qty[no].focus();
				pass = false;	
				break; 	
			} else {
				var tmp_qty = 0;
				for(var i=0;i<stock.length;i++) {
					if(productId[i].value == productId[no].value) {
						tmp_qty = (parseInt(tmp_qty) + parseInt(qty[i].value)); 
					}
				}
				
				if(parseInt(tmp_qty) > parseInt(stock[no].value) && stock_status[no].value == 'In Stock') {
					var word = (parseInt(stock[no].value) <= 1) ? "This product has <stock value> remaining in stock." : "This product has <stock value> remaining in stocks.";
					alert(word.replace(/\<stock value\>/g,stock[no].value));
					qty[no].focus();
					pass = false;	
					break;
				}
			}
		}
	}
	else
	{
		if(parseInt(qty.value) == 0 || qty.value == '') {
			alert("No quantity amount entered.");
			qty.focus();
			pass = false;
		} else if(digit_filter.test(qty.value) == false) {
				alert("Quatity, Only 0-9 are allowed.");
				qty.focus();
				pass = false;	
		} else if(parseInt(qty.value) > parseInt(stock.value) && stock_status.value == 'In Stock') {
			var word = (parseInt(stock.value) <= 1) ? "This product has <stock value> remaining in stock." : "This product has <stock value> remaining in stocks.";
			alert(word.replace(/\<stock value\>/g,stock.value));
			qty.focus();
			pass = false;	
		}
	}
	
	if(pass == false) { return false; } else { return true; } 
}

//Delete added product. 
function deleteOrder(box1, box2)
{
	var agree=confirm("Are you sure you want to complete this action?");
	if (agree)
	{
		document.getElementById('cancelBtn').value = 'yes';
		document.getElementById(box1).style.display = 'none';
		document.getElementById(box2).innerHTML = '';
		document.forms["cartForm"].submit();	
		return true ;
	}
	else { return false ; } 
}

//Checkout.
function checkout() {
	var subTotal = document.getElementById('subTotal');
	var pass  = cartOnSubmit();
	var form = document.cartForm;
	if(pass == true) {
		if(subTotal.innerHTML == '0.00') {
			alert("No item in shopping cart.");
		} else {
			form.page.value = 'cart/index/checkout/';
			form.submit();
// 			top.location.href= base_url_https + 'checkout/';	
		}
	}
}

//Request state data (malaysia only).
function requestStateData(box) 
{
	if(box == 'b_state_box') {
		var num = document.getElementById('b_country').value
		new ajax(url + 'checkout/state/b/' + num + '/', {update: $(box)});
	} else if(box == 's_state_box') {
		var num = document.getElementById('s_country').value
		new ajax(url + 'checkout/state/s/' + num + '/', {update: $(box)});
	}
}

//Submit discount code.
function addDiscount()
{
	var form = document.paymentForm;
	
    var formDiscount = form.discount_code.value.replace(/^\s+|\s+$/g, '');
    
    if(formDiscount.length > 0)
    {
	    var formVars = 'discount_code=' + formDiscount;
// 		new ajax(url + 'checkout/discount/', {postBody: formVars, method: 'post', update: $('content')});
		var url2 = url + 'checkout/discount/';
		var ajax = new Request({url:url2, onSuccess: function(jsonObj) {
			$('content').innerHTML = jsonObj;
			var url3 = url + 'checkout/discount_applied_msg/';
			var ajax2 = new Request({url:url3, onSuccess: function(jsonObj2) {
				$('content_discount').innerHTML = jsonObj2;
			}});
			ajax2.send(formVars);
		}});
		ajax.send(formVars);
		
		form.discount_code.value = '';
	}
	else {
		alert("Please enter discount code.");
	}
	
	
	
	
	
	
	
// 	var form = document.checkoutForm;
// 	
//     var formDiscount = form.discount_code.value.replace(/^\s+|\s+$/g, '');
//     var formpassword = form.discount_password.value.replace(/^\s+|\s+$/g, '');
//     
//     if(formDiscount.length > 0) {
// 	    var formVars = 'discount_code=' + formDiscount + '&distcount_password=' + formpassword;
// 		
// 		
// 		form.discount_code.value = '';
// 		form.discount_password.value = '';
// 	} else {
// 		alert('');
// 	}
	
	
	
	
	
	
	
	
	
}

//Change shipping rate. 
function shippingRate(shipping,handling)
{
	var form = document.paymentForm;
	
	var formShippingRate = shipping;
    var formHandlingFee = handling;	
  
    var formVars = 'shipping_rate=' + formShippingRate + '&handling_fee=' + formHandlingFee;
	new ajax(url + 'checkout/shippingRate/', {postBody: formVars, method: 'post', update: $('content')});
}

//Shipping form validation.
function shippingSubmit()
{
	var form = document.forms["paymentForm"];
	var email_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var digit_filter =/^([0-9])+$/;
	
	if (form.b_name.value == "") {
		alert( "Please enter billing name." );
		form.b_name.focus();	
	} else if (form.b_address1.value == "") {
		alert( "Please enter billing address." );
		form.b_address1.focus();
// 	} else if (form.b_city.value == "") {
// 		alert( "Please enter billing city." );
// 		form.b_city.focus();
	} else if (form.b_state.value == "") {
		alert( "Please enter billing state." );
		form.b_state.focus();	
	} else if (form.b_postcode.value == "" || digit_filter.test(form.b_postcode.value) == false) {
		alert( "Please enter billing postal code." );
		form.b_postcode.focus();
	} else if (form.b_telephone.value == "") {
		alert( "Please enter billing telephone." );
		form.b_telephone.focus();
	} else if (form.b_email.value == "" || email_filter.test(form.b_email.value) == false) {
		alert( "Please enter a valid email address." );
		form.b_email.focus();		
	} else if (form.destination_on.checked == true) {
		if (form.s_name.value == "") {
			alert( "Please enter recipient name." );
			form.s_name.focus();
		} else if (form.s_address1.value == "") {
			alert( "Please enter shipping address." );
			form.s_address1.focus();
// 		} else if (form.s_city.value == "") {
// 			alert( "Please enter shipping city." );
// 			form.s_city.focus();
		} else if (form.s_state.value == "") {
			alert( "Please enter shipping state." );
			form.s_state.focus();
		} else if (form.s_postcode.value == "" || digit_filter.test(form.s_postcode.value) == false) {
			alert( "Please enter shipping postal code." );
			form.s_postcode.focus();
		} else if (form.s_telephone.value == "") {
			alert( "Please enter shipping telephone." );
			form.s_telephone.focus();
		} else {
			return true;
		}
	} else {
		return true;
	}
	
	return false;
}

//Billing form validation.
function billingSubmit()
{
	var form = document.forms["paymentForm"];
	var digit_filter =/^([0-9])+$/;
	var curdate = new Date();
	var month = (curdate.getMonth() + 1);
	var year = curdate.getFullYear() + '';
	var check = true;
	var cheque_check = false; /*Cheque*/
	var maybank_check = false; /*Maybank2u*/
	
	var tmp = form.payment_type;
	for(var i=0;i<tmp.length;i++) {
		if(tmp[i].checked) {
			if(tmp[i].value != 'credit_card_type') { 
				check = false; 
				if(tmp[i].value == 'cheque_type') {
					var cheque_check = true;
				}
				else if(tmp[i].value == 'maybank2u') {
					var maybank_check = true;
				}
			} 
		}
	}
	
	var tmp_exp_month = form.exp_month.value;
	if(tmp_exp_month.substr(0,1) == 0)
		tmp_exp_month = tmp_exp_month.substr(1,1);
	
	if(form.shipping_validation.value <= 0) {	
		alert( "Do not have any custom shipping option." );
	} else if(check == true) {
		if (digit_filter.test(form.cc_num_1.value) == false || form.cc_num_1.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_1.focus();
		} else if (digit_filter.test(form.cc_num_2.value) == false || form.cc_num_2.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_2.focus();
		} else if (digit_filter.test(form.cc_num_3.value) == false || form.cc_num_3.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_3.focus();
		} else if (digit_filter.test(form.cc_num_4.value) == false || form.cc_num_4.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_4.focus();	
		} else if (form.name_on_card.value == "") {
			alert( "Please enter name on credit / debit card." );
			form.name_on_card.focus();					
		} else if (form.bank_name_on_card.value == "") {
			alert( "Please enter bank name on credit / debit card." );
			form.bank_name_on_card.focus();					
		} else if (parseInt(tmp_exp_month) < month && form.exp_year.value == year.substr(2,4)) {
			alert( "Sorry, your credit / debit card has expired." );			
		} else if (form.cvv.value == "") {
			alert( "Please enter CVV number." );
			form.cvv.focus();
		} else if (digit_filter.test(form.cvv.value) == false) {
			alert( "Please enter a valid CVV number. Only 0-9 are allowed." );
			form.cvv.focus();					
		} else {
			return true;
		}
	} else if(cheque_check == true) {
		if(form.cheque_num.value == '') {
			alert("Please enter the cheque number.");
			form.cheque_num.focus();
		} else {
			return true;
		}
	} else if(maybank_check == true) {
		if(form.transaction_num.value == '') {
			alert("Please enter the transaction number.");
			form.transaction_num.focus();
		}else {
			return true;
		}
	}
	else
	{
		return true;
	}
	
	return false;
}


//Register form validation.
function registerSubmit()
{
	var form = document.forms["paymentForm"];
	var digit_filter =/^([0-9])+$/;
	var curdate = new Date();
	var month = (curdate.getMonth() + 1);
	var year = curdate.getFullYear() + '';
	var check = true;
	var cheque_check = false; /*Cheque*/
	var maybank_check = false; /*Maybank2u*/
	
	var tmp = form.payment_type;
	for(var i=0;i<tmp.length;i++) {
		if(tmp[i].checked) {
			if(tmp[i].value != 'credit_card_type') { 
				check = false; 
				if(tmp[i].value == 'cheque_type') {
					var cheque_check = true;
				}
				else if(tmp[i].value == 'maybank2u') {
					var maybank_check = true;
				}
			} 
		}
	}
	
	var tmp_exp_month = form.exp_month.value;
	if(tmp_exp_month.substr(0,1) == 0)
		tmp_exp_month = tmp_exp_month.substr(1,1);
	
	if(check == true) {
		if (digit_filter.test(form.cc_num_1.value) == false || form.cc_num_1.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_1.focus();
		} else if (digit_filter.test(form.cc_num_2.value) == false || form.cc_num_2.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_2.focus();
		} else if (digit_filter.test(form.cc_num_3.value) == false || form.cc_num_3.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_3.focus();
		} else if (digit_filter.test(form.cc_num_4.value) == false || form.cc_num_4.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_4.focus();	
		} else if (form.name_on_card.value == "") {
			alert( "Please enter name on credit / debit card." );
			form.name_on_card.focus();					
		} else if (form.bank_name_on_card.value == "") {
			alert( "Please enter bank name on credit / debit card." );
			form.bank_name_on_card.focus();					
		} else if (parseInt(tmp_exp_month) < month && form.exp_year.value == year.substr(2,4)) {
			alert( "Sorry, your credit / debit card has expired." );			
		} else if (form.cvv.value == "") {
			alert( "Please enter CVV number." );
			form.cvv.focus();
		} else if (digit_filter.test(form.cvv.value) == false) {
			alert( "Please enter a valid CVV number. Only 0-9 are allowed." );
			form.cvv.focus();					
		} else {
			return true;
		}
	} else if(cheque_check == true) {
		if(form.cheque_num.value == '') {
			alert("Please enter the cheque number.");
			form.cheque_num.focus();
		} else {
			return true;
		}
	} else if(maybank_check == true) {
		if(form.transaction_num.value == '') {
			alert("Please enter the transaction number.");
			form.transaction_num.focus();
		}else {
			return true;
		}
	}
	else
	{
		return true;
	}
	
	return false;
}


//Renewal form validation.
function renewalSubmit()
{
	var form = document.forms["paymentForm"];
	var digit_filter =/^([0-9])+$/;
	var curdate = new Date();
	var month = (curdate.getMonth() + 1);
	var year = curdate.getFullYear() + '';
	var check = true;
	var cheque_check = false; /*Cheque*/
	var maybank_check = false; /*Maybank2u*/
	
	var tmp = form.payment_type;
	for(var i=0;i<tmp.length;i++) {
		if(tmp[i].checked) {
			if(tmp[i].value != 'credit_card_type') { 
				check = false; 
				if(tmp[i].value == 'cheque_type') {
					var cheque_check = true;
				}
				else if(tmp[i].value == 'maybank2u') {
					var maybank_check = true;
				}
			} 
		}
	}
	
	var tmp_exp_month = form.exp_month.value;
	if(tmp_exp_month.substr(0,1) == 0)
		tmp_exp_month = tmp_exp_month.substr(1,1);
	
	if(check == true) {
		if (digit_filter.test(form.cc_num_1.value) == false || form.cc_num_1.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_1.focus();
		} else if (digit_filter.test(form.cc_num_2.value) == false || form.cc_num_2.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_2.focus();
		} else if (digit_filter.test(form.cc_num_3.value) == false || form.cc_num_3.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_3.focus();
		} else if (digit_filter.test(form.cc_num_4.value) == false || form.cc_num_4.value.length != 4) {
			alert( "Please enter a valid credit / debit card number. Only 0-9 are allowed." );
			form.cc_num_4.focus();	
		} else if (form.name_on_card.value == "") {
			alert( "Please enter name on credit / debit card." );
			form.name_on_card.focus();					
		} else if (form.bank_name_on_card.value == "") {
			alert( "Please enter bank name on credit / debit card." );
			form.bank_name_on_card.focus();					
		} else if (parseInt(tmp_exp_month) < month && form.exp_year.value == year.substr(2,4)) {
			alert( "Sorry, your credit / debit card has expired." );			
		} else if (form.cvv.value == "") {
			alert( "Please enter CVV number." );
			form.cvv.focus();
		} else if (digit_filter.test(form.cvv.value) == false) {
			alert( "Please enter a valid CVV number. Only 0-9 are allowed." );
			form.cvv.focus();					
		} else {
			return true;
		}
	} else if(cheque_check == true) {
		if(form.cheque_num.value == '') {
			alert("Please enter the cheque number.");
			form.cheque_num.focus();
		} else {
			return true;
		}
	} else if(maybank_check == true) {
		if(form.transaction_num.value == '') {
			alert("Please enter the transaction number.");
			form.transaction_num.focus();
		}else {
			return true;
		}
	}
	else
	{
		return true;
	}
	
	return false;
}

function remDiv(num) {
	mainDIV[num].parentNode.removeChild(mainDIV[num]);
}

var mainDIV = new Array();
var contentDiv = new Array();
var mainDIVCount = 0;

//Quick purchase button.
function quickAddBtn(num,o_type,stock,obj) {
	var stock_status = document.getElementById(num).value;
	if(stock_status == 0) {
		alert("This product currently out of stock.");
		return false;
	}
	
	curObj = obj;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	mainDIV[mainDIVCount] = document.createElement("DIV");
	mainDIV[mainDIVCount].style.position="absolute";
	mainDIV[mainDIVCount].style.cssFloat="left";
	mainDIV[mainDIVCount].style.textAlign="right";
	mainDIV[mainDIVCount].style.padding=2+"px";
	mainDIV[mainDIVCount].style.border="1px solid gray";
	mainDIV[mainDIVCount].style.backgroundColor="white";
	mainDIV[mainDIVCount].style.zIndex=99+mainDIVCount;
	mainDIV[mainDIVCount].setAttribute("class", "clear");
	
	var continueDiv = document.createElement("DIV");
	continueDiv.style.cssFloat="left";
	continueDiv.style.textAlign="left";
	continueDiv.style.marginRight=20+"px";
	
	contentDiv[mainDIVCount] = document.createElement("DIV");
	contentDiv[mainDIVCount].style.cssFloat="left";
	contentDiv[mainDIVCount].style.textAlign="left";
	
	var img = document.createElement("IMG");
	img.setAttribute("src", "/images/ecommerce/ajax_loader.gif");
	var br = document.createElement("br");
	br.setAttribute("class", "clear");
	var br2 = document.createElement("br");
	br2.setAttribute("class", "clear");
	var br3 = document.createElement("br");
	br3.setAttribute("class", "clear");
	var br4 = document.createElement("br");
	br4.setAttribute("class", "clear");
	
	continueDiv.innerHTML = '<a href="/cart/">Go To Cart</a> | <a href="#" onclick="remDiv(\''+mainDIVCount+'\');return false">Continue Shopping</a>';
	
	contentDiv[mainDIVCount].appendChild(img);
	contentDiv[mainDIVCount].appendChild(document.createTextNode("Loading..."));
	mainDIV[mainDIVCount].innerHTML = '<a href="#" onclick="remDiv(\''+mainDIVCount+'\');return false">Close</a>';
	mainDIV[mainDIVCount].appendChild(br);mainDIV[mainDIVCount].appendChild(br2);
	mainDIV[mainDIVCount].appendChild(contentDiv[mainDIVCount]);
	mainDIV[mainDIVCount].appendChild(br3);mainDIV[mainDIVCount].appendChild(br4);
	mainDIV[mainDIVCount].appendChild(continueDiv);
	curObj.parentNode.appendChild(mainDIV[mainDIVCount]);
	addToCartAjax(mainDIVCount,num,o_type,stock);
	mainDIVCount++;
	return false;
	
// 	var stock_status = document.getElementById(num).value;
// 	
// 	if(stock_status == 0) {
// 		alert("This product currently out of stock.");
// 	}
// 	else {
// 		var tmp = num.split("_");
// 		var lhref = location.href;
// 		location.href = base_url + "buy/quickadd/" + tmp[1] + "/" + o_type + "/" + stock + "/" + stock_status + "/" + (lhref.substr(0, lhref.indexOf(base_url)) + lhref.substr(lhref.indexOf(base_url) + base_url.length));
// 	}
}

function addToCartAjax(divNum,num,o_type,stock) {
	if(window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var tmp = num.split("_");
	var param = "product_id="+tmp[1]+"&productType="+o_type;
	xmlhttp.open("POST", base_url + "buy/ajax_add/", false);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", param.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(param);
	contentDiv[divNum].innerHTML = xmlhttp.responseText;
	//Update Cart Counter
	xmlhttp.open("GET", base_url + "buy/cart_counter/", false);
	xmlhttp.send(param);
	document.getElementById('cart_counter').innerHTML = xmlhttp.responseText;

	xmlhttp.open("GET", base_url + "cart/get_cart_summary", false);
	xmlhttp.send(param);
	document.getElementById('cart_summary').innerHTML = xmlhttp.responseText;
}

function php_urlencode(str) {
	str = escape(str);
	return str.replace(/[*+\/@]|%20/g, function (s) {switch (s) {case "*" : s = "%2A";break;case "+" : s = "%2B";break;case "/" : s = "%2F";break;case "@" : s = "%40";break;case "%20" : s = "+";break;} return s;});
}

//Search introducer name.
function searchSponsor()
{
	var formVars = 'sponsor_code=' + document.getElementById('sponsor_code').value;
	new ajax(url + 'login/search/', {postBody: formVars, method: 'post', update: $('content')});
}

//having introducer code or not
function intorducer_code(num) {
	
	var form = document.guest_shopper;
	form.sponsor_code.disabled = false;
	document.getElementById('introduc').style.display="block";
	form.search3.style.display="block";
	if (num == '1') {
		document.getElementById('introduc').style.display="none";
		form.sponsor_code.disabled = true;
		form.search3.style.display="none";
	}
}

//having introducer code or not
function shopper_code() {
	
	var form = document.guest_shopper;
	
	for(var i = 0; i < form.guest.length; i ++) {
		if(form.guest[i].checked == true) {
			num = form.guest[i].value;
		}
	}
	form.conpassword.disabled = false;
	document.getElementById('guestfield').style.display="block";
	document.getElementById('guestfield1').style.display="none";
	if (num == '1') {
		document.getElementById('guestfield').style.display="none";
		document.getElementById('guestfield1').style.display="block";
		form.conpassword.disabled = true;
	}
}


//Continue As A Guest Shopper
function continueGuestShopper() {
	var form = document.guest_shopper;
	var email_filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(email_filter.test(form.email.value) == false && email_filter.test(form.email_new.value) == false ) {
		alert("Please enter a valid email address.");
		return false;
	} else if(form.passwd.value == '' && form.password_new.value == '') {
		alert("Please enter the password.");
		return false;
	} 
	
	for(var i = 0; i < form.guest.length; i ++) {
		if(form.guest[i].value == 2 && form.guest[i].checked == true && (form.conpassword.value == '' || form.conpassword.value != form.password_new.value)) {
			alert('Password and confirm password do not match. Please try again.');
			form.conpassword.value = '';
			form.password_new.value = '';
			form.password_new.focus();
			return false;
		}
	}
	
	/*if(form.sponsor_code.disabled == true) {
		return true;
	}
	else {
		if (form.sponsor_code.value == '') {
			alert("Please enter the introducer code.");
			form.sponsor_code.focus();
		}
		else {
			if(document.getElementById("intro")) {
				var msg = "The introducer you enter is : \n\nIntroducer Code : <introducer code> \nIntroducer Name : <introducer name>\n\n";
				msg = msg.replace(/\<introducer code\>/g, form.sponsor_code.value);
				msg = msg.replace(/\<introducer name\>/g, document.getElementById("intro").innerHTML);
				var agree = confirm(msg);
				if(agree == true) {
					return true;
				}
			}
			else {
				frames['sc_iframe'].location.href = url + 'login/search/' + form.sponsor_code.value + '/';
			}
		}
		
	}
	return false;*/
	return true;
}

// Add Product By
function add_by(num) {
	if(num == 2) {
		document.getElementById('addBysku').style.display = '';
		document.getElementById('addByname').style.display = 'none';
	}
	else {
		document.getElementById('addBysku').style.display = 'none';
		document.getElementById('addByname').style.display = '';
	}
	
}

// Order History Popup

var popWindow;
function popUpHistory(orderId) {
	
	if(popWindow!=null && !popWindow.closed){ popWindow.close(); }
	popWindow = window.open(base_url + 'account/node/shippingDetail/' + orderId + '/','shipping_pop','width=700,height=500,resizable=yes, scrollbars=yes,top=0, left=0');
	popWindow.focus();
	
}

function popUpHistory2() {
	
	var form = document.getElementById('orderDetailForm');
	
	if(popWindow!=null && !popWindow.closed){ popWindow.close(); }
	popWindow = window.open('','shipping_pop','width=700,height=500,resizable=yes, scrollbars=yes,top=0, left=0');
	form.submit();
	popWindow.focus();
	
}

function popUpRetrieve() {
	
	if(popWindow!=null && !popWindow.closed){ popWindow.close(); }
	popWindow = window.open(url + 'checkout/retrieve/', 'Retrieve','width=800,height=500,resizable=yes, scrollbars=yes,top=0, left=0');
	popWindow.focus();
	
}

function add_cb_optional(ele){
	var agree = false;
	if(ele.value > 0){
		agree = confirm("Comfirm update promotion product to cart.");
	}
	else{
		agree = confirm("Comfirm remove ordered promotion product to cart.");
	}
	
	
	if(agree){
		document.forms["cartForm"].submit();	
		return true ;
	}
	return false;
	
}
function add_sp_optional(ele){
	var agree = false;
	if(ele.checked == true){
		var sparr = ele.value.split("_");
		arr = document.getElementsByName('sp_opt_product[]');
		
		check = 0;
		for(i=0; i<arr.length; i++ ){
			splitval =arr[i].value.split("_");
			
			if((splitval[0] == sparr[0]) && arr[i].checked == true){
				check++;
			}  
			
		}
		
		if(check > 1){
			alert("Only 1 optional product of same special promotion can be choose.")
			ele.checked = false;
			return false;
			
		}
		
		agree = confirm("Comfirm add promotion product to cart.");
	}
	else{
		agree = confirm("Comfirm delete promotion product to cart.");
	}
	if(agree){
		document.forms["cartForm"].submit();	
		return true ;
	}
	return false;
	
}








