function productQuickEditSites(productSiteID){
	var ajaxurl = '/ajax/ajaxAdminFunctions.asp?type=updatesiteproduct&productid='+productSiteID;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//alert(result.responseText);
		},
								
		error: function(result)
		{
			alert(result.responseText);
			alert("There was an error updating this product");
		}
	});
}

function imageDefault(pk_imageID,pk_productID){
	var ajaxurl = '/ajax/ajaxAdminFunctions.asp?type=imagedefault&pk_productID='+pk_productID+'&pk_imageID='+pk_imageID;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//alert(result.responseText);
			if( result=="toomany" ){
				alert("You must unselect the current default image before selecting another.");
				var imageID = "img-" + pk_imageID;
				document.getElementById(imageID).checked = false;
				return;
			}
			if( result=="ok" ){
				alert("Default image changed successfully.");
				return;
			}
		},
								
		error: function(result)
		{
			alert(result.responseText);
			alert("There was an error updating this product");
		}
	});
}

function checkProductAddForm(){
	var sku = document.getElementById("fld_sku").value;
	var fld_title = document.getElementById("fld_title").value;
	var fld_price = document.getElementById("fld_price").value;
	var fld_listPrice = document.getElementById("fld_listPrice").value;
	var fld_costPrice = document.getElementById("fld_costPrice").value;
	var fld_minOrder = document.getElementById("fld_minOrder").value;
	var fld_wholesalePrice = document.getElementById("fld_wholesalePrice").value;
	var fld_weight = document.getElementById("fld_wholesalePrice").value;
	var fld_stock = document.getElementById("fld_stock").value;	
	var allowDecimal = $('#allowDecimal').val(); //document.getElementById("allowDecimal").value;	
	if( allowDecimal == "0" && fld_stock.indexOf(".")>0){
		alert("You can not enter a fractional stock quantity without first selecting ‘allow fractional quantities’ to equal yes");
		$('#to-do').unblock(); 
		return;
	}

	if( allowDecimal == "0" && fld_minOrder.indexOf(".")>0){
		alert("You can not enter a fractional minimum order quantity without first selecting ‘allow fractional quantities’ to equal yes");
		$('#to-do').unblock(); 
		return;
	}
	
	var ajaxurl = '/ajax/ajaxAdminFunctions.asp?type=checkaddproduct&sku='+sku;
	$("#to-do").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
	});
	
	if (sku==""){
			alert("You must enter a SKU for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (fld_title==""){
			alert("You must enter a title for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_price)){
			alert("You must enter a price for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_listPrice)){
			alert("You must enter a list price (RRP) for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_costPrice)){
			alert("You must enter a cost price for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_wholesalePrice)){
			alert("You must enter a wholesale price for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_minOrder)){
			alert("You must enter a minimum order value for this product.");
			$('#to-do').unblock(); 
			return;
	}
	if (isNaN(fld_weight)){
			alert("You must enter a weight value for this product.");
			$('#to-do').unblock(); 
			return;
	}

	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			if( result=="used-sku" ){
				alert("The SKU you entered is already in use. Please select another SKU.");
				$('#to-do').unblock();
				return;
			}
			else if( result=="ok-sku" ){
				document.forms.form1.onsubmit();
				valForm();
				if(document.MM_returnValue) {
					document.form1.submit();
				}
			}
		},
								
		error: function(result)
		{
			$('#to-do').unblock(); 
			alert("Error: " + result.responseText);
		}
	});
}


function checkProductEditForm(pk_productID){
	var sku = document.getElementById("fld_sku").value;
	var fld_title = document.getElementById("fld_title").value;
	var fld_price = document.getElementById("fld_price").value;
	var fld_listPrice = document.getElementById("fld_listPrice").value;
	var fld_costPrice = document.getElementById("fld_costPrice").value;
	var fld_minOrder = document.getElementById("fld_minOrder").value;
	var fld_wholesalePrice = document.getElementById("fld_wholesalePrice").value;
	var fld_weight = document.getElementById("fld_weight").value;
	var fld_stock = document.getElementById("fld_stock").value;	
	var allowDecimal = $('#allowDecimal').val(); //document.getElementById("allowDecimal").value;	
	if( allowDecimal == "0" && fld_stock.indexOf(".")>0){
		alert("You can not enter a fractional stock quantity without first selecting ‘allow fractional quantities’ to equal yes");
		$('#to-do').unblock(); 
		return;
	}
	
	if( allowDecimal == "0" && fld_minOrder.indexOf(".")>0){
		alert("You can not enter a fractional minimum order quantity without first selecting ‘allow fractional quantities’ to equal yes");
		$('#to-do').unblock(); 
		return;
	}
	
	var ajaxurl = '/ajax/ajaxAdminFunctions.asp?pk_productID='+pk_productID+'&type=checkeditproduct&sku='+sku;
	$("#to-do").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
	});
	
	if (sku==""){
			alert("You must enter a SKU for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (fld_title==""){
			alert("You must enter a title for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_price)){
			alert("You must enter a price for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_listPrice)){
			alert("You must enter a list price (RRP) for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_costPrice)){
			alert("You must enter a cost price for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_wholesalePrice)){
			alert("You must enter a wholesale price for this product.");
			$('#to-do').unblock(); 
			return;
	}
	
	if (isNaN(fld_minOrder)){
			alert("You must enter a minimum order value for this product.");
			$('#to-do').unblock(); 
			return;
	}
	if (isNaN(fld_weight)){
			alert("You must enter a weight value for this product.");
			$('#to-do').unblock(); 
			return;
	}

	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			if( result=="used-sku" ){
				alert("The SKU you entered is already in use. Please select another SKU.");
				$('#to-do').unblock();
				return;
			}
			else if( result=="ok-sku" ){
				document.forms.form1.onsubmit();
				valForm();
				if(document.MM_returnValue) {
					document.form1.submit();
				}
			}
		},
								
		error: function(result)
		{
			$('#to-do').unblock(); 
			alert("Error: " + result.responseText);
		}
	});
}

function CheckCategory(productID){
	var categoryID = document.getElementById("pk_categoryID").value;
	var fld_sortOrder = document.getElementById("fld_sortOrder").value;
	var ajaxurl = '/ajax/ajaxAdminFunctions.asp?fld_sortOrder='+fld_sortOrder+'&productID='+productID+'&categoryID='+categoryID+'&type=checkcategory';
	//alert(ajaxurl)
	$("#to-do").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
	});
	
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			if( result=="root-category" ){
				alert("You can't select a category which has sub categories. Please select another category.");
				$('#to-do').unblock();
				return;
			}
			else if( result=="end-category" ){
				document.getElementById("addcategory").value = "addcategory"
				document.form1.submit()
			}
			else if( result=="used-category" ){
				alert("This product is already assigned to that category. Please select another category.");
				$('#to-do').unblock();
				return;
			}
		},
								
		error: function(result)
		{
			$('#to-do').unblock(); 
			alert("Error: " + result.responseText);
			return;
		}
	});
}

function doproductsearchbysku(sku){
	var ajaxurl = '../ajax/productsearch.asp?type=sku&keyword='+sku;
	$("#searchtable").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
	}); 
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			$('#searchtable').unblock(); 
			document.getElementById("tablesearchresult").innerHTML = result;
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function doproductsearchbycategory(categoryid){
	var ajaxurl = '../ajax/productsearch.asp?type=category&keyword='+categoryid;
	$("#searchtable").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
	}); 
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			$('#searchtable').unblock(); 
			document.getElementById("tablesearchresult").innerHTML = result;
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function AddProductToCart(productID,quantity,allowDecimal, fld_stock, fld_minOrder){
	if( quantity == 0 ||  isNaN(quantity) || ( allowDecimal?false:(quantity.indexOf(".")!=-1) ) || (quantity.indexOf("-")!=-1) ){
		alert("The quantity you entered was incorrect. Please try again!");
		return;
	}
	if( quantity > fld_stock){
		alert("You have entered a quantity value higher than the amount of stock we have for this product. Please lower your quantity or call us for additional quantity purchases");
		return;
	}
	if( quantity < fld_minOrder){
		alert("You have entered a quantity value lower than our minimum order amount of " + fld_minOrder + ". Please amend and try again.");
		return;
	}
	var ajaxurl = '/ajax/ajaxStoreFunctions.asp?type=add&productid='+productID+'&quantity='+quantity;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			document.getElementById("carttotalstopright").innerHTML = result;
			document.getElementById('addtocartsuccess').style.display = '';
			return;
		},
								
		error: function(result)
		{
			alert(result.responseText);
			return;
		}
	});
}

function remindCustomer(pk_customerID){
	var ajaxurl = '/ajax/ajaxAdminFunctions.asp?type=remindcustomerpassword&pk_customerID='+pk_customerID;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			alert("Email password reminder has been sent to customer.");
		},
								
		error: function(result)
		{
			alert("Failed to send reminder");
		}
	});
}

function updateCart(orderid, orderrowID,quantity,optionjs){
	arroptionid = optionjs.split("@");
	var arroptions = "";
	for(var i=0;i<arroptionid.length-1;i++){
		arroptions = arroptions +  $("#"+arroptionid[i]).val() + "@";
		if( $("#"+arroptionid[i]).attr('needed')=="True" && $("#"+arroptionid[i]).val()=="NULL" ){
			alert("Please make sure you select all required options!");
			return;
		}
	}
	if( isNaN(quantity) || (quantity.indexOf(".")!=-1) || (quantity.indexOf("-")!=-1) ){
		alert("Please make sure you input the right quantity number!");
		return;
	}
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=update&orderid='+orderid+'&orderrowid='+orderrowID+'&quantity='+quantity+'&arroptions='+arroptions;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			document.location.reload();
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function UpdateCartTotals(){
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=updatetotals';
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//document.getElementById("shopbaskettotal").innerHTML = result;
			//updateCommission();
		},
								
		error: function(result)
		{
			//alert(result.responseText);
		}
	});
}

function ShowCartTotalsView(){
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=showtotalsview';
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			document.getElementById("quickshopbaskettotal").innerHTML = result;
			//updateCommission();
		},
								
		error: function(result)
		{
			//alert(result.responseText);
		}
	});
}

function updateShopCart(orderid){
	var discountCode = document.getElementById("discountCode").value;
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=adddiscount&code='+discountCode+'&orderid='+orderid;

	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//document.getElementById("quickshopbaskettotal").innerHTML = result;
			if( result=="-1" ){
				alert("Your discount code is invalid or has expired");
			}
			else if( result=="-2" ){
				alert("Your discount code has already been used with this order, you can not use it again!");
			}
			else if( result=="-3" ){
				alert("Sorry you do not have the correct product in your shopping basket for this discount code!");
			}
			else if( result=="-4" ){
				alert("Sorry you can't use a gift voucher to purchase more gift vouchers!");
			}
			else if( result=="-5" ){
				alert("Your shopping cart total is less than your gift voucher amount, please increase your shopping cart total!");
			}
			else{
				document.location.reload();
			}
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}


function removeItemFromCart(orderid,productID,orderRowID){
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=removeproduct&orderRowID='+orderRowID+'&productid='+productID+'&orderid='+orderid;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//document.getElementById("quickshopbaskettotal").innerHTML = result;
			document.location.reload();
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function updateShippingAddress(){
	var pk_customerShippingID = document.getElementById("select_shippingaddress").value;
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=updateshippingaddress&pk_customerShippingID='+pk_customerShippingID ;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//alert(result.responseText+pk_customerShippingID);
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function discountchange(obj){
	if( obj.value != "NULL" || true){
		var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=adddiscount&discountid='+obj.value ;
		$("#discounttr").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
		}); 
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				$('#discounttr').unblock(); 
				//document.getElementById("tablesearchresult").innerHTML = result;
				UpdateCartTotals();
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
	}
}

function adjustmentupdate(){
	var adjustreason = document.getElementById("adjustreason").value;
	if( adjustmentamount > 0  && adjustreason=="" ){
		alert("Please make sure adjustment reason at least 1 character.");
		return;
	}
	var adjustmentamount = document.getElementById("adjustmentamount").value;
	if( isNaN(adjustmentamount) ){
		alert("Please make sure you input the right adjustment amount.");
		return;
	}
	var adjustmentatype = document.getElementById("adjustmentatype").value;
	if( adjustmentatype=="Subtract" ){
		adjustmentamount = 0 - adjustmentamount;
	}
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=adjustmentupdate&adjustmentamount='+adjustmentamount+'&adjustreason='+adjustreason ;
	$("#discounttr").block({ 
		message: '<img src="../../images/loadingAnimation.gif" />',
		css: { width: '208px',  border: '0px' } 
	});
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//alert('Here');
			$('#discounttr').unblock(); 
			UpdateCartTotals();

			//document.getElementById("tablesearchresult").innerHTML = result;
		},					
		error: function(result)
		{
			alert(result.responseText);
		}
	}); 	
}

function agentblock() {
	var TradeCustomerID = $('#pk_tradeCustomerID').val();
	var customerID = $('#input_hidden_customerid').val();
	if (customerID==TradeCustomerID) {
		$('#agentblock').block({message: ''}); 
		return;
	}	
}

function agentblocknochange() {
	$('#agentblock').block({message: ''}); 
	return;
}


function GetShippingFeeByShippingMethod(){
	var pk_shippingID = $('#select_shippingmethod').val();
	if(  pk_shippingID=="NULL" ){
		alert("Please select one shipping method!")
		cleanShippingFee();
		return;
	}
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=getshippingfee&pk_shippingID='+pk_shippingID;
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				if( result=="NULL"){ //do not find
					alert(result.responseText);
					//alert(pk_shippingID + "We can not find a shipping rate - please call for custom quote on shipping");
					cleanShippingFee();
				}		
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
}

function updateCustomMethodShipping(){
	var customerPrice = $('#hidden_input_shipping_amount').val();
	var customerNotes = $('#hidden_input_shipping_note').val();
	if ( isNumber(customerPrice,false)==false ){
		alert('Please input correct shipping price');
		return;
	}
	if(customerNotes==""){
		alert('Please input shipping note');
		return;
	}
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=updatecustomshippingfee&shippingprice='+customerPrice+"&shippingnote="+customerNotes;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			document.location.reload();
		},					
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function cleanShippingFee(){
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=cleanshippingfee';
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				//UpdateCartTotals();
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
}

function cleancommissionlog(){
	var ajaxurl = '../ajax/ajaxStoreFunctions.asp?type=cleanshippingfee';
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				UpdateCartTotals();				
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
}



function ajaxCheckEmailExist(emailaddress){
	var ajaxurl = "../ajax/ajaxStoreFunctions.asp?type=checkemailexist&emailaddress=" + emailaddress;
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				//exist
				if( result=='true' ){ 
					$('#error_emailexist').show();
				} else{
					$('#error_emailexist').hide();
				}		
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
}


function CheckRegisterForm(){
	 document.MM_returnValue = false;
	  if( document.MM_returnValue ){
		var emailaddress = $('#fld_email').val();
	 	var ajaxurl = "../ajax/ajaxStoreFunctions.asp?type=checkemailexist&emailaddress=" + emailaddress;
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				//exist
				if( result=='true' ){ 
					alert("Email address already registered – please use an alternative email address or login to your existing account");
					$('#fld_email').focus();
				}	else {
					document.MM_returnValue = true;
					}
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
	 }
}

function getCountryByProvice( proviceid,countryHTMLID){
	if( proviceid=="NULL" ){
		alert('Please select a provice');
		return;
	}
	var ajaxurl = "../ajax/ajaxStoreFunctions.asp?type=getcountrybyprovice&proviceid=" + proviceid;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			if( result!='false' ){ 
				$('#'+countryHTMLID).val(result);
			} 	
		},					
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function getProvinceListByCountryID( countryid,provinceHTMLID, nameOfField ){
	if( countryid=="NULL" ){
		alert('Please select a country');
		return;
	}
	var ajaxurl = "/ajax/ajaxStoreFunctions.asp?type=getprovinceListbycountryid&nameOfField="+nameOfField+"&countryid=" + countryid;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			if( result!='false' ){ 
				//$('#'+provinceHTMLID).val(result);
				//alert(result);
				$('#'+provinceHTMLID ).html(result);
			} 	
		},					
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function getShippingMethodByAddress(customershippingid){
	if( customershippingid=="NULL" ){
		alert('Please select a shipping address');
		return;
	}
	var ajaxurl = "/ajaxStoreFunctions.asp?type=getshippingmethodbyaddress&customershippingid=" + customershippingid;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			$('#div_shippingmethod').html(result); 	
		},					
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function isInt(val,negative)   
    {   
      var   re;   
      if(negative)   
      {   
            re   =   /^-?\d+$/;   
      }   
      else   
      {   
          re   =   /^\d+$/;   
      }   
      return   re.test(val);   
}   
function   isNumber(val,negative)   
{   
    var   re;   
    if(negative)   
    {   
          re   =   /^-?\d+(.\d+)?$/;   
    }   
    else   
    {   
          re   =   /^\d+(.\d+)?$/;   
    }
    return   re.test(val);   
}   
  

