
defaultStatus = "Shop";
var butcol = '#C4A04A';
var txtcol = '#FF0000';
var butgif = 'Images/Pipe.gif';
var getin = "99";
var on = "on";
var off = "off";
var strChoice = "TyreSize";
var strCurrency = "€";
var curTax = 0;

//document.onkeypress = keypress;


//****************************************************************
//***=================== TRAP ENTER KEY =======================***
//****************************************************************
function keypress(evt) {
	if (evt.keyCode == 13) {
		evt.keyCode =9;
		return evt.keyCode;
	}
}

//****************************************************************
//***=========== TAB KEY RATHER THEN ENTER KEY ================***
//****************************************************************
function TabNotEnter(evt,objectID) {
	if (!evt)
		evt = window.event;

	//if (evt.keyCode == 13) {
	if (evt.keyCode == 40) {
		//****** DOWN ARROW - work out next box for focus ******
		if (objectID.length == 7){
			//******* 7 = CatQTY5 ******
			nextObjectID = objectID.substring(0,objectID.length-1);
			nextObjectNum = parseFloat(objectID.substring(objectID.length-1,objectID.length))+1;
		}else if (objectID.length == 8){
			//******* 8 = CatQTY15 ******
			nextObjectID = objectID.substring(0,objectID.length-2);
			nextObjectNum = parseFloat(objectID.substring(objectID.length-2,objectID.length))+1;
		}else{
			//******* 9 = CatQTY115 ******
			nextObjectID = objectID.substring(0,objectID.length-3);
			nextObjectNum = parseFloat(objectID.substring(objectID.length-3,objectID.length))+1;
		}
		nextObjectID = nextObjectID + nextObjectNum;
		if (document.getElementById(nextObjectID) == null){
		}else{
			document.getElementById(nextObjectID).focus();
		}
		//****** cancel the return key event ******
		if(window.event){
			evt.returnValue = false;
			evt.cancelBubble = true;
		}else{
			evt.preventDefault();
			evt.stopPropagation();
		}
		//alert(objectID + '#' + objectID.length);
	}
	if (evt.keyCode == 38) {
		//****** UP ARROW - work out previous box for focus ******
		if (objectID.length == 7){
			//******* 7 = CatQTY5 ******
			nextObjectID = objectID.substring(0,objectID.length-1);
			nextObjectNum = parseFloat(objectID.substring(objectID.length-1,objectID.length))-1;
		}else if (objectID.length == 8){
			//******* 8 = CatQTY15 ******
			nextObjectID = objectID.substring(0,objectID.length-2);
			nextObjectNum = parseFloat(objectID.substring(objectID.length-2,objectID.length))-1;
		}else{
			//******* 9 = CatQTY115 ******
			nextObjectID = objectID.substring(0,objectID.length-3);
			nextObjectNum = parseFloat(objectID.substring(objectID.length-3,objectID.length))-1;
		}
		nextObjectID = nextObjectID + nextObjectNum;
		if (document.getElementById(nextObjectID) == null){
		}else{
			document.getElementById(nextObjectID).focus();
		}
		//****** cancel the return key event ******
		if(window.event){
			evt.returnValue = false;
			evt.cancelBubble = true;
		}else{
			evt.preventDefault();
			evt.stopPropagation();
		}

		//alert(objectID + '#' + objectID.length);
	}

	//alert('wooo'+evt.keyCode);
}






//****************************************************************
//***================== PAGE THE RESULTS ======================***
//****************************************************************
function ReOrderResults(strOrder) {
	if (document.getElementById('strOrderBy').value == strOrder){
		if (document.getElementById('strOrderAsc').value == 'ASC'){
			document.getElementById('strOrderAsc').value = 'DESC';
		}else{
			document.getElementById('strOrderAsc').value = 'ASC';
		}
	}else{
		switch (strOrder){
		case 'Description':
			document.getElementById('strOrderAsc').value = 'ASC';
			break;
		case 'Load':
			document.getElementById('strOrderAsc').value = 'ASC';
			break;
		case 'InStock':
			document.getElementById('strOrderAsc').value = 'DESC';
			break;
		case 'OnOrder':
			document.getElementById('strOrderAsc').value = 'DESC';
			break;
		case 'Price':
			document.getElementById('strOrderAsc').value = 'ASC';
			break;
		default:
			document.getElementById('strOrderAsc').value = 'ASC';
		}
		
	}
	document.getElementById('strOrderBy').value = strOrder;
	document.getElementById('frmSearch').submit();
}


//****************************************************************
//***================== PAGE THE RESULTS ======================***
//****************************************************************
function PagePrev() {
  var intRec = 0;
  intRec = document.getElementById('intStartRec').value;
  if (intRec.length==0){
		intRec = 0;
  }
  if (isNaN(intRec)){
		intRec = 0;
	}else{
		intRec = parseFloat(intRec) - 15;
	}
	document.getElementById('intStartRec').value = intRec;
	document.getElementById('frmPaged').submit();
}
function PageNext() {
  var intRec = 0;
  intRec = document.getElementById('intStartRec').value;
  if (intRec.length==0){
		intRec = 0;
  }
  if (isNaN(intRec)){
		intRec = 0;
  }else{
		intRec = parseFloat(intRec) + 15;
	}
	document.getElementById('intStartRec').value = intRec;
	document.getElementById('frmPaged').submit();
}



//****************************************************************
//***============ CHECK THE QUANTITY IN STOCK =================***
//****************************************************************
function CheckQty(strLine,intOrder,intQty) {
	//alert('in CheckQty');
	//if (intOrder >= 10){
	//	alert("Are You Sure You Wanted To Order "+intOrder+ " ?");
	//}

	//if (intOrder > intQty){
	//	alert("Only "+intQty+ " Available! - Please Call Our Office");
  	//document.getElementById(strLine).value = intQty;
	//}
	UpdateCart();
}
//****************************************************************
//***=========== VIEW CART - CANCEL ORDER LINE ================***
//****************************************************************
function CancelLine(strStockCode) {
	document.getElementById('strWhat').value='delete';
	document.getElementById('strDelete').value=strStockCode;
	document.getElementById('frmCart').submit();
}
//****************************************************************
//***====== VIEW CART - UPDATE THE CART FORM RE-SUBMIT =======***
//****************************************************************
function UpdateCart() {
	document.getElementById('frmCart').submit();
}
//****************************************************************
//***============= VIEW CART - REFRESH BUTTON =================***
//****************************************************************
function Submit_Refresh() {
	document.getElementById('strWhat').value='refresh';
	document.getElementById('frmCart').submit();
}
//****************************************************************
//***=========== VIEW CART - GOTO PLACE ORDER PAGE ============***
//****************************************************************
function Submit_PlaceOrder1() {
	document.getElementById('strWhat').value='placeorder1';
	document.getElementById('frmCart').submit();
}
//****************************************************************
//***========== VIEW CART - CANCEL ENTIRE ORDER ===============***
//****************************************************************
function Submit_Cancel() {
	document.getElementById('strWhat').value='cancel';
	document.getElementById('frmCart').submit();
}
//****************************************************************
//***======== VIEW CART - SEARCH FOR ANOTHER ITEM =============***
//****************************************************************
function Submit_AddItem() {
	document.getElementById('strWhat').value='additem';
	document.getElementById('frmCart').submit();
}





//****************************************************************
//***========= PLACE ORDER - ACCEPT AND PLACE ORDER ===========***
//****************************************************************
function Submit_PlaceOrder2() {
	document.getElementById('strWhat').value='placeorder2';
	document.getElementById('frmOrder').submit();
}
//****************************************************************
//***============= PLACE ORDER - CHANGE ORDER =================***
//****************************************************************
function Submit_Change() {
	document.getElementById('strWhat').value='change';
	document.getElementById('frmOrder').submit();
}
//****************************************************************
//***========== PLACE ORDER - CANCEL ENTIRE ORDER =============***
//****************************************************************
function Submit_CancelOrder() {
	document.getElementById('strWhat').value='cancel';
	document.getElementById('frmOrder').submit();
}





//****************************************************************
//***============ RECALCULATE THE ORDER ON THE FLY ============***
//****************************************************************
function ReCalcOrder() {
	var i = 0;
	var OrderLines = 0;
	var intQty = 0;
	var curPrice = 0;
	var curTotal = 0;
	var curNTotal = 0;
	var curVTotal = 0;
	var curGTotal = 0;
	var curCarriage = 0;

	OrderLines = document.getElementById('OrderLines').value;
 	for ( i=1; i <= OrderLines; i++){
		intQty = parseFloat(document.getElementById('OrderLineQty'+i).value);
		if (isNaN(intQty)){
			intQty = 1;
		}
		curPrice = parseFloat(document.getElementById('OrderLinePrice'+i).value);
		curTotal = intQty * curPrice;
		document.getElementById('OrderLineTotal'+i).value = ConvToCur(curTotal);
		curNTotal = curNTotal + curTotal;
	}

	curTax = document.getElementById('curPRAM_Tax').value / 100
	curNTotal = curNTotal + curCarriage;
	curVTotal = curNTotal * curTax;
	curVTotal = curVTotal;
	curGTotal = curNTotal + curVTotal;

	document.getElementById('NTotal').value = ConvToCur(curNTotal);
	document.getElementById('VTotal').value = ConvToCur(curVTotal);
	document.getElementById('GTotal').value = ConvToCur(curGTotal);
	//document.getElementById('CartTotal').value = ConvToCur(curGTotal);
}


//****************************************************************
//***============ CHECK THE QUANTITY IN STOCK =================***
//****************************************************************
function CheckQty(strLine,intOrder,intQty,intRecalcOrder) {
	//alert('in CheckQty');
	//if (intOrder >= 10){
	//	alert("Are You Sure You Wanted To Order "+intOrder+ " ?");
	//}

	//if (intOrder > intQty){
	//	alert("Only "+intQty+ " Available! - Please Call Our Office");
  	//document.getElementById(strLine).value = intQty;
	//}
}


//****************************************************************
//***======== SHOW WHICH FIELDS ON ADVANCED SEARCH ============***
//****************************************************************
function CheckTyreSearch(strTyreSearch) {
	if (strTyreSearch == "Manufacturer") {
		document.getElementById('DivManfSearch').style.display = 'block';
		document.getElementById('DivTextSearch').style.display = 'none';
	}else{
		document.getElementById('DivManfSearch').style.display = 'none';
		document.getElementById('DivTextSearch').style.display = 'block';
	}

	if (strTyreSearch == "StockCode") {
		document.getElementById('txtWidth').value = '';
		document.getElementById('txtWidth').selectedIndex = 0;
		document.getElementById('txtProfile').value = '';
		document.getElementById('txtProfile').selectedIndex = 0;
		document.getElementById('txtDiameter').value = '';
		document.getElementById('txtDiameter').selectedIndex = 0;
		document.getElementById('txtSpeed').value = '';
		document.getElementById('txtSpeed').selectedIndex = 0;
		document.getElementById('txtSearch').value = '';
		document.getElementById('txtSearch').focus();
	}else{
		document.getElementById('txtSearch').value = '';
		document.getElementById('txtSearch').focus();
	}

}
//****************************************************************
//***============ FILL IN SEARCH BOX FROM COMBOS ==============***
//****************************************************************
function BuildSearch() {
	var strWidth = "";
	var strProfile = "";
	var strDiameter = "";
	var strSearch = "";
	strWidth = document.getElementById('txtWidth').value;
	strProfile = document.getElementById('txtProfile').value;
	strDiameter = document.getElementById('txtDiameter').value;
	strSearch = strWidth + strProfile + strDiameter;
	document.getElementById('txtSearch').value = strSearch;

}
//****************************************************************
//***============ FILL IN SEARCH BOX FROM COMBOS ==============***
//****************************************************************
function ClearSearch(objectID) {

	document.getElementById('txtSearch').value = '';
	document.getElementById('strOrderBy').value = '';
	document.getElementById('strOrderAsc').value = '';

	switch (objectID){
	case 'txtSearch':
		document.getElementById('optSearchProd').value = '';
		document.getElementById('txtWidth').value = '';
		document.getElementById('txtProfile').value = '';
		document.getElementById('txtDiameter').value = '';
		document.getElementById('txtSpeed').value = '';
		break;
	case 'optSearchProd':
		document.getElementById('txtWidth').value = '';
		document.getElementById('txtProfile').value = '';
		document.getElementById('txtDiameter').value = '';
		document.getElementById('txtSpeed').value = '';
		break;
	default:
		document.getElementById('optSearchProd').value = '';
		break;
	}
}
//****************************************************************
//***===== CLOSE ERROR BOX DIV AFTER THEY CLICKED BUTTON ======***
//****************************************************************
function ResetAfterError() {
	document.getElementById('txtSearch').value = '';
	document.getElementById('ErrorSearch').style.visibility="hidden";
	document.getElementById('txtSearch').focus();
}


//****************************************************************
//***========= FILL IN SEARCH COMBOS FROM TEXT BOX ============***
//****************************************************************
function Reverse() {
	var strWidth = "";
	var strProfile = "";
	var strDiameter = "";
	var strSpeed = "";
	var strSearch = "";
	var strUpper = "";

	strSearch = document.getElementById('txtSearch').value;
	strSearch = strSearch.toUpperCase();

	if (strSearch.length != 0){
		document.getElementById('txtSearch').value = strSearch;

		strSearch = StripChars(strSearch);
		strWidth = strSearch.substring(0,3);
		strProfile = strSearch.substring(3,5);
		strDiameter = strSearch.substring(5,7);
		strSpeed = strSearch.substring(7,8);

		if (parseFloat(strProfile) < 25){
			strDiameter = strProfile;
			strProfile = "";
		}		
		document.getElementById('txtWidth').value = strWidth;
		document.getElementById('txtProfile').value = strProfile;
		document.getElementById('txtDiameter').value = strDiameter;
		document.getElementById('txtSpeed').value = strSpeed;
	}
}

//****************************************************************
//***=============== FILL IN DELIVERY ADDRESS =================***
//****************************************************************
function FillDelivery(strFrom, strDel_Code) {

	SelectBox = document.getElementById('optBranchCode');
	SelectBranch = document.getElementById('optBranchSelect');

	if (strFrom=='strBranchCode'){
	  strDel_Code = "000" + strDel_Code;
	  strDel_Code = strDel_Code.substring(strDel_Code.length-3,strDel_Code.length);
	}else{
		strDel_Code = SelectBranch.options[SelectBranch.selectedIndex].value;
  	document.getElementById('strBranchCode').value = strDel_Code;
	}

	for (var i=0; i < SelectBox.length; i++) {
		strOption = SelectBox.options[i].value;
		strOptionText = SelectBox.options[i].text;
		if (strDel_Code == strOption){
			strOptionText = strOptionText.replace(/#/g,'\n');

			strOptionText = strOptionText + "\n\nBranch Code: "+ strDel_Code;
			document.getElementById('strOrderDetails').value = strOptionText;

			SelectBranch.selectedIndex = i;
			//alert(strDel_Code +":"+ strBranchCode);
		}
	}



	//alert(SelectBox.length);
	//alert(box.options[box.selectedIndex].value);
	//document.getElementById('txtSearch').value = strSearch;
}












//****************************************************************
//***============ POPUP WINDOW & PASS FILENAME ================***
//****************************************************************
function ShowPic(strPicName) {
	var filename = 'ShowPic.htm' + '?pic='+strPicName;
	var winl = (screen.width - 640) / 2;
	var wint = (screen.height - 480) / 2;
	var newwindow = window.open(filename,'popup','width=640, height=480, left='+winl+', top='+wint+', channelmode=no, directories=no, fullscreen=no, location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no');

	if (window.focus) {
		newwindow.focus()
	}

}



function Search(strValue) {
	document.getElementById('txtSearch').value = strValue+"";
	document.getElementById('txtType').value = "Category";
	frmSearch.submit();
}
function SetText() {
	document.getElementById('txtType').value = "Keyword";
}




//****************************************************************
//***=========== RETURN ONLY ALPHANUMERIC CAPS ================***
//****************************************************************
function StripChars(strString) {
	var validchars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var strNew = ""
	for (var i=0; i < strString.length; i++) {
		var letter = strString.charAt(i).toUpperCase();
	    	if (validchars.indexOf(letter) != -1)
			strNew = strNew + letter;
	}
	return strNew;
}


//****************************************************************
//***======= CONVERT FLOATING POINT TO A CURRENCY STRING ======***
//****************************************************************
function ConvToCur(fltValue){
	<!---****** ACCEPTS fltValue AS Numeric - RETURNS strValue AS STRING (CURRENCY STYLE) ******--->
	fltValue = (Math.round(fltValue * 100)) / 100;
	strValue = fltValue.toString();

	if (strValue.length == 0){
		return strCurrency + "0.00";
	}else{
		if (strValue.indexOf(".") == -1){
			strValue = strValue + ".00";
		}else{
			if (strValue.indexOf(".") == strValue.length - 1){
				strValue = strValue + "00";
			}else if (strValue.indexOf(".") == strValue.length - 2){
				strValue = strValue + "0";
			}
		}

		if (strValue.indexOf(strCurrency) == -1){
			strValue = strCurrency+strValue;
		}

		return strValue
	}
}


//****************************************************************
//***======= CONVERT CURRENCY STRING TO A FLOATING POINT ======***
//****************************************************************
function ConvToFlt(strValue){
	<!---****** ACCEPTS strValue AS STRING (CURRENCY FORMAT) - RETURNS fltValue AS FLOATINGPOINT) ******--->
	if (strValue.charAt(0) == strCurrency){
		strValue = strValue.substring(1,strValue.length);
	}
	fltValue = parseFloat(strValue);
	return fltValue
}


//****************************************************************
//***================ ROUNDING FUNCTION =======================***
//****************************************************************
function RoundNumber(curValue,intDP) {
	if (curValue > 8191 && curValue < 10485) {
		curValue = curValue-5000;
		var newnumber = Math.round(curValue*Math.pow(10,intDP))/Math.pow(10,intDP);
	} else {
		var newnumber = Math.round(curValue*Math.pow(10,intDP))/Math.pow(10,intDP);
	}
	return newnumber;
}





//****************************************************************
//***============= CONVERT CHARS TO UPPERCASE =================***
//****************************************************************
//*** ASP CODE = OnKeyUp='this.value = UpperCase(this.value);' ***
function UpperCase(strKey)
{
	strKey = strKey.toUpperCase();
	return strKey;
}



//****************************************************************
//***================ DISPLAY FLASHING TEXT ===================***
//****************************************************************
function FlashingText(strFlashingText){
	var ctext = strFlashingText;
	if(navigator.appName == "Netscape") {
		document.write('<layer id="c"><center>'+ctext+'</center></layer><br>');
	}
	if (navigator.appVersion.indexOf("MSIE") != -1){
		document.write('<div id="c"><center>'+ctext+'</center></div>');
	}
	timeFlash = setInterval("ChangeColour()",300);
}
function ChangeColour(){ 
	strColour = document.all.c.style.color;
	if (strColour == "black"){
		strColour = "red";
	}else{
		strColour = "black";
	}

	if(navigator.appName == "Netscape") {
		document.c.document.write('<center><font color="'+strColour);
		document.c.document.write('">'+ctext+'</font></center>');
		document.c.document.close();
	}
	else if (navigator.appVersion.indexOf("MSIE") != -1){
		document.all.c.style.color = strColour;
	}
}


//****************************************************************
//***============= ADD TO FAVORITES / BOOKMARK ================***
//****************************************************************
function AddToFavorites(){

	if (window.sidebar) {
		//****** firefox / mozilla ******
		window.sidebar.addPanel(document.title, location.href,"");	
	} else if( window.external ) { 
		//****** internet explorer ******
		window.external.AddFavorite( location.href, document.title);
	} else {
		//****** opera / safari - dont care ******
	}
}


//****************************************************************
//***================== PRINT THIS PAGE =======================***
//****************************************************************
function PrintThisPage(){
	window.print();
}
