
var global_sess="";

var str_Load_Products;
	str_Load_Products="";
	
	global_sess='2*1*1';

var tot_cart_items=0;
var linebreak = "<br />";


var my_shopping_cart=new Array(10);
for (i=0; i <10; i++)
//my_shopping_cart[i]=new Array(3); ''SSS
my_shopping_cart[i]=new Array(5);
   

function build_array_from_global_var()
{
if (global_sess!="")
{
var product_data1=global_sess.split("|");
for (i=0; i <product_data1.length; i++)
my_shopping_cart[i]=product_data1[i].split("*");
tot_cart_items=product_data1.length;
}
else
tot_cart_items=0;
}

function list_product_data()
{
var str,i;
str="<table>";

for (i=0; i <3; i++)
{
str=str +  "<tr><td>" + product_item[i][1] + "...." + "<input type=button value=Buy  onclick='add_item(" + i.toString() + ",0,-1);'></input></td></tr>"  ;
}
str=str + "</table>";
product_list.innerHTML=str;
}

//function get_cart(l,price)
function get_cart(l,prod_name,price,prod_price)
{
//alert('m i on load');
var str,i;
var k9,amt=0;
//alert(price);
str="<table>";
if (tot_cart_items<=0 )
            str=str +  "<tr><td colspan=4>Cart is empty</td></tr>"  ;
else
{
            for (i=0; i <tot_cart_items; i++)
            if (my_shopping_cart[i][2]=="1") 
            {
                    k9=parseInt(my_shopping_cart[i][0]);//item id
                    //alert(my_shopping_cart[i][3]);
                    if (my_shopping_cart[i][3] == -1)//dnt override rice in DB
                    {
                       // str=str +  "<tr><td valign=top>" + my_shopping_cart[i][1]   + " x </td><td width='130'>" +   product_item[k9][1] + " £" +product_item[k9][4] + "</td><td> <a style='cursor: hand' class='red12bold' onclick='remove_item(" + i.toString() + ");'>X</a></td></tr>"  ;
                        str=str +  "<tr><td valign=top>" + my_shopping_cart[i][1]   + " x </td><td width='130'>" + my_shopping_cart[i][4]+"<font color='red'>  "+ my_shopping_cart[i][3] +"</font></td><td> <a title='Remove from cart' style='cursor: pointer;cursor:hand;' class='red12bold' onclick='remove_item(" + i.toString() + ");'>X</a></td></tr>"  ;
                       //amt=amt+parseFloat(product_item[k9][4]) *parseFloat(my_shopping_cart[i][1]) ;
                      //alert(parseFloat(my_shopping_cart[i][1]) + 'M here?');
                      //alert(my_shopping_cart[i][5] + "*" + my_shopping_cart[i][1]);
                      amt=amt+parseFloat(my_shopping_cart[i][5]) *parseFloat(my_shopping_cart[i][1]) ;
                       
                       
                    }
                    else //override price in DB- use sent price
                    {
                        str=str +  "<tr><td valign=top>" + my_shopping_cart[i][1]   + " x </td><td width='130'>" + my_shopping_cart[i][4]+"<font color='red'>  "+ my_shopping_cart[i][3] +"</font></td></font><td> <a title='Remove from cart' style='cursor: pointer;cursor:hand;' class='red12bold' onclick='remove_item(" + i.toString() + ");'>X</a></td></tr>"  ;
                        //alert(my_shopping_cart[i][3]);
                       // amt=amt+parseFloat(my_shopping_cart[i][4]) *parseFloat(my_shopping_cart[i][1]) ;
                       amt=amt+parseFloat(my_shopping_cart[i][5]) *parseFloat(my_shopping_cart[i][1]) ;
                    }

            }

            //str=str +  "<tr><td colspan=4>____________________________</td></tr>"  ;
            //str=str +  "<tr><td colspan=4> tot_cart_itemsal Invoice value : " + roundNumber(amt).toString() + "</td></tr>"  ;
}

str=str + "</table>";

document.getElementById("cart").innerHTML=str;
document.getElementById("product_value").innerHTML= (Math.round(amt*100)/100).toString();
//alert('here');
build_global_var_from_array();
}

function roundNumber(inum) {
	var rnum = inum;
	var rlength = 2; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}


function build_global_var_from_array()
{
	if (tot_cart_items==0) 
		global_sess="";
	else
	for (i=0; i <tot_cart_items; i++)
	if (i==0)
		global_sess=my_shopping_cart[i][0] + "*" + my_shopping_cart[i][1] + "*" + my_shopping_cart[i][2] + "*" + my_shopping_cart[i][3] + "*" + my_shopping_cart[i][4] + "*" + my_shopping_cart[i][5];
	else
		global_sess=global_sess + "|" + my_shopping_cart[i][0] + "*" + my_shopping_cart[i][1] + "*" + my_shopping_cart[i][2] +"*" + my_shopping_cart[i][3] + "*" + my_shopping_cart[i][4] + "*" + my_shopping_cart[i][5];

//alert(global_sess);
post_data(global_sess);
}


function takeToAmazon(cartContents)
{

var objHTTP =null;

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  objHTTP=new XMLHttpRequest()
  }
// code for IE
else if (window.ActiveXObject)
  {
  objHTTP=new ActiveXObject("Microsoft.XMLHTTP")
  }

  

   
  
  var getData= "asp/create_amazon_cart.aspx?cart="+cartContents ; 
 
  objHTTP.open("POST",getData,false);
   
		try
		{
		objHTTP.send(null);
		}
		catch(e)
		{
	 
		return false;
		}
 alert(objHTTP.responseText );
 
}

function post_data(s)
{  

var objHTTP =null;

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  objHTTP=new XMLHttpRequest()
  }
// code for IE
else if (window.ActiveXObject)
  {
  objHTTP=new ActiveXObject("Microsoft.XMLHTTP")
  }

  // Set file path to update user information
  //var getData= "get_supplier_terms_n_conditions.asp?supplier_id="+supplier_id +"&frmCountry="+frmCountry+"&toCountry="+ toCountry+"&category="+category +"&weight="+ weight+"&width="+ width+"&length="+length+"&height="+height+ "" ;
  //alert(width);

   
  
  var getData= "/asp/post_sess_data.aspx?cart="+s ; 
//  alert(s);
  objHTTP.open("POST",getData,false);
  // send the IDispatch xmldom to server through IStream interface
		try
		{
		objHTTP.send(null);
		}
		catch(e)
		{
		//alert(e.toString());
		return false;
		}
		
		 

 
//alert(objHTTP.responseText );
}

function get_item_id(folder_name)
{

for (var i_item_cnt=0 ;(i_item_cnt <GLB_product_item_length); i_item_cnt++)			
	{
	    if (product_item[i_item_cnt][10] == (folder_name + ".asp") )
	    {
	    	return i_item_cnt;
	    	i_item_cnt=GLB_product_item_length;
	    }	
	 }   
}


function add_item(k,prod_name, l,price,prod_price)
//add_item(11,'prod name',1.99,
{
var WT_POSITIVE_NUMBER = /^[0-9]+\.*[0-9]*$/;
//alert(/^[1-9]+\.*[0-9]*$/.test(l.toString()));
if (/^[0-9]+\.*[0-9]*$/.test(l.toString())  && l > 0){
var exists_flag=0,exists_i=0;
alert('This item will now be added to your shopping cart');
//alert(tot_cart_items);
for (i=0; i <tot_cart_items; i++)
if (my_shopping_cart[i][0]==k) //check if item exists
{
    exists_flag=1;
    exists_i=i;
}


if (exists_flag==1) 
{

        my_shopping_cart[exists_i][1]=(parseInt(my_shopping_cart[exists_i][1]) +parseInt(l) ).toString();
        //alert(my_shopping_cart[exists_i][3]);
        if ((parseFloat(my_shopping_cart[exists_i][3]) > parseFloat(price)) && price != -1)
        {
	        my_shopping_cart[exists_i][3]=price;
        	
	        my_shopping_cart[exists_i][4]=prod_name;
	        my_shopping_cart[exists_i][5]=prod_price;
	        
        }

}

else
{
        my_shopping_cart[tot_cart_items][0]=k; //item
        my_shopping_cart[tot_cart_items][1]=l;//qty

        my_shopping_cart[tot_cart_items][2]="1";
        my_shopping_cart[tot_cart_items][3]=price;
        my_shopping_cart[tot_cart_items][4]=prod_name;        
        my_shopping_cart[tot_cart_items][5]=prod_price;       
        //alert('helo?');
        tot_cart_items=tot_cart_items+1;
}

get_cart(l,prod_name,price,prod_price);

}
else
{
alert('Please enter valid quantity');
}
}


function remove_item(k)
{
if (k<(tot_cart_items-1))
for(i=k;i<tot_cart_items;i++)
{
	my_shopping_cart[i][0]=my_shopping_cart[i+1][0];
	my_shopping_cart[i][1]=my_shopping_cart[i+1][1];
	my_shopping_cart[i][2]=my_shopping_cart[i+1][2];
}
tot_cart_items=tot_cart_items-1;
get_cart(-1,-1);
}

function remove_item_from_view_cart(k)
{
	if (k<(tot_cart_items-1))
for(i=k;i<tot_cart_items;i++)
{
	my_shopping_cart[i][0]=my_shopping_cart[i+1][0];
	my_shopping_cart[i][1]=my_shopping_cart[i+1][1];
	my_shopping_cart[i][2]=my_shopping_cart[i+1][2];
}
	tot_cart_items=tot_cart_items-1;
	get_view_cart();
}

function get_view_cart()
{

var str,i;
var k9;
var amt=0;

//alert(global_sess);
//str="<table>";
str=" <table width='96%' border='0' align='center' cellpadding='0' cellspacing='0' valign='top'>";

if (tot_cart_items<=0 )
	str=str +  "<tr><td colspan=4>Your shopping cart is empty</td></tr>"  ;
else
{
for (i=0; i <tot_cart_items; i++)
	if (my_shopping_cart[i][2]=="1")
	{
	k9=parseInt(my_shopping_cart[i][0]);

	if (i%2 != 0)
	{
		str=str + "<tr bgcolor='#EEFFAC'>";
	}
	else
	{
		str=str + "<tr>";
	}


	str=str + "<td width='17%' height='20' align='center'>" +my_shopping_cart[i][0]+ "</td>";//<img src='/ebay/product_information/a.gif' width='52' height='45' />";
	str=str +  "<td width='30%' class='black' valign='center' >&nbsp;&nbsp;&nbsp;" +my_shopping_cart[i][4]+ "</td>";
	//str=str +  "<td width='16%' class='black' align='center'>£" + product_item[k9][4] + " </td>" ;
	if (my_shopping_cart[i][3] != -1)
{
		str=str +  "<td width='16%' class='black' align='center'>" + my_shopping_cart[i][3] + " </td>" ;

}
else
{
str=str +  "<td width='16%' class='black' align='center'>" + my_shopping_cart[i][3] + " </td>" ;

}
	str=str +  "<td width='21%' class='black' align='center'>"+ my_shopping_cart[i][1] +"</td>" ; //qty
	str=str +  "<td width='21%' class='black'align='center' > <a title='Remove from cart' style='cursor: pointer;cursor:hand;' class='red12bold' onclick='remove_item_from_view_cart(" + i.toString() + ");'>X</a></td>" ;
	str=str + "</tr>"

	//amt=amt+parseFloat(product_item[k9][4]) * parseFloat(my_shopping_cart[i][1]) ;
	if (my_shopping_cart[i][3] != -1)
	{
	amt=amt+parseFloat(my_shopping_cart[i][5]) * parseFloat(my_shopping_cart[i][1]) ;
}
else
{


amt=amt+parseFloat(my_shopping_cart[i][5]) * parseFloat(my_shopping_cart[i][1]) ;

}


	}


}

str=str + "</table>";

document.getElementById("cart").innerHTML=str;

document.getElementById("divCartTotal").innerHTML='';

document.getElementById("divCartTotal").innerHTML=Math.round(amt*100)/100;


//document.getElementById("divFinalCheckoutTotal").innerHTML=+ roundNumber(Math.round(amt*100)/100 + Math.round(document.frmCheckOut.selShippingMethod.value*100)/100);
//document.getElementById("divFinalCheckoutTotal").innerHTML=roundNumber(parseFloat(document.getElementById("divFinalCheckoutTotal").innerHTML)-parseFloat(document.getElementById("divDiscount").innerHTML));
//parseFloat(document.frmCheckOut.selShippingMethod.value);


build_global_var_from_array();



}



function build_paypal_var()
{


 var k9,amt=0;
 var strPaypal;
 strPaypal='';

// strPaypal='<form name="frmCheckOut" action="https://www.paypal.com/uk/cgi-bin/webscr?__track=_ext-enter:p/xcl/pay/buy-index-blank_reg:_xclick-user-submit" method="post" >'

 strPaypal=strPaypal + '<input type="hidden" name="business" value="driving@123driving.co.uk">';
 strPaypal=strPaypal + '<input type="hidden" name="cmd" value="_cart">';
 strPaypal=strPaypal + '<input type="hidden" name="upload" value="1">';

 if (tot_cart_items> 0 )
 {
 for (i=0; i <tot_cart_items; i++)
 	if (my_shopping_cart[i][2]=="1")//qty > 0
 	{
 	k9=parseInt(my_shopping_cart[i][0]);

 	//Assign here
//alert('"item_number_' + my_shopping_cart[i][0].toString() +'"');
 	strPaypal=strPaypal + '<input type="hidden" name="item_name_' + (parseInt(i) +1).toString() +'" value="'+ my_shopping_cart[i][4] +'">';
	strPaypal=strPaypal + '<input type="hidden" name="item_number_' + (parseInt(i) +1).toString() +'" value="50'+my_shopping_cart[i][0].toString()+'">';

	//strPaypal=strPaypal + '<input type="hidden" name="item_number_' + (parseInt(i) +1).toString() +'" value="50'+(parseInt(i) +1).toString()+'">';

//	strPaypal=strPaypal + '<input type="hidden" name="amount_' + (parseInt(i) +1).toString() +'" value="'+ product_item[k9][4] +'">';
 	strPaypal=strPaypal + '<input type="hidden" name="quantity_' + (parseInt(i) +1).toString() +'" value="'+my_shopping_cart[i][1]+'">';

strPaypal=strPaypal + '<input type="hidden" name="amount_' + (parseInt(i) +1).toString() +'" value="'+ my_shopping_cart[i][5] +'">';

if (1==2)
{
if (my_shopping_cart[i][0].toString() == '55')

{
	strPaypal=strPaypal + '<input type="hidden" name="tax_' + (parseInt(i) +1).toString() +'" value="0">';
	strPaypal=strPaypal + '<input type="hidden" name="amount_' + (parseInt(i) +1).toString() +'" value="'+ my_shopping_cart[i][5] +'">';

}
else
{

VAT= parseFloat(my_shopping_cart[i][5])* parseFloat(0.15);
VAT2=Math.round(VAT*100)/100;
price=parseFloat(my_shopping_cart[i][5])-parseFloat(VAT2);

	strPaypal=strPaypal + '<input type="hidden" name="tax_' + (parseInt(i) +1).toString() +'" value="'+VAT2+'">';
	strPaypal=strPaypal + '<input type="hidden" name="amount_' + (parseInt(i) +1).toString() +'" value="'+ price +'">';

}
}

 	}

 	//strPaypal=strPaypal + '<input type="hidden" name="shipping_1" value="2.00">';

 	//strPaypal=strPaypal + '<input type="hidden" name="shipping_1" value='"+ document.frmCheckOut.selShippingMethod.value +"'>';
 //document.frmCheckOut.selShippingMethod.options[document.frmCheckOut.selShippingMethod.selectedIndex ].value
	//strPaypal=strPaypal + '<input type="hidden" name="return" value="http://www.123discountshop.com/asp/prod_payment_thank_you.asp">';
	//strPaypal=strPaypal + '<input type="hidden" name="cancel_return" value="http://www.theory-test.co.uk/asp/payment_cancel.asp">' ;

	strPaypal=strPaypal + '<input type="hidden" name="currency_code" value="GBP">';
	strPaypal=strPaypal + '<input type="hidden" name="return" value="http://www.123discountshop.co.uk/asp/prod_payment_thank_you.asp">';
	strPaypal=strPaypal + '<input type="hidden" name="cancel_return" value="http://www.123discountshop.com/asp/payment_cancel.asp">';


	//strPaypal=strPaypal + '</form>';

	//document.write (strPaypal);
	document.getElementById("divForm").innerHTML=strPaypal;
	//alert(document.frmCheckOut.selShippingMethod.value);

	 document.getElementById("divForm").innerHTML=document.getElementById("divForm").innerHTML + '<input type="hidden" name="shipping_1" value="'+document.frmCheckOut.selShippingMethod.value.toString()+'">';
//alert(strPaypal);
	document.frmCheckOut.submit();
 }
 else
 {
 	alert('Your shopping cart is empty');
 }


}

function onChangeUpdateCartTotal(selectObj)
{
	//alert('here');
//alert(roundNumber(parseFloat(document.frmCheckOut.selShippingMethod.value)));
	document.getElementById("divFinalCheckoutTotal").innerHTML= roundNumber(roundNumber(parseFloat(document.getElementById("divCartTotal").innerHTML)) + roundNumber(parseFloat(document.frmCheckOut.selShippingMethod.value)));
}

function add_item_and_checkout(k,l,price)
{ 

var exists_flag=0,exists_i=0;
alert('This item will now be added to your shopping cart');
for (i=0; i <tot_cart_items; i++)
if (my_shopping_cart[i][0]==k) //check if item exists
{
exists_flag=1;
exists_i=i;
}

if (exists_flag==1) 
{
my_shopping_cart[exists_i][1]=(parseInt(my_shopping_cart[exists_i][1]) +parseInt(l) ).toString();


if ((parseFloat(my_shopping_cart[exists_i][3]) > parseFloat(price)) && price != -1)
{

	my_shopping_cart[exists_i][3]=price;
}

}

else
{
my_shopping_cart[tot_cart_items][0]=k; //item
my_shopping_cart[tot_cart_items][1]=l;//qty

my_shopping_cart[tot_cart_items][2]="1";
my_shopping_cart[tot_cart_items][3]=price;
tot_cart_items=tot_cart_items+1;
}

//var str,i;
var k9,amt=0;

str="<table>";
if (tot_cart_items<=0 )
str=str +  "<tr><td colspan=4>Cart is empty</td></tr>"  ;
else
{
for (i=0; i <tot_cart_items; i++)
if (my_shopping_cart[i][2]=="1")
{
k9=parseInt(my_shopping_cart[i][0]);//item
//alert(k9);
if  (my_shopping_cart[i][3] == -1)
{
str=str +  "<tr><td valign=top>" + my_shopping_cart[i][1]   + " x </td><td width='130'>" +   product_item[k9][1] + " £" +product_item[k9][4] + "</td><td> <a style='cursor: hand' class='red12bold' onclick='remove_item(" + i.toString() + ");'>X</a></td></tr>"  ;
amt=amt+parseFloat(product_item[k9][4]) *parseFloat(my_shopping_cart[i][1]) ;
}
else
{
str=str +  "<tr><td valign=top>" + my_shopping_cart[i][1]   + " x </td><td width='130'>" +   product_item[k9][1] + " £" +parseFloat(my_shopping_cart[i][3]) + "</td><td> <a style='cursor: hand' class='red12bold' onclick='remove_item(" + i.toString() + ");'>X</a></td></tr>"  ;
amt=amt+parseFloat(my_shopping_cart[i][3]) *parseFloat(my_shopping_cart[i][1]) ;
}

}

//str=str +  "<tr><td colspan=4>____________________________</td></tr>"  ;
//str=str +  "<tr><td colspan=4> tot_cart_itemsal Invoice value : " + roundNumber(amt).toString() + "</td></tr>"  ;
}

str=str + "</table>";
//document.getElementById("cart").innerHTML=str;
//document.getElementById("product_value").innerHTML='£'+(Math.round(amt*100)/100).toString();
	if (tot_cart_items==0) 
		global_sess="";
	else
	for (i=0; i <tot_cart_items; i++)
	if (i==0)
		global_sess=my_shopping_cart[i][0] + "*" + my_shopping_cart[i][1] + "*" + my_shopping_cart[i][2] + "*" + my_shopping_cart[i][3] + "*" + my_shopping_cart[i][4] ;
	else
		global_sess=global_sess + "|" + my_shopping_cart[i][0] + "*" + my_shopping_cart[i][1] + "*" + my_shopping_cart[i][2] +"*" + my_shopping_cart[i][3] + "*" + my_shopping_cart[i][4] ;
//add_item("+i_item_cnt+",document.getElementById('txtQ" + product_item[i_item_cnt][0]+"').value,-1);
}
build_array_from_global_var();





