
function loadURL(url,div){
  var ajaxRequest = new XHConn();
  ajaxRequest.connect(url, "GET", null ,fnWhenDone, div);
}

function fnWhenDone(xmlhttp,div) {
  if (div!=null && xmlhttp.responseText.indexOf("HTTP Status")<0) {
    document.getElementById(div).innerHTML="&nbsp;";			
    document.getElementById(div).innerHTML=xmlhttp.responseText;
  }
}

function readParameter() {
	var htmlURL = document.URL;
	var htmlParams = htmlURL.substring(htmlURL.indexOf('?')+1,htmlURL.length);

	if (htmlParams.indexOf('#')!=-1)
    	htmlParams = htmlParams.slice(0,htmlParams.indexOf('#'));
		
    	var arrayParams = htmlParams.split('&');	
	
	for (var i=0;i<arrayParams.length;i++){
		var parameters = arrayParams[i].split('=');
		var parameterName = parameters[0];
		var parameterValue = parameters[1];
		if (document.getElementById(parameterName)!=null)
			document.getElementById(parameterName).value = parameterValue;
	}
}

function getElement(name){
    return document.getElementById(name);
}

function arrayReturnValue( arrayName ){
    var arrayValue = 0;
	
	for ( var cont=0; cont<arrayName.length; cont++ ){
	      arrayValue = parseInt(arrayName[cont]) + arrayValue;
	}
	return arrayValue;	 
}

function selectedRadioValue(radioName){
	var value;
 	var radio = document.getElementsByName(radioName);
	  for (var i=0;i<radio.length;i++){
	      if (radio[i].checked == true)
	          value = radio[i].value;
	  }
  	return value;
}

function popWindow( url, target, width, height ) {
	window.open( url, target, "toolbar=no, location=no, directories=no, status=2, menubar=no, scrollbars=yes, resizable=no, width="+width+", height="+height+"");		
}

function setCookieTable(){
   if ( compareCookie("productKey","quantity") == false ){
	  if ( getCookie("productKey") != "0"){
		   setCookie( "productKey", getCookie("productKey") + "," + getElement("productKey").value );
		   setCookie( "longName", getCookie("longName") + "," + getElement("longName").value );
    	   setCookie( "salePrice", getCookie("salePrice") + "," + getElement("salePrice").value );
    	   setCookie( "quantity", getCookie("quantity") + "," + getElement("quantitySelect").value );
	  }
	  else{
		   setCookie( "productKey" , getElement("productKey").value );
		   setCookie( "longName" , getElement("longName").value );
    	   setCookie( "salePrice", getElement("salePrice").value );
    	   setCookie( "quantity" , getElement("quantitySelect").value );
	  }
	}
	setCookie("subTotal", getSubTotal() );
	setCookie("taxi" , getTaxi() );
	setCookie("total", getTotal() );
	setHTMLshoppingHeader();
}

function initArrayByCookies(){
   	initHTMLelements();
	setProductKey  ( arrayCookie ("productKey") );
	setLongName ( arrayCookie ("longName"));
	setSalePrice( arrayCookie ("salePrice"));
	setQuantity ( arrayCookie ("quantity"));
	addRowsByCookies ();
}

function setCookiesAfterDelete(){
	setCookie ( "productKey"  , productKey);
	setCookie ( "longName" , longName);
	setCookie ( "salePrice", salePrice);
	setCookie ( "quantity" , quantity);
	setCookie ( "subTotal", getSubTotal() );
    setCookie ( "taxi" , getTaxi() );
    setCookie ( "total", getTotal() );
	setHTMLshoppingHeader();
}

function deleteCookiesTable() {
	deleteCookie ( "productKey");
	deleteCookie ( "longName");
	deleteCookie ( "salePrice");
	deleteCookie ( "quantity" );
	deleteCookie ( "subTotal" );
    deleteCookie ( "taxi"  );
    deleteCookie ( "total" );
	setHTMLshoppingHeader();
}

function initHTMLelements(){
 	setTableId("productTable");
	setSubTotalId("SubTotal");
	setTotalId ("Total");
	setTaxiId ("Taxi");
	setTotalQuantityId ("TotalQuantity");
}

function setHTMLshoppingHeader(){
	getElement("quantityTop").innerHTML = valueCookie("quantity");
	getElement("subTotalTop").innerHTML = "$&nbsp;"+ parseFloat( valueCookie("subTotal") ).toFixed(2);
	getElement("quantityBottom").innerHTML = valueCookie("quantity");
	getElement("subTotalBottom").innerHTML = "$&nbsp;"+ parseFloat( valueCookie("subTotal") ).toFixed(2);
}

function getCookiePaymenthMethod(){
    var paymentMethodKey = returnCookie( "paymenthMethodCookie", "1" );
	return paymentMethodKey;
}


var salePrice = new Array();
var quantity  = new Array();  
var longName  = new Array();
var productKey  = new Array();
var articles  = new Array();
var elements  = new Array();

var tableId;
var taxiId;
var subTotalId;
var totalId;
var totalQuantityId;

function setProductKey( productKey ){
	this.productKey = productKey;
}

function setLongName( longName ){
	this.longName = longName;
}

function setSalePrice( salePrice ){
	this.salePrice = salePrice;
}

function setQuantity( quantity ){
	this.quantity  = quantity;
}

function setTableId( tableId ){
    this.tableId = getElement ( tableId );    
}

function setTaxiId( taxiId ){
	this.taxiId = getElement ( taxiId );
}

function setSubTotalId( subTotalId ){
	this.subTotalId = getElement ( subTotalId );
}

function setTotalId( totalId ){
    this.totalId = getElement ( totalId );    
}

function setTotalQuantityId( totalQuantityId ){
	this.totalQuantityId = getElement ( totalQuantityId );
}

function getSubTotal(){
	var subTotal = 0;
    if ( returnCookie( "salePrice", false ) != false && returnCookie( "quantity", false ) != false  ){
	     var salePrice = arrayCookie( "salePrice");
		 var quantity  = arrayCookie( "quantity" );
		 for ( var cont=0; cont < salePrice.length; cont++ ){
		    if (parseFloat(salePrice[cont]) > 0) 
	          subTotal =  parseFloat(subTotal) + (parseFloat(salePrice[cont]) * parseFloat(quantity[cont]) );
	     }
		 return subTotal;
	}
	else
	     return subTotal;
}

function getTaxi(){
	var taxi = valueCookie("subTotal");
	if (parseFloat(taxi)>0 && getElement("taxAmount").value>0)
		return parseFloat(taxi) * getElement("taxAmount").value;
	else
		return 0;
}

function getTotal(){
	var total = parseFloat( valueCookie("subTotal") ) + parseFloat(valueCookie("taxi")) ;
	return total;
}

function createImage(){
	return "<a href='#' onclick='deleteRow(this.parentNode.parentNode.parentNode.rowIndex)'><img src='image/remove.gif' border='0' title='remover'/></a> ";
}

function deleteRow( position ){
  	changeRows( position );
	tableId.deleteRow( position );
	setCookiesAfterDelete();
	setHTMLsubTotal();
	setHTMLtaxi();
	setHTMLtotal();
	setHTMLquantity();
}
	
function changeRows( position ){
	var popFlag = productKey.length>1;
 	for ( var move=position-1; move < productKey.length ; move++ ){
          productKey   [ move ] = productKey   [ move+1 ];
          longName  [ move ] = longName  [ move+1 ];
		  salePrice [ move ] = salePrice [ move+1 ];
		  quantity  [ move ] = quantity  [ move+1 ];
    }
	if (popFlag) {
		productKey.pop();
		longName.pop();
		salePrice.pop();
		quantity.pop();
	}
	setTimeout("loadShoppingCart();",500);
}

function setHTMLsubTotal(){
	subTotalId.innerHTML = parseFloat(valueCookie("subTotal")).toFixed(2);
}

function setHTMLtaxi(){
	taxiId.innerHTML = parseFloat(valueCookie("taxi")).toFixed(2);
	getElement("tax_cart").value = parseFloat(currencyConverter(valueCookie("taxi"))).toFixed(2);
}

function setHTMLtotal(){
	totalId.innerHTML = parseFloat(valueCookie("total")).toFixed(2);
	getElement("defaultCurrencyLabel").innerHTML = getElement("defaultCurrencyKey").value;
	if (getElement("paypalTotal")!=null){
		getElement("paypalTotal").innerHTML = parseFloat(currencyConverter(valueCookie("total"))).toFixed(2);
		getElement("paypalCurrencyLabel").innerHTML = getElement("paypalCurrencyKey").value;
		getElement("paypalCurrencyMessageLabel").innerHTML = getElement("paypalCurrencyKey").value;
	}
}

function setHTMLquantity(){
	totalQuantityId.innerHTML = valueCookie("quantity");
}

function addRowsByCookies(){
   var x , y, cols;  
   quantityTotal = 0;

   for (var rows=0;rows<productKey.length;rows++){	
        cols = 0;
        x = tableId.insertRow( rows+1 ); 		
		do{ 
	    	y = x.insertCell(cols);
			y.innerHTML = addColumns( cols,rows );
			
			if (rows % 2) 
				y.className="tableInputPair";
			else
				y.className="tableInputEven";
				
			cols ++;
		} while(cols<6);
  	}
	setHTMLsubTotal();
	setHTMLtaxi();
	setHTMLtotal();
	setHTMLquantity();
}

function addColumns ( column, row ){
	var className = "tableInputEven";
	if (row % 2)	
		className = "tableInputPair";
	
	if (column==0){
      return '<span class="'+className+'">'+ productKey[row] + '</span>' ;		
    }
    else
	if (column==1){
	  return '<input type="hidden" name="item_name_'+(row+1)+'" value="'+longName[row]+'" readonly="readonly" />' +
	  		 '<span class="'+className+'">'+longName[row]+'</span>';
	}
	else
	if(column==2){
	  var rowPrice = 0;
	  if (salePrice[row] > 0)
	  	rowPrice = salePrice[row];
	  var paypalAmount = currencyConverter(rowPrice); 
	  return '<div style="text-align:right">' + 
	  		 '<input type="hidden" name="amount_'+(row+1)+'" value="'+(paypalAmount)+'" readonly="readonly" />' +	
	  		 '<span class="'+className+'">'+rowPrice+'</span>'
			 '</div>'; 
	}
	else
	if(column==3){
	  return '<div style="text-align:right">' + 
	  		 '<input type="hidden" name="quantity_'+(row+1)+'" value="'+quantity[row]+'" readonly="readonly" />' +
	  		 '<span class="'+className+'">'+quantity[row]+'</span>' +
			 '</div>'; 	  
	}
	else
	if(column==4){
	  var rowTotal = 0;
	  if (salePrice[row] > 0)
	  	rowTotal =  quantity[row] * salePrice[row];
	  return '<div style="text-align:right">' + 
	  		 '<span class="'+className+'">$ ' + parseFloat( rowTotal ).toFixed(2) + '</span>' +
			 '</div>';    	
	}
	if(column==5){		
  	  return '<div style="text-align:right">' + createImage() + '</div>'; 	   
	}
}

function setCookie( name, value )
{  	
	document.cookie= name + "=" + escape( value );
}

function getCookie( name ){
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) 
		    return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }

    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie( name )
{  if ( getCookie(name) !=null )
        document.cookie = name + "= ";
}

function arrayCookie( cookieName ){
    var splitCookie = 0;
	
	if ( returnCookie( cookieName, false )!=false ){
	     splitCookie = getCookie( cookieName );
		 return splitCookie.split(',');
	}
	else
		 return splitCookie;
}

function valueCookie( name ){
    var cookieValue = 0;

	if ( returnCookie( name, false ) != false  ){
	     var cookie = arrayCookie( name );
		 for ( var cont=0; cont<cookie.length; cont++ ){
	           cookieValue = parseFloat(cookieValue) + parseFloat(cookie[cont]);
	     }
	     return cookieValue;
	}
	else
	     return cookieValue;
}

function returnCookie( cookieName, paramToReturn ){
    if ( getCookie( cookieName ) == null)
        return paramToReturn;
    else
        return getCookie(cookieName);
}

function compareCookie ( cookieToFind, cookieToChange ){
	var findItFlag = false;
	var toFind     = getElement( cookieToFind ).value;
    var changeIt   = getElement( "quantitySelect" ).value;
	
	if ( returnCookie( cookieToFind, false ) == false )
	     setCookie( cookieToFind ,"0"); 
    
	if ( getCookie( cookieToFind ) != "0"){
	     var count = 0;
		 var toCompare = arrayCookie( cookieToFind );
		 var toChange  = arrayCookie( cookieToChange );
		
		 do {
		     if ( toCompare[ count ] == toFind ){
				  toChange [ count ] = parseInt(toChange [count]) + parseInt(changeIt);
				  setCookie( cookieToChange, toChange )
				  findItFlag = true;				  
			 }
			 count++;
		 }while (count < toCompare .length && findItFlag != true );
	}
	return findItFlag;
}

function currencyConverter(amount){
	if (document.getElementById("paypalCurrencyRate")!=null) 
		var paypayCurrencyRate = document.getElementById("paypalCurrencyRate").value;
	else
		var paypayCurrencyRate = 1;
	return  parseFloat(amount / paypayCurrencyRate).toFixed(2); 
}