var responseDataArr = null;	
var formWaitingToBeSubmitted = null;
var ajaxInProcess = false;

var inputAccountArr = new Array();
inputAccountArr[0] = "FirstName";
inputAccountArr[1] = "LastName";
inputAccountArr[2] = "Role";
inputAccountArr[3] = "RoleOther";
inputAccountArr[4] = "Company";
inputAccountArr[5] = "CompanyType";
inputAccountArr[6] = "CompanyTypeOther";
inputAccountArr[7] = "CompanyWebsite";
inputAccountArr[8] = "Address1";
inputAccountArr[9] = "Address2";
inputAccountArr[10] = "City";
inputAccountArr[11] = "YourState";
inputAccountArr[12] = "ZipCode";
inputAccountArr[13] = "Country";
inputAccountArr[14] = "Phone";
inputAccountArr[15] = "HowDidYouHear";
inputAccountArr[16] = "HowDidYouHearOther";

var inputAddressArr = new Array();
inputAddressArr[0] = "City";
inputAddressArr[1] = "YourState";
inputAddressArr[2] = "Country";

function xmlhttpPost(postData, lookupType) {

	startUpdate(postData,lookupType);

	var xmlHttpReq;

	if (window.XMLHttpRequest) { xmlHttpReq = new XMLHttpRequest(); }
	else if (window.ActiveXObject) { xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); }

	if(xmlHttpReq) {
		if(lookupType=="account"){
			xmlHttpReq.open("POST", "/ProductDetail/ajax_AccountLookup.asp", true);
		}
		else if(lookupType=="address"){
			xmlHttpReq.open("POST", "/ProductDetail/ajax_AddressLookup.asp", true);
		}
		xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlHttpReq.onreadystatechange = function () {
		    if (xmlHttpReq.readyState == 4) {
                // modify this to pause long enough for "updating" message to appear and then hide it once the fields have been filled
		        //alert(xmlHttpReq.responseText);
		        if (lookupType == "account") {
		            setTimeout("updateInfo('" + escape(xmlHttpReq.responseText) + "','" + lookupType + "')", 1000);
		        }
		        else if (lookupType == "address") {
		            setTimeout("updateInfo('" + escape(xmlHttpReq.responseText) + "','" + lookupType + "')", 1000);
		        }
		    }
		}
		if(lookupType=="account"){
			ajaxInProcess = true;
			xmlHttpReq.send("email="+postData);			
		}
		else if(lookupType=="address"){
			ajaxInProcess = true;
			xmlHttpReq.send("zipcode="+postData);
		}
	}
}

function checkFormSubmit(form){
	if(ajaxInProcess){
		formWaitingToBeSubmitted = form;
	}
	else{
		formWaitingToBeSubmitted = null;
		form.submit();
	}
}

function checkNotYouSubmit(form){
	if(getElement("action") != null){
		getElement("action").value = "notyou";	
	}
	checkFormSubmit(form);
}


function checkEmailAddress(EmailAddress){
	if(!InVirtualSite && EmailAddress != ""){
		xmlhttpPost(EmailAddress,"account");
	}
}

function checkZipCode(ZipCode){
	if(!InVirtualSite  && ZipCode != ""){
		xmlhttpPost(ZipCode,"address");
	}
}

function startUpdate(postData,lookupType){
	if(getElement("boxtext") != null){
		
		if(lookupType == "account"){
			getElement("boxtext").innerHTML = "Looking up contact information for<br><b>" + postData + "</b>";
		}
		else if(lookupType == "address"){
			getElement("boxtext").innerHTML = "Looking up address information for<br><b>" + postData + "</b>";
		}
	}
	showBox();
}

function stopUpdate() {
	var form;
	hideBox();
	ajaxInProcess = false;
	if(formWaitingToBeSubmitted != null){
		form = formWaitingToBeSubmitted;
		formWaitingToBeSubmitted = null;
		form.submit();		
	}
}

function updateInfo(responseText, lookupType) {

    responseText = unescape(responseText);

	responseDataArr = null;
	responseDataArr = responseText.split("\t");
	
	var inputFieldArr = null;
	
	if(lookupType == "account"){
		inputFieldArr = inputAccountArr;
	}
	else if(lookupType == "address"){
		inputFieldArr = inputAddressArr;
	}
	
	var InputName;
	var InputElement;
	var InputValue;

	if (responseDataArr != null && responseDataArr != '' && inputFieldArr != null) {

	    document.contactform.UserType[1].checked = true;
	    toggleContactInformation();
	    toggleExistingUserInformation();
		
		var arLen=inputFieldArr.length;
		
		for ( var i=0, len=arLen; i<len; ++i ){
			
			InputName = inputFieldArr[i];
			InputElement = getElement(InputName);
			InputValue = responseDataArr[i];
			
			if(InputValue != undefined && InputValue != ''){
			
				if(InputElement != null && InputValue != undefined && InputElement.type != undefined){
					
					if(InputElement.type == "text"){
						InputElement.value = InputValue;
					}
					else if(InputElement.type == "select-one"){
						InputElement.value = InputValue;
					}
					else{
						//alert(InputName+".type = "+InputElement.type);
					}
				}
				else{
					InputElement = getElement(InputName+"1");
					if(InputElement != null){
						if(InputElement.type == "radio"){
							//alert(InputName+"1.type = radio");
							for ( var j=1; j<20; ++j ){
								InputElement = getElement(InputName+j);
								if(InputElement != null){
									if(InputElement.value == InputValue){
										//alert(InputName+j+" checked ("+InputElement.value+" = "+InputValue+")");
										InputElement.checked = true;
									}
									else{
										//alert(InputName+j+" unchecked");
										InputElement.checked = false;
									}
								}
								else{
									//alert(InputName+j+" is null");
									break;
								}
							}
						}
						else{
							//alert(InputName+"1.type = "+InputElement.type);
						}
					}
					else{
						//alert("element '"+InputName+"' not found");	
					}
				}
			}
		}
	}
	stopUpdate();
}





function initializeExtraRequestInformation() {
    if (getElement("ShowAllRequestInfo") != null) {
        if (getElement("ShowAllRequestInfo").value == "true") {
            showExtraRequestInformation();
        }
        else {
            hideExtraRequestInformation();
        }
    }
}

function toggleExtraRequestInformation() {
	if (getElement("ShowAllRequestInfo") != null) {
        if (getElement("ShowAllRequestInfo").value == "true") {
            hideExtraRequestInformation();
        }
        else {
            showExtraRequestInformation();
        }
    }
}


function showExtraRequestInformation() {
    showElement("trLikeTo");
   //showElement("trReceiveCompetitiveQuotes");
    showElement("trQuantity");
    showElement("trUOM");
    showElement("trYearlyUsage");
    showElement("trTimeFrame");

    var o = getElement("ToggleExtraRequestInfo");
    if (o != null) {
        o.innerHTML = "<span>hide optional details</span>";
        o.className = "IconLink icon_delete"
    }

    if (getElement("ShowAllRequestInfo")) { getElement("ShowAllRequestInfo").value = "true"; }
    
}

function hideExtraRequestInformation() {
    hideElement("trLikeTo");
   // hideElement("trReceiveCompetitiveQuotes");
    hideElement("trQuantity");
    hideElement("trUOM");
    hideElement("trYearlyUsage");
    hideElement("trTimeFrame");

    var o = getElement("ToggleExtraRequestInfo");
    if (o != null) {
        o.innerHTML = "<span>show optional details</span>";
        o.className = "IconLink icon_add"
    }

    if (getElement("ShowAllRequestInfo")) { getElement("ShowAllRequestInfo").value = "false"; }
}


var toggleExistingInit = 0;

function initializeContactInformation() {

    if (document.contactform.UserType[0].checked) {
        document.contactform.ShowAllNewUserContactInfo.value = "false";
        initializeNewUserInformation();
    } else {
        document.contactform.ShowAllExistingUserContactInfo.value = "false";
        initializeExistingUserInformation();
    }
    
}

function toggleContactInformation() {

    if (document.contactform.UserType[0].checked) {
        document.contactform.ShowAllNewUserContactInfo.value = "false";
        toggleNewUserInformation();
    } else {
        document.contactform.ShowAllExistingUserContactInfo.value = "false";
        toggleExistingUserInformation();
    }
    
}

function initializeNewUserInformation() {
	showNewUserInformation()
    if (getElement("ShowAllNewUserContactInfo") != null) {
        if (getElement("ShowAllNewUserContactInfo").value == "true") {
            showExtraUserInformation();
        }
        else {
            hideExtraUserInformation();
        }
    }
}

function toggleNewUserInformation() {

    document.contactform.UserType[0].checked = true;

	showNewUserInformation();

    if (document.contactform.ShowAllNewUserContactInfo.value == "true") {
		hideExtraUserInformation();
        document.contactform.ShowAllNewUserContactInfo.value = "false";
    } else {
		showExtraUserInformation();
        document.contactform.ShowAllNewUserContactInfo.value = "true";
    }
}

function showNewUserInformation(){
	showElement("trFirstName");
    showElement("trLastName");
    showElement("trCompany");
    showElement("trCompanyType");
    showElement("trZipCode");
    showElement("trPhone");
}
function hideNewUserInformation(){
	hideElement("trFirstName");
    hideElement("trLastName");
    hideElement("trCompany");
    hideElement("trCompanyType");
    hideElement("trZipCode");
    hideElement("trPhone");
}

function initializeExistingUserInformation() {
    if (getElement("ShowAllExistingUserContactInfo") != null) {
        if (getElement("ShowAllExistingUserContactInfo").value == "true") {
            showExtraUserInformation();
        }
        else {
             hideExtraUserInformation();
        }
    }
}

function toggleExistingUserInformation() {
	if (getElement("ShowAllExistingUserContactInfo") != null) {
        if (getElement("ShowAllExistingUserContactInfo").value == "true") {
            hideExtraUserInformation();
			getElement("ShowAllExistingUserContactInfo").value = "false";
        }
        else {
            showExtraUserInformation();
			getElement("ShowAllExistingUserContactInfo").value = "true";
        }
    }
}

function showExtraUserInformation(){
	var o;
	if(document.contactform.UserType[1].checked){
		o = getElement("existingUserToggle");
		showElement("trExistingUserToggle");
        hideElement("trNewUserToggle");
	}
	else{
		o = getElement("newUserToggle");		
        showElement("trNewUserToggle");
		hideElement("trExistingUserToggle");
	}
	showNewUserInformation()
    showElement("trRole");
    showElement("trCompanyWebsite");
    showElement("trAddress1");
    showElement("trAddress2");
    showElement("trCity");
    showElement("trYourState");
    showElement("trCountry");
    showElement("trHowDidYouHear");
	if (o) {
		o.innerHTML = "<span>hide optional details</span>";
		o.className = "IconLink icon_delete"
		//o.href = "javascript:hideExtraUserInformation();"
	}
}
function hideExtraUserInformation(){
	var o;
	if(document.contactform.UserType[1].checked){
		o = getElement("existingUserToggle");
		showElement("trExistingUserToggle");
        hideElement("trNewUserToggle");
		hideNewUserInformation()
	}
	else{
		o = getElement("newUserToggle");		
        showElement("trNewUserToggle");
		hideElement("trExistingUserToggle");
	}	
    hideElement("trRole");
    hideElement("trCompanyWebsite");
    hideElement("trAddress1");
    hideElement("trAddress2");
    hideElement("trCity");
    hideElement("trYourState");
    hideElement("trCountry");
    hideElement("trHowDidYouHear");
	if (o) {
		o.innerHTML = "<span>show optional details</span>";
		o.className = "IconLink icon_add"
		//o.href = "javascript:showExtraUserInformation();"
	}
}

/*********************/
/* DEPRECATED


function initializeExtraContactInformation(){
	if(getElement("ShowAllContactInfo") != null){
		if(getElement("ShowAllContactInfo").value == "true"){
			showExtraContactInformation();
		}
		else{
			hideExtraContactInformation();
		}
	}
}

function toggleExtraContactInformation(){
	if(getElement("ShowAllContactInfo") != null){
		if(getElement("ShowAllContactInfo").value == "true"){
			hideExtraContactInformation();
		}
		else{
			showExtraContactInformation();
		}
	}
}


function showExtraContactInformation(){
	showElement("trRole");
	showElement("trCompanyWebsite");
	showElement("trAddress1");
	showElement("trAddress2");
	showElement("trCity");
	showElement("trYourState");
	showElement("trCountry");
	showElement("trPhone");
	showElement("trHowDidYouHear");
	if(getElement("ToggleExtraContactInfo") != null){
		getElement("ToggleExtraContactInfo").innerHTML = "hide extra";
	}
	if(getElement("ShowAllContactInfo") != null){
		getElement("ShowAllContactInfo").value = "true";
	}
	if(getElement("divToggleExtraContactInfo") != null){
		showElement("divToggleExtraContactInfo");
	}
}

function hideExtraContactInformation(){
	hideElement("trRole");
	hideElement("trCompanyWebsite");
	hideElement("trAddress1");
	hideElement("trAddress2");
	hideElement("trCity");
	hideElement("trYourState");
	hideElement("trCountry");
	//hideElement("trPhone");
	hideElement("trHowDidYouHear");
	if(getElement("ToggleExtraContactInfo") != null){
		getElement("ToggleExtraContactInfo").innerHTML = "show all";
	}
	if(getElement("ShowAllContactInfo") != null){
		getElement("ShowAllContactInfo").value = "false";
	}
	if(getElement("divToggleExtraContactInfo") != null){
		showElement("divToggleExtraContactInfo");
	}
}


******END DEPRECATED ***/

function checkRequestType(RequestType){
	
	var tdQuantity = null;
	var tdUOM = null;
	var tdEstimatedYearlyUsage = null;
	var tdPurchaseTimeFrame = null;
	
	tdQuantity = getElement("tdQuantity");
	tdUOM = getElement("tdUOM");
	tdEstimatedYearlyUsage = getElement("tdEstimatedYearlyUsage");
	tdPurchaseTimeFrame = getElement("tdPurchaseTimeFrame");
	
	var Quantity;
	var UOM;
	var EstimatedYearlyUsage;
	var PurchaseTimeFrame;														
	
	if(RequestType == "Receive Quote/Pricing"){
		showElement("trReceiveCompetitiveQuotes");
	}
	else{
		hideElement("trReceiveCompetitiveQuotes");
	}	
	
	Quantity = "ChkLabel";
	UOM = "ChkLabel";
	EstimatedYearlyUsage = "ChkLabel";
	PurchaseTimeFrame = "ChkLabel";
	
	if("<%=InVirtualSite%>" != "True"){
		if(RequestType == "Receive Quote/Pricing" || RequestType == "Purchase this Product"){
			Quantity = "ChkLabelReq";
			UOM = "ChkLabelReq";
			if(RequestType != "Purchase this Product"){
				EstimatedYearlyUsage = "ChkLabelReq";
				PurchaseTimeFrame = "ChkLabelReq";
			}
		}
	}
	
	if(tdQuantity!=null){tdQuantity.className = Quantity;}
	if(tdUOM!=null){tdUOM.className = UOM;}
	if(tdEstimatedYearlyUsage!=null){tdEstimatedYearlyUsage.className = EstimatedYearlyUsage;}
	if(tdPurchaseTimeFrame!=null){tdPurchaseTimeFrame.className = PurchaseTimeFrame;}
	
}

function checkCompanyType(CompanyType){
	var o1 = null;
	if(document.all) {
		o1 = document.all("Other");
	}
	else if(document.getElementById) {
		o1 = document.getElementById("Other");
	}
	if(o1!=null) {
		if(CompanyType == "Other"){
			o1.style.display = "inline";
		}
		else{
			o1.style.display = "none";
		}
	}
}
