var xmlhttp;

// 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;
  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] + ' - ' + doctitleroot;
	for (var i = 1; i <= product_text.length -1;i++) {
		var lineitem = product_text[i].split('::');
		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()';
//		}

			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='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;
  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 ;
  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;
  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 + "'");
		}
	}
}


/* * * * * * * * * * * * * * * * * * * * * * * * *\
  ##  	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));
}