var xmlhttp;
var sitepath = 'http://www.tafmusicgifts.co.uk/';
//var sitepath = 'http://192.168.0.7/tafmusic/root/gifts/';

// get shop product information
function shopProdDisplay(itemid,cat) {

xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
//  var afile = 'http://www.tafmusicgifts.co.uk/includes/ajax/aitemreplace.php?id=' + itemid + '&c=' + cat;
//  var afile = 'http://192.168.0.7/tafmusic/root/gifts/includes/ajax/aitemreplace.php?id=' + itemid + '&c=' + cat;
  var afile = sitepath + 'includes/ajax/aitemreplace.php?id=' + itemid + '&c=' + cat;
  xmlhttp.onreadystatechange=updateProdDetailText;
  xmlhttp.open("GET",afile,true);
  xmlhttp.send('');
 }
}

function updateProdDetailText()
{

if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {

	var newtxt = xmlhttp.responseText;
	newtext = newtxt.replace(/[\n]/g,"");
	var product_text = newtext.split('<#break#>')
	var doctitle = document.title.split('- ');
	var doctitleroot = doctitle[doctitle.length-1];
//	document.title = 'Music gifts: ' + product_text[0] + ' - From the TAF Music Giftshop'; // + ' - ' + doctitleroot;
	for (var i = 1; i <= product_text.length -1;i++) {
		var lineitem = product_text[i].split('::');
		switch(lineitem[0]){
			case 'pagetitle':

			eval("document.getElementById('lightbox_img').title='" + lineitem[1] + "'");
			eval("document.getElementById('pagetitle').innerHTML='" + lineitem[1] + "'");
				break;
			case 'special_price' :
			if (lineitem[1] > 0) {
				document.getElementById('itemspprice').style.display="block";
				document.getElementById('itemprice').className="itempricesp";
//				document.getElementById('itemprice').style.textDecoration="line-through";
			} else {
				document.getElementById('itemspprice').style.display="none";
//				document.getElementById('itemprice').style.textDecoration="none";
				document.getElementById('itemprice').className="itemprice";
			}
			case 'metatitle':
				document.title = product_text[0] + ' ' + lineitem[1];
				break;
			case 'metadesc':
				document.getElementById('metadesc').content = product_text[0] + ', ' + lineitem[1];
				break;
			case 'metakey':
				document.getElementById('metakey').content = product_text[0] + ', ' + lineitem[1];
				break;
			default:

			eval("document.getElementById('" + lineitem[0] + "').innerHTML='" + lineitem[1] + "'");
		} // switch
		/*
		if (lineitem[0] == 'pagetitle') {
			eval("document.getElementById('lightbox_img').title='" + lineitem[1] + "'");
		}
//		if (lineitem[0] == 'itemqty' && lineitem[1] == 0) {
//			document.getElementById('addtotrolleylink').href='javascript:productNotAvailable()';
//		}
		if (lineitem[0]=='special_price' ) {
			if (lineitem[1] > 0) {
				document.getElementById('itemspprice').style.display="block";
				document.getElementById('itemprice').className="itempricesp";
//				document.getElementById('itemprice').style.textDecoration="line-through";
			} else {
				document.getElementById('itemspprice').style.display="none";
//				document.getElementById('itemprice').style.textDecoration="none";
				document.getElementById('itemprice').className="itemprice";
			}

		} else if (lineitem[0]=='metatitle') {
			document.title = product_text[0] + ': ' + lineitem[1] + ' from TAF Music gifts';
		}	else {   // don't process special_price as there is no such item!
			eval("document.getElementById('" + lineitem[0] + "').innerHTML='" + lineitem[1] + "'");
		} */

	}

  }
  }
}

function shopImgRepl(id,img,cat,scrollindex){
	var largeimg = img.replace('medium','large');
	eval("document.getElementById('productImageLarge').src='http://www.tafmusicgifts.co.uk/images/products/medium/" + img + "m.jpg'");
	var	href=sitepath + 'product.php?tid=' + id + '&c=' + cat + '&z=' + scrollindex;
//	alert (href);
	eval("document.getElementById('addtotrolleylink').href='" + href + "'");
	eval("document.getElementById('lightbox_img').href='http://www.tafmusicgifts.co.uk/images/products/large/" + img + "b.jpg'");

	document.getElementById('itemAddedToTrolleyMsgBox').style.display="none";
	shopProdDisplay(id,cat);
}
/**
 *
 * @access public
 * @return void
 **/
function productNotAvailable(){
	alert ("This product is currently out of stock");
}

// check username is not already in use
function shopInvoiceNo() {
//alert ("here ..... ");
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
  var afile = 'includes/ajax/ashop_invoice_no.php';
//   xmlhttp.onreadystatechange={return: xmlhttp.responseText};
  xmlhttp.onreadystatechange=returnInvoiceNo;
  xmlhttp.open("GET",afile,true);
  xmlhttp.send('');
  }
}

function returnInvoiceNo()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
	   var response = xmlhttp.responseText;
//	   alert("response" + response);
	   eval("document.getElementById('pp_invoice_no').value='" + response + "'");
    }
  }
}

function adjustQty(itemid,origqty){
	var item= eval("document.getElementById('qty_selector_" + itemid + "')");
	var newqty = item.selectedIndex;
	alert((newqty *1) + 1);
	document.getElementById('trolleyform').action="www.tafmusicgifts.co.uk/my_trolley.php";
	document.getElementById('uid').value=itemid;
	document.getElementById('update_qty').value=(newqty *1) + 1;
	document.getElementById('orig_qty').value=origqty;

	document.getElementById('trolleyform').submit();

}
function increaseQty(itemid,currqty,incrtype){

	if (incrtype == 1) {
		newqty = (currqty * 1) + 1;
	} else {
		newqty = (currqty * 1) - 1;
	}
	document.getElementById('trolleysubmitform').action="http://www.tafmusicgifts.co.uk/my_trolley.php";
	document.getElementById('uid').value=itemid;
	document.getElementById('update_qty').value=newqty;
	document.getElementById('orig_qty').value=currqty;

	document.getElementById('trolleysubmitform').submit();

}

// trolley functions

function addToTrolley(itemid){
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
  //var afile = 'http://www.tafmusicgifts.co.uk/includes/ajax/additemto_trolley.php?tid=' + itemid;
  var afile = filepath +'includes/ajax/additemto_trolley.php?tid=' + itemid;
  xmlhttp.onreadystatechange=trolleyItemAddedMsg;
  xmlhttp.open("GET",afile,true);
  xmlhttp.send('');
  }
}

function trolleyItemAddedMsg()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {
	var oTrolley = new Object();
	oTrolley = xmlhttp.responseText;
	var res = oTrolley.split("xxx");

	var item = res[0].substr(10);
	if (res[0].substr(2,7) == "success") {
	  var msg = item + " has been added to your trolley";
	} else {
	  var msg = "Sorry, " + item + " WAS NOT added to your trolley";
	}
	  eval(" document.getElementById('addItemToTrolleyMsg').innerHTML='" + msg + "'");
	  document.getElementById('itemAddedToTrolleyMsgBox').style.display="block";
	  eval("document.getElementById('trolleydata').innerHTML='" + res[1] + "'");
  }
  }

}


function setFirstItemTrolleyId(){
//	var id = document.getElementById('prodid').innerHTML;
//	document.getElementById('trolley_summary_add').onclick=function(){ addToTrolley(id);};
}

function itemAddedToTrolleyMsg(item){
	var msg = item + " has been added to your trolley";
	eval(" document.getElementById('addItemToTrolleyMsg').innerHTML='" + msg + "'");
	document.getElementById('itemAddedToTrolleyMsgBox').style.display="block";
 }
function itemNotAddedToTrolleyMsg(item){
	var msg = "Sorry, " + item + " is currently unavailable has been NOT added to your trolley";
	eval(" document.getElementById('addItemToTrolleyMsg').innerHTML='" + msg + "'");
	document.getElementById('itemAddedToTrolleyMsgBox').style.display="block";
 }
function hideItemAddedToTrolleyMsgBox(){
	document.getElementById('itemAddedToTrolleyMsgBox').style.display="none";
}
function emptyTrolley(itemcount){
	var msg= "You have chosen to remove all " + itemcount + " items from your trolley\n\n";
	msg += "Do you really want to do this?"
	if (confirm(msg)) {
		window.location="my_trolley.php?et=all";
	}
}

 function personaliseCharCount(item_ref,maxchar) {
		var custom_text = 'custom_text_' + item_ref;
		var charcount = 'charcount_' + item_ref
//		alert (charcount);
		var textval = document.getElementById(custom_text).value;
		var chrRemain = (maxchar*1) - (textval.length);
		document.getElementById(charcount).innerHTML = chrRemain;
}

function proceedToPaypal(){
	shopInvoiceNo();
	  if (confirm('pp_invoice_no = ' + document.getElementById('pp_invoice_no').value)) {
		document.getElementById('trolleyform').submit();
	  }
}


function hoverToPaypal(){
	window.status="Click to proceed to Pay Pal checkout";
}


function updateBulkQty(formid){
//	var selidex = document.getElementById('bulk_qty_' + itemid).selectedIndex;

}

function setBulkQty(){
	var sellist = document.getElementById('bulk_qty');
	var selindex = sellist.selectedIndex;
	var bulkSel = sellist.options[selindex].value;
	var href = document.getElementById('addtotrolleylink').href;
	var bulkQty = bulkSel.split('++');
	if (href.indexOf('&q=') > 5) {
		var newHref = href.substr(0,href.indexOf('&q=')) + '&q=' + bulkQty[0] + '&pr=' + bulkQty[1];
	} else {
		var newHref = href + '&q=' + bulkQty[0] + '&pr=' + bulkQty[1];

	}

	eval("document.getElementById('addtotrolleylink').href='" + newHref + "'");

//	alert (selindex + ' -- ' + bulkQty[0] + ' == ' + bulkQty[1] + ' ... ' + href);

}
/* * * * * * * * * * * * * * * * * * * * * * * * *\
   ##     search filter functions      ##
\* * * * * * * * * * * * * * * * * * * * * * * * */

function shopSearchFilter(step) {

var	itemlist = "_sub_cat";
var sellist = eval("document.getElementById('_sub_cat')");
var selidx  = sellist.selectedIndex;
var selitem = sellist.options[selidx].value

xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
//  var afile = 'http://www.tafmusicgifts.co.uk/includes/ajax/asearchfilters.php?c=' + selitem ;
  var afile = sitepath + 'includes/ajax/asearchfilters.php?c=' + selitem ;
  xmlhttp.onreadystatechange=updateProdSearchFilter;
  xmlhttp.open("GET",afile,true);
  xmlhttp.send('');
 }
}
function updateProdSearchFilter()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {
	// rest the text search box
	  document.getElementById('_text_search').value = '';
	  var newtxt = xmlhttp.responseText;
  	  var colorlist = true;
  	  var aColorList = newtxt.split('::');
	  var searchList = eval("document.getElementById('_colour')");
	  searchList.options.length=0;
	  searchList.options[0] = new Option('All Colours','0',false,true);
	  for (i=1;i < aColorList.length; i++) {
		searchList.options[i] = new Option(aColorList[i-1],aColorList[i-1],false);
	  }
	}
  }
}

function shopSearchFilter_old(step) {
switch(step){
	case '1':
		itemlist = "_sub_cat";
//		document.getElementById('_style').options.length=0;
//		document.getElementById('_style').options[0] = new Option('All styles','0',false,true);
		break;
	case '2':
		itemlist = "_type";
		break;
	case '3':
		itemlist = "_style";
		break;
	case '4':
		itemlist = "_colour";
		break;
} // switch
var	itemlist = "_sub_cat";
var sellist = eval("document.getElementById('" + itemlist + "')");
var selidx  = sellist.selectedIndex;
var selitem = sellist.options[selidx].value

xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
//  var afile = 'http://www.tafmusicgifts.co.uk/includes/ajax/asearchfilters.php?c=' + selitem + '&s=' + step;
  var afile = sitepath + 'includes/ajax/asearchfilters.php?c=' + selitem + '&s=' + step;
  xmlhttp.onreadystatechange=updateProdSearchFilter;
  xmlhttp.open("GET",afile,true);
  xmlhttp.send('');
 }
}
function updateProdSearchFilter_old()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {
	// rest the text search box
	document.getElementById('_text_search').value = '';
	var newtxt = xmlhttp.responseText;
	if (newtxt.indexOf('@@')) {
		itemlist = newtxt.split('@@');
		var colorlist = true;
	} else {
		itemlist = new array(newtxt);
		var colorlist = false;
	}

	var aSubCatList = itemlist[0].split('::');
	var presel = false;
	var searchList = eval("document.getElementById('" + aSubCatList[0].substring(2) + "')");
	searchList.options.length=0;
	searchList.options[0] = new Option('All','0',false,true);
	for (i=1;i < aSubCatList.length; i++) {


		searchList.options[i] = new Option(aSubCatList[i],aSubCatList[i],false);
	}
	if (colorlist) {
  	  var aColorList = itemlist[1].split('::');
	  var searchList = eval("document.getElementById('_colour')");
	  searchList.options.length=0;
	  searchList.options[0] = new Option('All','0',false,true);
	  for (i=1;i < aColorList.length; i++) {
		searchList.options[i] = new Option(aColorList[i],aColorList[i],false);
	  }
	}
  }
  }
}

function basketScroll(amount){
//	alert (amount + ' -- ' + ((amount * 1) * 110) -20);
	if (amount > 5) {
    	var scrollsize = ((amount * 1) * 90);
	}

//alert (amount + ' -- ' + scrollsize)	;
	document.getElementById('thumbbox').scrollTop = scrollsize;

}
function shopScroll(direction){

    var scrollsize = 150;
	var i=0;
	while (i < scrollsize ) {
		timer=setTimeout("shopScroller('"+direction+"')",20);
		i++;
	}
	timer = clearTimeout();
}

function shopScroller(direction){
	switch(direction){
		case "down":
			document.getElementById('thumbbox').scrollTop-=1;
			break;
		case "up":
			document.getElementById('thumbbox').scrollTop+=1;
			break;
		case "top":
			document.getElementById('thumbbox').scrollTop=0;
			break;
		case "bottom":
			document.getElementById('thumbbox').scrollTop=document.getElementById('thumbbox').scrollHeight;
			break;
	} // switch

}

function shopGoBtnOver() {
	document.getElementById('shopGoButton').style.backgroundImage="url(http://www.tafmusicgifts.co.uk/images/pageheader/onclickbutton.jpg) top left no-repeat"
}
function shopGoBtnOut() {
	document.getElementById('shopGoButton').style.backgroundImage="url(http://www.tafmusicgifts.co.uk/images/pageheader/offclickbutton.jpg) top left no-repeat"
}


function hiliteHomeItem(item,linetype,navtype){

	var myloc = 'a/' + window.location;
	var homeitem ='homeimg_' + item;

	if (myloc.indexOf('shop.php') > 1) {
		if (linetype=='on'){
		var vColor = '#FA6BD5';
			document.getElementById(homeitem ).style.color=  vColor ;
			eval("document.getElementById('" + homeitem + "').style.textDecoration='underline'");
			eval("document.getElementById('navitem_" + item + "').style.color='" + vColor + "'");
			eval("document.getElementById('homeimg_" + item + "').style.textDecoration='underline'");

		}
		if (linetype=='off'){
		var vColor = '#91C0F9';
		var doc ='homeimg_' + item;
		if (document.getElementById(homeitem) != null) {
			eval("document.getElementById('" + homeitem + "').style.textDecoration='none'");
			eval("document.getElementById('" + homeitem + "').style.color='" + vColor + "'");
		}
			eval("document.getElementById('navitem_" + item + "').style.textDecoration='none'");
			eval("document.getElementById('navitem_" + item + "').style.color='" + vColor + "'");
		}
	}
}

function updateringsize(itemid) {
xmlhttp=null;
var ringsize = document.getElementById('_ring_size');
	var sellist = document.getElementById('_ring_size');
	var selindex = sellist.selectedIndex;
	var ringsize = sellist.options[selindex].value;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
//	var path = 'http://www.tafmusicgifts.co.uk/';
	var path = 'http://192.168.0.7/tafmusic/gifts/';
  var afile = path + 'includes/ajax/asetringsize.php?i=' + itemid  + '&r=' + ringsize;;
//  var afile = 'http://192.168.0.7/tafmusic/gifts/includes/ajax/aitemreplace.php?id=' + itemid + '&c=' + cat;
  xmlhttp.onreadystatechange=null;
  xmlhttp.open("GET",afile,false);
  xmlhttp.send('');
 }
}

function redeemShopCoupon() {

xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null) {
  var cname = document.getElementById('coupon_name').value;
  var code1 = document.getElementById('coupon_code1').value;
  var code2 = document.getElementById('coupon_code2').value;
  var postref = document.getElementById('post_ref').value;
  var subtotal = document.getElementById('sale_total_amount').value;
  var shipping = document.getElementById('handling_cart').value;
  var amt = (subtotal * 1) + (shipping * 1);
//  var amt = document.getElementById('page_total_display').innerHTML;
  eval("document.getElementById('coupon_code').value='" + cname + ' ' + code1 + '-' + code2 +"'");
//  var afile = 'http://www.tafmusicgifts.co.uk/includes/ajax/redeemshopcoupon.php?n=' + cname + '&c1=' + code1 + '&c2=' + code2;
  var afile = sitepath + 'includes/ajax/redeemshopcoupon.php?n=' + cname + '&c1=' + code1 + '&c2=' + code2 + '&p=' + postref + '&s=' + amt;
//  var afile = 'http://www.tafmusicgifts.co.uk/includes/ajax/redeemshopcoupon.php?n=' + cname + '&c1=' + code1 + '&c2=' + code2 + '&p=' + postref + '&s=' + amt;
  xmlhttp.onreadystatechange=updateShopCoupon;
  xmlhttp.open("GET",afile,true);
  xmlhttp.send('');
 }
}

function updateShopCoupon()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {
    	var responseText = xmlhttp.responseText;
  		var couponVar = responseText.split('::');
    	var subtotal = document.getElementById('sale_total_amount').value;
    	var shipping = document.getElementById('handling_cart').value;

    	var saletotal = subtotal *1; // + saletotal;
    	var dis_amt;
    	var discount_amt;

			if (couponVar[1] > 0) {  // percentage discount
 	   	  		dis_amt = ((subtotal)  * (couponVar[1]/100));
				discount_amt = number_format(dis_amt * 100);
				var new_amt  = ((subtotal * 1) - dis_amt);
				newtotal = new_amt + shipping * 1;

    		}
    		else if (couponVar[2] > 0) {  // gift voucher - fixed discount amount
    			dis_amt = (couponVar[2]);
    			discount_amt = number_format(dis_amt * 100);
				var new_amt  = ((saletotal) - dis_amt);

				if (discount_amt < (((saletotal) + (shipping *1)) - .5)) {
					if (discount_amt >= saletotal) {  // we need to cover postage with the gift coupon - so convert postage to a line item
						var postitem = couponVar[4];
						document.getElementById('item_name_' + postitem).disabled = false;
						document.getElementById('amount_' + postitem).disabled = false;
						document.getElementById('quantity_' + postitem).disabled = false;
						document.getElementById('handling_cart').value = '0.00';
					}
					var newtotal = (((saletotal *1) + (shipping *1)) - dis_amt)
					var zerototal = false;
				} else  {
					document.getElementById('payby_coupon').value="1";
					document.getElementById('coupon_value').value=discount_amt;
				// need to process order without going through PayPal
					var newtotal = 0;
					var zerototal = true;
			   }
    	} else if (couponVar[3] != 'xx') {  // voucher's been redeemed or something's wrong - display the error message
    		alert (couponVar[3]);
    		return false;
    	}
		// display discount rows
    	document.getElementById('discountdiv').style.display = 'block';
    	document.getElementById('summaryDiscount').style.display = 'block';

    	// set discount detail - plus percentage
    	if (couponVar[1] > 0) {
    		document.getElementById('discount_reason').innerHTML = 'Discount - Coupon Redemption (' + couponVar[1] + '%) : ';
			document.getElementById('summarydisc_pc').innerHTML =  couponVar[1];
		} else if (couponVar[2] > 0) {
	    	document.getElementById('discount_reason').innerHTML = 'Discount - Gift Coupon (&#163;' + couponVar[2] + ') : ';
		//document.getElementById('summarydisc_pc').innerHTML =  couponVar[1];
		}
	   	// set the actual amount of discount
		document.getElementById('page_discount_amt').innerHTML = '-' + discount_amt;
	   	document.getElementById('summarydisc_amt').innerHTML = discount_amt;
	   	document.getElementById('discount_amount_cart').value =  discount_amt;

	   	// set the new total
	   	if (zerototal) {
		   	document.getElementById('disp_subtotal').innerHTML = '0.00';
		   	document.getElementById('summary_total_display').innerHTML = '0.00';
	   		document.getElementById('page_total_display').innerHTML = '0.00';
		   	document.getElementById('paypalbtn').src = 'images/clickheretopayv.jpg';
		   	document.getElementById('paypalbtn').alt = 'Click here to redeem a voucher to pay for your purchases';
	   		document.getElementById('paypalbtn').title = 'Click here to redeem a voucher to pay for your purchases';
	   	} else {
	   		document.getElementById('disp_subtotal').innerHTML = number_format(new_amt * 100);
		   	document.getElementById('summary_total_display').innerHTML = number_format(newtotal * 100);
		   	document.getElementById('page_total_display').innerHTML =  number_format(newtotal * 100);
	   	}
  }
 }
}
function updateShopCoupon_working_110505()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"function checkUserName() {
    	var responseText = xmlhttp.responseText;
//alert(responseText);
  		var couponVar = responseText.split('::');
    	var subtotal = document.getElementById('sale_total_amount').value;
    	var saletotal = document.getElementById('page_total_display').innerHTML;
    	var dis_amt;
    	var discount_amt;
    	if (couponVar[1] > 0) {  // percentage discount
    	  	dis_amt = ((subtotal)  * (couponVar[1]/100));

    	} else if (couponVar[2] > 0) {  // gift voucher - fixed discount amount
    		dis_amt = (couponVar[2]);
    	} else if (couponVar[3] != 'xx') {  // voucher's been redeemed or something's wrong - display the error message
    		alert (couponVar[3]);
    		return false;
    	}
		discount_amt = number_format(dis_amt * 100);
		//discount_amt = dis_amt * 100;
		var new_amt  = ((saletotal * 1) - dis_amt);
		if (discount_amt < saletotal) {
			var newtotal = ((saletotal *1) - dis_amt)
			var zerototal = false;
		} else {
			var newtotal = 0;
			var zerototal = true;
		}



		// display discount rows
    	document.getElementById('discountdiv').style.display = 'block';
    	document.getElementById('summaryDiscount').style.display = 'block';

    	// set discount detail - plus percentage
    	if (couponVar[1] > 0) {
    	document.getElementById('discount_reason').innerHTML = 'Discount - Coupon Redemption (' + couponVar[1] + '%) : ';
		document.getElementById('summarydisc_pc').innerHTML =  couponVar[1];
		} else if (couponVar[2] > 0) {
    	document.getElementById('discount_reason').innerHTML = 'Discount - Gift Coupon (&#163;' + couponVar[2] + ') : ';
		//document.getElementById('summarydisc_pc').innerHTML =  couponVar[1];
		}
	   	// set the actual amount of discount
		document.getElementById('page_discount_amt').innerHTML = '-' + discount_amt;
	   	document.getElementById('summarydisc_amt').innerHTML = discount_amt;
	   	document.getElementById('discount_amount_cart').value =  discount_amt;

	   	// set the new total
	   	if (zerototal) {
	   	document.getElementById('disp_subtotal').innerHTML = '0.00';
	   	document.getElementById('summary_total_display').innerHTML = '0.00';
	   	document.getElementById('page_total_display').innerHTML = '0.00';
	   	} else {
	   	document.getElementById('disp_subtotal').innerHTML = number_format(new_amt * 100);
	   	document.getElementById('summary_total_display').innerHTML = number_format(newtotal * 100);
	   	document.getElementById('page_total_display').innerHTML = number_format(newtotal * 100);
	   	}
  }
 }
}

function checkVoucherForm() {
	var err_msg = new Array();
	var error_msg = '';
	var error_message = '';
	var j=0;

	var name = document.getElementById('last_name').value;
	if (name.length < 1) {
		err_msg[j] = "Your Name";
		j++;
	}
	var name = document.getElementById('address_street').value;
	if (name.length < 1) {
		err_msg[j] = "Street Address";
		j++;
	}
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  var filter1 = (new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  // Expression for postcodes: ANA NAA
  var filter2 = (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  // Expression for postcodes: AANA  NAA
  var filter3 = (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  	var postcode = document.getElementById('address_zip').value;

	if (! filter1.test(postcode) && ! filter2.test(postcode) && ! filter3.test(postcode)) {
		err_msg[j] = "Valid UK postcode";
		j++;
	}

	var email = document.getElementById('payer_email').value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (! filter.test(email)) {
		err_msg[j] = "Valid email address";
		j++;
	}

 	for (i=0; i < err_msg.length; i++) {
 		error_msg += err_msg[i] + ', ';
 	}

	error_message = error_msg.substr(0,(error_msg.length)-2);
	if (error_message.length > 0) {
		error_msg = "Please check the following fields:\n\n" + error_message;
 		alert (error_msg);
	} else {
		document.getElementById('trolleyform').submit();
	}
}

function updatethumbimage(imgid){
	var imgfile = 'image' + imgid;
	var imgsrc = parent.document.getElementById(imgfile).src;
	eval("parent.document.getElementById(imgfile).src='" + imgsrc + "x'");
}
/* * * * * * * * * * * * * * * * * * * * * * * * *\
  ##  	generic functions
\* * * * * * * * * * * * * * * * * * * * * * * * */
function number_format(mynumber) {
		var newno = ((mynumber * 1)/100) + .001;
		var tempno = newno.toString();
		var aNum = tempno.split(".");
		return (aNum[0] + '.' +  aNum[1].substr(0,2));


}
