function action(url,formObj) {
	if (formObj == null)
		formObj = document.inquiry;
	if (displayAlert(formObj)) {
		formObj.action=url ;
		formObj.submit();
	}
}

function actionHighlight(url,addedUrl,formObj,frameName) {
	if (formObj == null)
		formObj = document.inquiry;
	if (frameName == null)
		frameName = "inquiry_iframe";
	if (displayAlert(formObj)) {
		formObj.action=url ;
		formObj.target=frameName;
		formObj.submit();
		replaceCheckbox(formObj,addedUrl);
	}
}

function replaceCheckbox(formObj,addedUrl){
	var num = 0;
	for (var i=formObj.length-1;i>=0;i--) {
		var element = formObj.elements[i] ;
		if ((element.type=='checkbox') && element.disabled==false&&element.checked==true ) {
			element.checked=false ;
			var parent = element.parentNode;
			var innerHtml ="<span class=\"small\"><a href=\""+addedUrl+"\" >Added<br>to<br>basket</a></span>";
			parent.align="center";
			parent.innerHTML=innerHtml;
			num++;
		} 
	}
	//alert (num);
	if (num > 0)
		updateInqNowAction();
}

function updateInqNowAction() {
	document.getElementById("inquirenow").style.display = 'none';
	document.getElementById("inquirenow_bottom").style.display = 'none';
	document.getElementById("inquirenow_basket").style.display = 'inline';
	document.getElementById("inquirenow_bottom_basket").style.display = 'inline';
}

function actionInquiry(url) {
	if (displayAlertInquiry('inquiry')) {
    	document.inquiry.action=url ;
        document.inquiry.submit();
	}
}
function inquireNow(url){
	document.inquiry.action=url ;
	document.inquiry.submit();
}

function displayAlert(formObj) {
	// get the size of the checkboxes
	if (formObj == null)
		formObj = document.inquiry;
	var hasMoreCheckBox="false";
	for (var i=0;i<formObj.length;i++) {
		if ((formObj.elements[i].type=='checkbox' || formObj.elements[i].type=='radio') && formObj.elements[i].disabled==false) {
			hasMoreCheckBox="true";
			break;
		} 
	}
	if ( (formObj.elements.length + elementCount) > 0 &&  hasMoreCheckBox=="true") {
		for (var i=0;i<formObj.length;i++) {
			var e = formObj.elements[i];
			if(e.name!="all_ticker" && e.name!="toggleSA" && (e.type == "checkbox" || e.type == "radio")) {
				if(e.checked == true) {
					return true;
				}
			}
		}
		alert(msgstr);
		return false;
	} else {
		formObj.blur();
	}
}

function displayAlertInquiry() {
	// get the size of the checkboxes
    var hasMoreCheckBox="false";
    for (var i=0;i<document.inquiry.length;i++) {
    	if (document.inquiry.elements[i].type=='checkbox' && document.inquiry.elements[i].disabled==false) {
        	hasMoreCheckBox="true";
            break;
		}
	}
	if ((document.inquiry.elements.length + elementCount) > 0 &&  hasMoreCheckBox =="true") {
		var j=0;
        for (var k=0;k<document.inquiry.length;k++) {
        	var l = document.inquiry.elements[k];
            if(l.type == "checkbox") {
            	if(l.checked == true) {
                	j++;
                    if (j > 151) {
						alert("Sorry, you have exceeded the limit. The number of products\nand/or suppliers that can be included in an Inquiry is 150.");
                        return false;
					}
				}
			}
		}
	}
	if ((document.inquiry.elements.length + elementCount) > 0 &&  hasMoreCheckBox=="true") {
    	for (var i=0;i<document.inquiry.length;i++) {
        	var e = document.inquiry.elements[i];
            if(e.type == "checkbox") {
            	if(e.checked == true) {
                	return true;
				}
			}
		}
		alert(msgstr);
        return false;
	} else {
		inquiry.blur();
	}
}

function actionInvokeSingle(url, id, formObj) {
	if (formObj == null)
		formObj = document.inquiry;
	for (var i=0;i<formObj.length;i++) {
		var e = formObj.elements[i];
		if((e.name =="all_ticker" || e.name =="product_id" || e.name =="supplier_id") && e.type == "radio") {
			if(e.value == id)
				e.checked = true;
		}
	}
	if (displayAlert(formObj)) {
		formObj.action=url ;
		formObj.submit();
	}
}

function allInquireNow(checkBoxName,inqpathObj,inqpathVal,url) {
	var oEl=document.getElementsByName(checkBoxName);
	for (var i=0;i<oEl.length;i++){
		oEl.item(i).checked = true;
	}
	inqpathObj.value = inqpathVal;
	actionInqNow(url);	
}

function actionInqNow(url,formObj,inqPathVal) {
	if (formObj == null)
		formObj = document.inquiry;	
	var basketCount = parseInt(formObj.addedTobasketCount.value);
	var itemCount = parseInt(formObj.itemCount.value);
	var flag = false;
	if(itemCount>0 && (!isFromShowroom)) {
		itemCount = itemCount-1;
	}
	//check all_ticker's state		
	for (var i=0;i<formObj.length;i++) {
		var e = formObj.elements[i];
		if((e.name =="all_ticker" || e.name =="product_id" || e.name =="supplier_id") && e.type == "checkbox") {
			if(e.checked == true)
				flag = true;
		}
	}
	var urlWithInqPath =  url;
	if (inqPathVal!=null) {
		var saChkBox = formObj.toggleSA;
		if (saChkBox!=null && saChkBox!=undefined && saChkBox.checked) {
			inqPathVal = inqPathVal + "SA";
		}
		urlWithInqPath = urlWithInqPath + (inqPathVal.indexOf("&inquiry_path=")>=0?"":"&inquiry_path=") + inqPathVal;
	}
	if(flag){
		if(basketCount != itemCount){
			if (displayAlert(formObj)) {
				formObj.action=urlWithInqPath ;
				formObj.target='';
				formObj.submit();
			}	
		}else{//added for MR12877
		    formObj.action=urlWithInqPath ;
			formObj.target='';
			formObj.submit();
		}
	} else {
		if(basketCount ==0){
			if (displayAlert(formObj)) {
				formObj.action=urlWithInqPath ;
				formObj.target='';
				formObj.submit();
			}	
		}else{ //added for MR12877
				formObj.action=urlWithInqPath ;
				formObj.target='';
				formObj.submit();
		}
	}
}

function actionInqSingleInqPath(url,inqPathVal,id,formObj) {
	actionInqSingle(url, id, formObj, inqPathVal);
}
function actionInqSingle(url, id, formObj,inqPathVal) {
	if (formObj == null)
		formObj = document.inquiry;
	for (var i=0;i<formObj.length;i++) {
		var e = formObj.elements[i];
		if((e.name =="product_id" || e.name =="supplier_id") && e.type == "checkbox") {
			if(e.value == id)
				e.checked = true;
		}
	}
	var basketCount = parseInt(formObj.addedTobasketCount.value);
	var itemCount = parseInt(formObj.itemCount.value);	
	var flag = false;
	if(itemCount>0 && (!isFromShowroom)) {
		itemCount = itemCount-1;
	}
	//check all_ticker's state		
	for (var i=0;i<formObj.length;i++) {
		var e = formObj.elements[i];
		if((e.name =="all_ticker" || e.name =="product_id" || e.name =="supplier_id") && e.type == "checkbox") {
			if(e.checked == true)
				flag = true;
		}
	}
	var urlWithInqPath =  url;
	if (inqPathVal!=null) {
		var saChkBox = formObj.toggleSA;
		if (saChkBox!=null && saChkBox!=undefined && saChkBox.checked) {
			inqPathVal = inqPathVal + "SA";
		}
		urlWithInqPath = urlWithInqPath + (inqPathVal.indexOf("&inquiry_path=")>=0?"":"&inquiry_path=") + inqPathVal;
	}
	if(flag){
		if(basketCount != itemCount){
			if (displayAlert(formObj)) {
				formObj.action=urlWithInqPath ;
				formObj.submit();
			}	
		}else{//added for MR12877
		    formObj.action=urlWithInqPath ;
			formObj.submit();
		}
	} else {
		if(basketCount ==0){
			if (displayAlert(formObj)) {
				formObj.action=urlWithInqPath ;
				formObj.submit();
			}	
		}else{ //added for MR12877
			formObj.action=urlWithInqPath ;
			formObj.submit();
		}
	}
}



//actionInqNowAuto - Added for MR#4556
function actionInqNowAuto(url,viewBasketUrl,formObj,inqPathVal) {
	if (formObj == null)
		formObj = document.inquiry;
	var basketCount = parseInt(formObj.addedTobasketCount.value);
	var itemCount = parseInt(formObj.itemCount.value);	
	var flag = false;
	if(itemCount>0 && (!isFromShowroom)) {
		itemCount = itemCount-1;
	}
	//check all_ticker's state		
	for (var i=0;i<formObj.length;i++) {
		var e = formObj.elements[i];
		if((e.name =="all_ticker" || e.name =="product_id" || e.name =="supplier_id") && (e.type == "checkbox" || e.type == "radio")) {
			if(e.checked == true)
				flag = true;
		}
	}	
	if( !flag || basketCount == itemCount){		
		window.location=viewBasketUrl;
		return;
	}
	var urlWithInqPath =  url;
	if (inqPathVal!=null) {
		var saChkBox = formObj.toggleSA;
		if (saChkBox!=null && saChkBox!=undefined && saChkBox.checked) {
			inqPathVal = inqPathVal + "SA";
		}
		urlWithInqPath = urlWithInqPath + (inqPathVal.indexOf("&inquiry_path=")>=0?"":"&inquiry_path=") + inqPathVal;
	}
	if(flag){
		if(basketCount != itemCount){
			if (displayAlert(formObj)) {
				formObj.action=urlWithInqPath ;
				formObj.target='';
				formObj.submit();
			}	
		}
	} else {
		if(basketCount ==0){
			if (displayAlert(formObj)) {
				formObj.action=urlWithInqPath ;
				formObj.target='';
				formObj.submit();
			}	
		}
	}
}

/* from pagination.jsp */
var winConfirm = null; 
var buttonNum = 0; 
var doAction = "No"; 
var url = "";
var inquiryFormName = "inquiry";
var inquiryForm;
var objColls;
var addInquireURL = "";

function newConfirm(url1,imgSrc) 
{
	var windowWidth = 420; 
	var windowHeight = 105; 
	var locX = ( screen.width - windowWidth ) / 2; 
	var locY = ( screen.height - windowHeight ) / 2; 
	var windowFeatures = "width=" + windowWidth 
	+ ",height=" + windowHeight 
	+ ",screenX=" + locX 
	+ ",screenY=" + locY 
	+ ",titlebar=no" 
	+ ",statusbar=no"
	+ ",left=" + locX 
	+ ",top=" + locY; 

	if ((winConfirm != null) && !winConfirm.closed) { 
		winConfirm.close(); 
	} 
	winConfirm = window.open("","winConfirm",windowFeatures); 
	var theHTML = '<html><head><title>Confirm Window</title>'
    + '<style>'
    + '.smallfont {font: 12px Arial,sans-serif;}'
    + '.btnfont {font: 12px Arial,sans-serif;}'
    + '</style></head>'
	+ '<body bgcolor="#EBE9D6">'
	+ '<center><table cellpadding="0" cellspacing="0" border="0" width="100%">'
	+ '<tr><td><img src="'+ imgSrc +'"></td>' 
	+ '<td><span class=smallfont>&nbsp;&nbsp;Would you like to add the selected item(s) to your inquiry basket?</span>'
	+ '</td></tr></table><br>' 
	+ '<form name="buttonForm" >' 
	+ '<input class="btnfont" type="button" value="   Yes   "' 
	+ ' onclick="opener.buttonClicked(1);self.close();">' 
	+ '&nbsp;&nbsp;' 
	+ '<input class="btnfont" type="button" value="    No    "' 
	+ ' onclick="opener.buttonClicked(0);self.close();">' 
	+ '</form></center></body></html>'; 
	winConfirm.document.writeln(theHTML); 
	winConfirm.document.close();
    return true;
}

function buttonClicked(buttonChoice) 
{ 
	switch(buttonChoice) 
	{ 
		case 1:
			doAction = "Yes";
			action(addInquireURL);
			break;
		default: 
			doAction = "No";
			clearCheckedForBasket();
			nextPage(url);
	} 
}

function validate() {
	if (doAction == "Yes") {
		return true;  
	 } else {
		return false;
	 } 
} 

function loseSelectBasketConfirm(url2,imgSrc,addURL) {
	url = url2;
	addInquireURL = addURL;
	//important,if no add basket button,then return directly
	//the variant hasAddBasketButton if definded in the jsp page which has the ADD TO BASKET button
	if(typeof(hasAddBasketButton)=='undefined') {
		location.href = url2;
	} else {
		if(hasCheckedForBasket()==true) {
			newConfirm(url2,imgSrc);
		} else {
			nextPage(url2);
		}
	}
	return false;
}

function nextResult(nextPageURL) {
	document.searchbox.action=nextPageURL;
	document.searchbox.submit();
}

function nextPage(url1) {
	document.inquiry.action=url1;
	document.inquiry.submit();
}

function clearCheckedForBasket(){
	objColls=inquiryForm=document.getElementsByName(inquiryFormName);
	if(objColls.length>0)
		inquiryForm=objColls[0];
	for (var i=0;i<inquiryForm.elements.length;i++) {
		var e=inquiryForm.elements[i];
		if ((e.type=='checkbox') && e.disabled==false && e.checked==true) {
			e.checked=false;
		} 
	}
}

/* Special Functions for new Hybrid TP Page */
function tpAddToBasket(url) {
	var prodBoxes = document.getElementsByName('product_id');
	var checkboxDivider = document.getElementsByName("upperCheckBoxes");
	var maxBoxesToCheck = prodBoxes.length;
	if (checkboxDivider!=null && checkboxDivider.length>0) {
		maxBoxesToCheck = parseInt(checkboxDivider.item(0).value);
	}
	var pickedUnverified = false;
	var atLeastOneProdSelected = false;
	for (var i=0;i<prodBoxes.length;i++) {
		if (i < maxBoxesToCheck) {
			if (prodBoxes.item(i).checked==true) {
				atLeastOneProdSelected = true;
			}
		}
		if (i >= maxBoxesToCheck) {
			if (prodBoxes.item(i).checked==true) {
				prodBoxes.item(i).checked = false;
				pickedUnverified = true;
			}
		}		
	}
	if (atLeastOneProdSelected) {
		if (pickedUnverified) {
			alert('Note: only products from Verified Suppliers may be added to your Inquiry Basket.');
		}
		document.inquiry.action=url ;
		document.inquiry.submit();
	} else {
		alert("You haven't selected any products. Please first select a product, then click the \"Add to Basket\" button.\nNote: only products from Verified Suppliers may be added.");
	}
}
function tpInquireNow(url,upperOrLower,viewBasketUrl,numItemsInBasket,inqPathVal) {
	var prodBoxes = document.getElementsByName('product_id');
	var checkboxDivider = document.getElementsByName("upperCheckBoxes");
	var maxBoxesToCheck = prodBoxes.length;
	if (checkboxDivider!=null && checkboxDivider.length>0) {
		maxBoxesToCheck = parseInt(checkboxDivider.item(0).value);
	}
	var pickedUnverified = false;
	var pickedVerified = false;
	var atLeastOneProdSelected = false;
	for (var i=0;i<prodBoxes.length;i++) {
		if (i < maxBoxesToCheck) {
			if (prodBoxes.item(i).checked==true) {
				if (upperOrLower == 'UPPER') {
					atLeastOneProdSelected = true;
				} else if (upperOrLower == 'LOWER') {
					pickedVerified = true;
					prodBoxes.item(i).checked = false;
				}
			}
		} else if (i >= maxBoxesToCheck) {
			if (prodBoxes.item(i).checked==true) {
				if (upperOrLower == 'UPPER') {
					pickedUnverified = true;
					prodBoxes.item(i).checked = false;
				} else if (upperOrLower == 'LOWER') {
					atLeastOneProdSelected = true;
				}
			}
		}		
	}
	if (upperOrLower=="LOWER" && document.inquiry.toggleSA!=null && document.inquiry.toggleSA!=undefined && document.inquiry.toggleSA.checked) {
		document.inquiry.toggleSA.checked = false;
	}
	var urlWithInqPath =  url;
	if (inqPathVal!=null) {
		if (upperOrLower=="UPPER") {
			var saChkBox = document.inquiry.toggleSA;
			if (saChkBox!=null && saChkBox!=undefined && saChkBox.checked) {
				inqPathVal = inqPathVal + "SA";
			}
		}
		urlWithInqPath = urlWithInqPath + (inqPathVal.indexOf("&inquiry_path=")>=0?"":"&inquiry_path=") + inqPathVal;
	}
	if (atLeastOneProdSelected) {
		document.inquiry.action=urlWithInqPath ;
		document.inquiry.submit();
	} else {
		if (viewBasketUrl!=null && numItemsInBasket>0) {
			window.location.href = viewBasketUrl;
		} else {
			alert('Please select an  item before clicking\nthe "Inquire Now" or "Add to Inquiry Basket"\n buttons.');
		}
	}
}

function tpInqNowSingle(url, id, inqPathVal) {
	var prodBoxes = document.getElementsByName('product_id');
	var checkboxDivider = document.getElementsByName("upperCheckBoxes");
	var maxBoxesToCheck = prodBoxes.length;
	if (checkboxDivider!=null && checkboxDivider.length>0) {
		maxBoxesToCheck = parseInt(checkboxDivider.item(0).value);
	}
	var pickedUpper = false;
	var pickedLower = false;
	var foundInqClickAt = 0;
	for (var i=0;i<prodBoxes.length;i++) {
		if (prodBoxes.item(i).value==id) {
			prodBoxes.item(i).checked=true;
			foundInqClickAt = i;
		}
	}
	if (foundInqClickAt < maxBoxesToCheck) {
		pickedUpper = true;
	} else if (foundInqClickAt >= maxBoxesToCheck) {
		pickedLower = true;
	}
	if (pickedLower && document.inquiry.toggleSA!=null && document.inquiry.toggleSA!=undefined && document.inquiry.toggleSA.checked) {
		document.inquiry.toggleSA.checked = false;
	}
	for (var j=0;j<prodBoxes.length;j++) {
		if (prodBoxes.item(j).checked==true) {
			if ((j < maxBoxesToCheck) && (foundInqClickAt >= maxBoxesToCheck)) {
				prodBoxes.item(j).checked = false;
			} else if ((j >= maxBoxesToCheck) && (foundInqClickAt < maxBoxesToCheck)) {
				prodBoxes.item(j).checked = false;
			}
		}

	}
	actionInqSingle(url, id, null, inqPathVal);
}

