function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function navigateToAnchor(){
   var anchor = document.getElementById("anchor");
   if (anchor != null) {
      if (anchor.value != ""){
         location.hash = anchor.value;
      }
   }
}

function printdiv(printpage) {
	var headstr = "<html><head><title></title></head><body>";
	var footstr = "</body>";
	var newstr = document.all.item(printpage).innerHTML;
	var oldstr = document.body.innerHTML;
	document.body.innerHTML = headstr+newstr+footstr;
	window.print(); 
	document.body.innerHTML = oldstr;
	return false;
}

function backToAccounts() {
	window.location = "../secure/tradeaccmanagement.jsf";
}

function submitIEfromInput(formName){  
   if ((event.which && event.which == 13) ||   
     (event.keyCode && event.keyCode == 13)){  
     document.all(formName).submit();  
   }  
}

function incrementQtyBox() {
	var x=document.forms["productInfo"]["productInfo:qtybox"].value;
	var y = Number(x);
	if (y==null) {
		y=0;
	}
	y=y+1;
	document.forms["productInfo"]["productInfo:qtybox"].value=y;
}

function decrementQtyBox() {
	var x=document.forms["productInfo"]["productInfo:qtybox"].value;
	var y = Number(x);
	if (y==null) {
		y=0;
	} else {
		if (y>0) {
			y=y-1;
		}
	}
	document.forms["productInfo"]["productInfo:qtybox"].value=y;
}

function incrementQtyBoxProductList(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var targId=targ.id;
	var newTargId=targId.replace("incrementButton","qtybox");
	var x=document.forms["productList"][newTargId].value;
	var y = Number(x);
	if (y==null) {
		y=0;
	}
	y=y+1;
	document.forms["productList"][newTargId].value=y;
}

function decrementQtyBoxProductList(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var targId=targ.id;
	var newTargId=targId.replace("decrementButton","qtybox");
	var x=document.forms["productList"][newTargId].value;
	var y = Number(x);
	if (y==null) {
		y=0;
	} else {
		if (y>0) {
			y=y-1;
		}
	}
	document.forms["productList"][newTargId].value=y;
}

function linkToRoomSearch(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var searchValue=targ.value;
	if (document.domain == "localhost") {
		var url="http://" + document.domain + ":8080/tearcraft/public/saleproducts.jsf?freeSearch=room:" + searchValue;
	} else {
		var url="http://" + document.domain + "/tearcraft/public/saleproducts.jsf?freeSearch=room:" + searchValue;
	}
	window.location = url;
}

function linkToPriceSearch(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var searchValue=targ.value;
	if (searchValue.substr(0,1) == "+") {
		// starts with a plus
		var startPrice = "0";
		var endPrice = searchValue.substr(1,10);
	} else {
		if (searchValue.substr((searchValue.length)-1,1) == "+") {
			// ends with a plus
			var startPrice = searchValue.substr(0,(searchValue.length)-1);
			var endPrice = "99999";
		} else {
			// start and end prices
			var listArray = searchValue.split("-");
			var startPrice = listArray[0];
			var endPrice = listArray[1];
		}
	}
	startPrice = pad(startPrice,10);
	endPrice = pad(endPrice,10);
	if (document.domain == "localhost") {
		var url="http://" + document.domain + ":8080/tearcraft/public/saleproducts.jsf?freeSearch=priceInc:[" + startPrice + " TO " + endPrice + "]";
	} else {
		var url="http://" + document.domain + "/tearcraft/public/saleproducts.jsf?freeSearch=priceInc:[" + startPrice + " TO " + endPrice + "]";
	}
	window.location = url;
}

function linkToPartnerSearch(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var searchValue=targ.value;
	if (document.domain == "localhost") {
		var url="http://" + document.domain + ":8080/tearcraft/public/saleproducts.jsf?freeSearch=partner:" + searchValue;
	} else {
		var url="http://" + document.domain + "/tearcraft/public/saleproducts.jsf?freeSearch=partner:" + searchValue;
	}
	window.location = url;
}

function linkToTypeSearch(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var searchValue=targ.value;
	if (document.domain == "localhost") {
		var url="http://" + document.domain + ":8080/tearcraft/public/saleproducts.jsf?freeSearch=productGroup:" + searchValue;
	} else {
		var url="http://" + document.domain + "/tearcraft/public/saleproducts.jsf?freeSearch=productGroup:" + searchValue;
	}
	window.location = url;
}

function linkToCountrySearch(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var searchValue=targ.value;
	if (document.domain == "localhost") {
		var url="http://" + document.domain + ":8080/tearcraft/public/saleproducts.jsf?freeSearch=countryOrigin:" + searchValue;
	} else {
		var url="http://" + document.domain + "/tearcraft/public/saleproducts.jsf?freeSearch=countryOrigin:" + searchValue;
	}
	window.location = url;
}

function linkToColourSearch(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) {
		// defeat Safari bug
		targ = targ.parentNode;
	}
	var searchValue=targ.value;
	if (document.domain == "localhost") {
		var url="http://" + document.domain + ":8080/tearcraft/public/saleproducts.jsf?freeSearch=colourPalette:" + searchValue;
	} else {
		var url="http://" + document.domain + "/tearcraft/public/saleproducts.jsf?freeSearch=colourPalette:" + searchValue;
	}
	window.location = url;
}

function pad(number, length) {
	   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;
}

function checkZoomify() {
	var playerVersion = swfobject.getFlashPlayerVersion();
	if (playerVersion.major == 0) {
		var url = window.location.toString();
		var startpos = url.lastIndexOf("catalogueCode=");
		if (startpos >= 0) {
			var catCode = url.substring(startpos+14);
			if (catCode != null) {
				var container = document.getElementById("theMovie");
				imagetag = document.createElement("img");
				imagetag.id = catCode;
				imagetag.height = 300;
				imagetag.width = 300;
				imagetag.src = "https://static.prolog.co.uk/client/ecom_tearcraft/images/" + catCode + ".jpg";
				container.parentNode.replaceChild(imagetag,container);
			}
		}
		var youtubeContainer = document.getElementById("youtubeSection");
		divtag = document.createElement("div");
		youtubeContainer.parentNode.replaceChild(divtag,youtubeContainer);
	}
}
