function doproductsearchbykeyword(keyword){
	var ajaxurl = '../ajax/productsearch.asp?type=keyword&keyword='+keyword;
	$("#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 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,optionjs,upselljs){
	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( quantity == 0 ||  isNaN(quantity) || (quantity.indexOf(".")!=-1) || (quantity.indexOf("-")!=-1) ){
		alert("Please make sure you input a correct quantity number!");
		return;
	}
	
	arrupselljs = upselljs.split("@");
	var arrupsells = "";
	for(var i=0;i<arrupselljs.length-1;i++){
		upsell = $("#"+arrupselljs[i]);
		if( upsell.attr('checked') == true ){
			upsellqty =  $("#"+arrupselljs[i] + "-QTY").val();
			if( upsellqty == 0 ||  isNaN(upsellqty) || (upsellqty.indexOf(".")!=-1) || (upsellqty.indexOf("-")!=-1) ){
				alert("Please select a quantity you require for related products");
				return;
			}else{
				upsellproductid =  arrupselljs[i].substring(  arrupselljs[i].lastIndexOf("-")+1 );
				arrupsells = arrupsells + upsellproductid +"*"+upsellqty + "@";
			}
		}
	}
	
	var ajaxurl = 'http://shop.jukebox45s.co.uk/ajax/shopcart.asp?type=add&productid='+productID+'&quantity='+quantity+'&arroptions='+arroptions+'&arrupsells='+arrupsells;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//alert(result);
			//UpdateCartTotals();
			//alert('Product added to cart');
			document.location = 'http://shop.jukebox45s.co.uk/cart/basket.asp'
			return;
		},
								
		error: function(result)
		{
			alert(result.responseText);
			return;
		}
	});
}

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/shopcart.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/shopcart.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/shopcart.asp?type=showtotalsview';
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			document.getElementById("quickshopbaskettotal").innerHTML = result;
			//updateCommission();
		},
								
		error: function(result)
		{
			//alert(result.responseText);
		}
	});
}

function updatecartdiscount(orderid){
	var discountCode = document.getElementById("discountCode").value;
	var ajaxurl = '../ajax/shopcart.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{
				document.location.reload();
			}
		},
								
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}


function removeItemFromCart(orderid,productID){
	var ajaxurl = '../ajax/shopcart.asp?type=removeproduct&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/shopcart.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/shopcart.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/shopcart.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 selectAgentByTradeCustomerID(TradeCustomerID){
		if(TradeCustomerID=="NULL"){
			$('#pk_agentID').attr('value','NULL');
			$('#span_hidden_othercustomer').hide();
			$('#span_hidden_otheragent').hide();
			$('#span_hidden_customer').show();
			$('#span_hidden_agent').show();
			var ajaxurl = '../ajax/shopcart.asp?type=cleanpricesession';
			$.ajax({
				type:"get",
				url:ajaxurl,
				success: function(result)
				{
					updateCommission();
				},					
				error: function(result)
				{
					alert(result.responseText);
				}
			}); 
			$('#commissions').hide();
			return;
		}
		var customerid = $('#input_hidden_customerid').val();
		$('#span_hidden_othercustomer').show();
		$('#span_hidden_otheragent').show();
		$('#span_hidden_customer').hide();
		$('#span_hidden_agent').hide();
		
		//alert(TradeCustomerID);
		var ajaxurl = '../ajax/shopcart.asp?type=selectagentbytradecustomerid&tradecustomerid='+TradeCustomerID+'&customerid='+customerid;
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				//alert(result);
				$('#commissions').show();
				arrresult = result.split("@");
				$('#pk_agentID').attr('value',arrresult[0]);
				$('#span_tradecustomerinfo').attr('innerHTML',arrresult[1]);
				$('#span_agentinfo').attr('innerHTML',arrresult[2]);
				updateCommission();
			},					
			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 updateCommission(){
	var TradeCustomerID = $('#pk_tradeCustomerID').val();
		if( TradeCustomerID=="NULL"  ){
			//alert("Not trade so no commision")
			return;	
		}
	var agentID = $('#pk_agentID').val();
	var ajaxurl = '../ajax/shopcart.asp?type=updatecommission&tradecustomerid='+TradeCustomerID+'&agentid='+agentID;
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				var oldcustomerinfo = $('#span_tradecustomerinfo').attr('innerHTML');
				var oldagentinfo  = $('#span_agentinfo').attr('innerHTML');
				$('#commissions').attr('innerHTML',result);
				$('#span_tradecustomerinfo').attr('innerHTML',oldcustomerinfo);
				$('#span_agentinfo').attr('innerHTML',oldagentinfo);				
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		}); 	
}

//insert Commision log by ajax
function InsertCommisionLog(){
		var TradeCustomerID = $('#pk_tradeCustomerID').val();
		if( TradeCustomerID=="NULL"  ){
			return;
		}
		var agentID = $('#pk_agentID').val();
		var ajaxurl = '../ajax/shopcart.asp?type=insertcommsion&tradecustomerid='+TradeCustomerID+'&agentid='+agentID;
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				//alert(result);				
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		}); 	
}

function GetShippingFeeByShippingMethod(){
	var pk_shippingID = $('#select_shippingmethod').val();
	if(  pk_shippingID=="NULL" ){
		alert("Please select one shipping method!")
		cleanShippingFee();
		return;
	}
	var ajaxurl = '../ajax/shopcart.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/shopcart.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/shopcart.asp?type=cleanshippingfee';
		$.ajax({
			type:"get",
			url:ajaxurl,
			success: function(result)
			{
				//UpdateCartTotals();
			},					
			error: function(result)
			{
				alert(result.responseText);
			}
		});
}

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



function ajaxCheckEmailExist(emailaddress){
	var ajaxurl = "../ajax/shopcart.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 savewarehousechange(select_warehouse_id,pickingID){
	if( $('#'+select_warehouse_id).val() == "NULL" ){
		alert('Please select a warehouse');
		return;
	}
	var warehouseid = $('#'+select_warehouse_id).val() ;
	var ajaxurl = "../ajax/shopcart.asp?type=changewarehouse&pickingID=" + pickingID + "&warehouseid="+ warehouseid;
	$.ajax({
		type:"get",
		url:ajaxurl,
		success: function(result)
		{
			//alert(result);
			if( result == "wrong" ){
				alert("There is not enough stock available at this warehouse. Select another.");
			}else{
				var selctedwarehousename = $('#'+select_warehouse_id).find('option:selected').text(); 
				$('#td_warehousename'+pickingID).attr('innerHTML',selctedwarehousename);
				alert("Dispatching warehouse has been changed.");
			}	
		},					
		error: function(result)
		{
			alert(result.responseText);
		}
	});
}

function CheckRegisterForm(){
	 document.MM_returnValue = false;
	  if( document.MM_returnValue ){
		var emailaddress = $('#fld_email').val();
	 	var ajaxurl = "../ajax/shopcart.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/shopcart.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/shopcart.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 = "http://www.powerprogolf.com/ajax/shopcart.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);   
}   
  

