function cvHelper(cmd,e) {
///
e = e || window.event;
if (e.pageX || e.pageY) {//netscape
x = e.pageX;
y = e.pageY;
}
else
{
x = e.clientX;
y = e.clientY;

}



obj = document.getElementById('cvHelper');

	if (cmd == 'close')
	obj.style.display='none';
	else
	{
	left = (x -100) + 'px';
	top = (y - 100) + 'px';
	obj.style.top =top  ;
	obj.style.left = left;
	obj.style.display='block';


	}




}


function setAddressButton() {
document.getElementById('address').checked=true;

}



function changePage(page) {


document.forms['main_search'].page.value=page;

sortOrder = document.forms['sub_search'].searchOrder.options[document.forms['sub_search'].searchOrder.selectedIndex].value;

document.forms['main_search'].order_by.value = sortOrder;

document.forms['main_search'].submit();

}

function changeOrder(choice)
{
document.forms['main_search'].rm.value ='list';
document.forms['main_search'].order_by.value = choice;
document.forms['main_search'].page.value = 1;
document.forms['main_search'].submit();
}


function changePageCount()
{
pagecount = document.forms['sub_search'].pagecount.options[document.forms['sub_search'].pagecount.selectedIndex].value;
document.forms['main_search'].rm.value ='list';
document.forms['main_search'].pagecount.value = pagecount;
document.forms['main_search'].page.value = 1;
document.forms['main_search'].submit();
}

function doSearch()
{
document.forms['main_search'].mode.value = 'list';
//always a new search so set page to 1
document.forms['main_search'].page.value =1;
document.forms['main_search'].submit();
}


function setPublisher(whc)
{
document.forms['main_search'].mode.value = 'publisherid';
//always a new search so set page to 1
document.forms['main_search'].page.value =1;
document.forms['main_search'].product.value =whc;
document.forms['main_search'].submit();
}


function backToSearch()
{

document.forms['main_search'].submit();

}

function readMore(whc,friendlyURL)
{

document.forms['main_search'].rm.value ='book';
document.forms['main_search'].product.value =whc;
document.forms['main_search'].submit();
}

function meetTheAuthor(whc)
{
document.forms['main_search'].rm.value ='author';
document.forms['main_search'].product.value =whc;
document.forms['main_search'].submit();

}

function changeCat(whc)
{
document.forms['main_search'].rm.value ='list';
document.forms['main_search'].mode.value ='browse';
//uncomment to reset sort order to default
//document.forms['main_search'].order_by.value = '';
//document.forms['sub_search'].searchOrder.selectedIndex=1;
document.forms['main_search'].page.value = 1;
document.forms['main_search'].keyword.value = '';
document.forms['main_search'].search_filter.selectedIndex = 1;
document.forms['main_search'].keyword_saved.value = '';
document.forms['main_search'].search_filter_saved.value = '';
document.forms['main_search'].product.value =whc;
document.forms['main_search'].submit();
}

function changeSubCat(whc)
{
document.forms['main_search'].rm.value ='list';
document.forms['main_search'].mode.value ='subcategory';
document.forms['main_search'].product.value =whc;


document.forms['main_search'].page.value = 1;
//document.forms['main_search'].order_by.value = '';
//document.forms['sub_search'].searchOrder.selectedIndex=1;

document.forms['main_search'].submit();
}





function getBillingAddress()
{
if (document.getElementById('billingisdelivery').checked)
ajaxFunction('getbillingaddress','');
else
clearBillingAddress();
}


function addToCart(prod)
{
ajaxFunction('addone',prod);
}

function deleteCart(prod) {
ajaxFunction('delete', prod);
}

function updateCart(prod) {
ajaxFunction('update', prod);
}



function forgot() {
email = document.getElementById('email').value;
ajaxFunction('forgotpassword',email);

}


function editAddress(whc) {


document.getElementById('editBox'+whc).style.display="block";
document.getElementById('loader'+whc).style.display="block";

ajaxFunction('editaddress',whc);

}


function ajaxFunction(call,val)
{
if ((call == 'addone') || ( call == 'delete')   || (call == 'update') || (call =='getbillingaddress') ) {
obj = document.getElementById('loader'+val);
obj.style.display='block';
}

if (call == "forgotpassword" ) {
obj = document.getElementById('loader');
obj.style.display='block';
}


var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
     //handle response here
 var resp  =xmlHttp.responseText.replace(/[\r\n]/,'');
if (resp == 'error') {
alert('Sorry, an error has occured.') ; //dev need to fix this
}
else {
//JSON -> object
var results  = eval('(' + resp + ')');
if (call == 'addone') {
	obj = document.getElementById('loader'+val);
	obj.style.display='none';
	obj = document.getElementById('added'+val);
	obj.innerHTML = results.qty + ' now in cart';

	obj = document.getElementById('cartbutton'+val);
	obj.style.display='block';
	}
	else if (call == 'delete')
	{
	//resp is subtotal-grandtot
	res = resp.split('-');
	removeProduct(val,results.subtotal,results.grandtotal);



	if (results.grandtotal == 0)
	{
		obj = document.getElementById('total');
		obj.innerHTML = 'your cart is now empty';
	}

	// No need to set display none, as element has already been removed

	//obj = document.getElementById('loader'+val);
	//obj.style.display='none';

	}
	else if (call == 'update' )
	{

	obj = document.getElementById('qty_'+val);

	if (obj.value == 0)
	{
		ajaxFunction('delete',val)
	}
	else
	{
		updateProduct(val,results.qty,results.subtot,results.subtotal,results.grandtotal);
		obj = document.getElementById('loader'+val);
		obj.style.display='none';

	}

	}
	else if (call == 'forgotpassword' )
	{
	forgotPassword(results.message);
	obj = document.getElementById('loader');
	obj.style.display='none';
	}
	else if (call == 'getbillingaddress') {
	setBillingAddress(results.line1,results.line2,results.citytown,results.zip,results.countryid);
	obj = document.getElementById('loader');
	obj.style.display='none';
	}
	else if (call == 'editaddress') {
	setAddress(val,results.Addressee,results.AddressLine1,results.AddressLine2,results.CityTown,results.PostZipCode,results.CountryID);
	obj = document.getElementById('loader'+val);
	obj.style.display='none';
	}
	else if (call == 'updateaddress') {
	obj = document.getElementById('loader'+results.id);
	obj.style.display='none';
	if (results.errors) {
		for (var error in results.errors) {
			document.getElementById(error + 'Label' + results.id).style.color = 'red';
		}
	} else {
		txt = results.Addressee + ',' + results.AddressLine1 + ',' ;
			if (results.AddressLine2 != '' )
			txt = txt + results.AddressLine2 + ',';
		txt = txt + results.CityTown + ',' + results.PostZipCode + ',' + results.Country;
		document.getElementById('addressLine' + results.id).innerHTML = txt;
		document.getElementById('editBox' + results.id).style.display='none';
	}
	}
	else
	{

	//do nothing
	}

}
	}
    } //end onready function

//build url

if ((call == 'addone') || ( call == 'delete')  ) {
//url = '/cart/' + call + '/' + val;
url = '/cgi-bin/cart.cgi?rm=' + call + '&product=' + val;
}
else if (call == 'update') {
obj = document.getElementById('qty_'+val);
qty = obj.value;
//url = '/cart/' + call + '/' + val + '/' + qty;
url = '/cgi-bin/cart.cgi?rm=' + call + '&product=' + val + '&setting=' + qty;
}
else if (call == 'getbillingaddress') {
//url = '/cart/getbillingaddress/'
url = '/cgi-bin/cart.cgi?rm=' + 'getbillingaddress';
}
else if (call == 'editaddress') {
//url = '/cart/getbillingaddress/'
url = '/cgi-bin/cart.cgi?rm=' + 'editaddress' + '&id=' + val;
}
else if (call == 'forgotpassword') {
url = '/cgi-bin/cart.cgi?rm=forgotpassword&email=' + val;
}
else if (call == 'updateaddress') {
url = '/cgi-bin/cart.cgi?rm=updateaddress' + val;
}

  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
  }


function forgotPassword(msg) {
document.getElementById('message').innerHTML = msg;

}

function clearBillingAddress()
{


document.getElementById('BillAddressLine1').value='';
document.getElementById('BillAddressLine2').value='';
document.getElementById('BillCityTown').value='';
document.getElementById('BillPostZipCode').value='';
countrySel = document.getElementById('Billcountry');
countrySel.selectedIndex=0;

}

function setBillingAddress(line1,line2,citytown,zip,countryid)
{
	document.getElementById('BillAddressLine1').value=line1;
	document.getElementById('BillCityTown').value=citytown;
	document.getElementById('BillPostZipCode').value=zip;

	if (typeof line2 == 'string') {
		document.getElementById('BillAddressLine2').value=line2;
	}

	countrySel = document.getElementById('Billcountry');
	idx = countrySel.options.length;

	//set the country selector
	for (i=0; i < idx; i++) {

		val = countrySel.options[i].value;

		if (val == countryid) {

			countrySel.selectedIndex = i;
			break;
		}
	}
}

function updateAddress(whc) {
//validate
//post
//close box
//update line display


error = 0;

Addressee = document.getElementById('Addressee' + whc).value;
AddressLine1 = document.getElementById('AddressLine1' + whc).value;

AddressLine2 = document.getElementById('AddressLine2' + whc).value;

CityTown = document.getElementById('CityTown' + whc).value;
PostZipCode = document.getElementById('PostZipCode' + whc).value;
country = document.getElementById('country' + whc).options[document.getElementById('country' + whc).selectedIndex].value;

if (Addressee  == '') {
error=1;
document.getElementById('AddresseeLabel' + whc).style.color='red';
}

if (AddressLine1 == '') {
error=1;
document.getElementById('AddressLine1Label' + whc).style.color='red';
}

if (CityTown == '') {
error=1;
document.getElementById('CityTownLabel' + whc).style.color='red';
}


if (PostZipCode == '') {
error=1;
document.getElementById('PostZipCodeLabel' + whc).style.color='red';
}

if (country == 0) {
error=1;
document.getElementById('CountryLabel' + whc).style.color='red';
}


if (error == 0) {
params = '&id=' + whc + '&Addressee=' + Addressee + '&AddressLine1=' + AddressLine1 +  '&AddressLine2=' + AddressLine2 + '&CityTown=' + CityTown + '&PostZipCode=' + PostZipCode + '&CountryID=' + country;

document.getElementById('loader'+whc).style.display="block";
ajaxFunction('updateaddress',params);




}

}

function setAddress(val,addressee,line1,line2,citytown,zip,countryid) {


document.getElementById('Addressee'+val).value=addressee;
document.getElementById('AddressLine1'+val).value=line1;
document.getElementById('AddressLine2'+val).value=line2;
document.getElementById('CityTown'+val).value=citytown;
document.getElementById('PostZipCode'+val).value=zip;

countrySel = document.getElementById('country'+val);
idx = countrySel.options.length;
//set the country selector
for (i=0; i < idx; i++) {
val = countrySel.options[i].value;
	if (val == countryid) {
	countrySel.selectedIndex = i;
	break;
	}

}

}



function updateProduct(val,qty,subtot,subtotal,grandtotal) {

subtot = new Number(subtot);
subtotal = new Number(subtotal);
grandtotal= new Number(grandtotal);

	subtot = subtot.toFixed(2);
	subtotal = subtotal.toFixed(2);
	grandtotal = grandtotal.toFixed(2);

	obj = document.getElementById('qty_'+val);
	obj.value = qty;
	obj = document.getElementById('subtot_'+val);
	obj.innerHTML=subtot;

	obj = document.getElementById('subtotal');
	obj.innerHTML=subtotal;

	obj = document.getElementById('grandtotal');
	obj.innerHTML=grandtotal;

}

function removeProduct(itm,subtotal,grandtotal) {

subtotal = new Number(subtotal);
grandtotal= new Number(grandtotal);

subtotal = subtotal.toFixed(2);
grandtotal = grandtotal.toFixed(2);

obj = document.getElementById("basketrow_"+itm);
p = obj.parentNode;
p.removeChild(obj);
//dev - only use dom manipulation if browser safe
//obj.innerHTML = '';

b = document.getElementById('basket');
rows = b.getElementsByTagName('tr');

        if (rows.length <= 1)
        {
        h = document.getElementById("basket_head");
        p = h.parentNode;
        p.removeChild(h);
        m = document.getElementById('empty_basket');
        m.innerHTML = "Your basket is empty at the moment";
        }

obj = document.getElementById('subtotal');
obj.innerHTML=subtotal;

obj = document.getElementById('grandtotal');
obj.innerHTML=grandtotal;
}

function pay_now () {

	document.getElementById("pay_now_button").style.display = 'none';
	document.getElementById("pay_now_spinner").style.display = 'block';

	document.checkout.submit();
	return false;
}
