// JScript File

function autoSelectInquiry()
{
	showDiv(0);
	var iType
	iType=document.location.search;
	iType=iType.substring(7,iType.length)
	if ((iType>0)&&(iType<(document.forms['EmailForm'].InquiryTypeDropDown.length)))
	{
		document.forms['EmailForm'].InquiryTypeDropDown.options[iType].selected=true;
		for (var i=0; i < document.forms['EmailForm'].InquiryTypeDropDown.length;i++)
		{
			if ((document.forms['EmailForm'].InquiryTypeDropDown.options[i].value)==iType)
			{
				document.forms['EmailForm'].InquiryTypeDropDown.options[iType].selected=true;
				showDiv(iType);
			}
		}
	}
	document.EmailForm.InquiryTypeDropDown.selectedIndex=0;
}

function resetStyle(fldName)
{
	fldName = "lbl" + fldName;
	//alert(fldName);
	document.getElementById(fldName).style.color="black";
	document.getElementById(fldName).style.fontWeight="normal";
}

function requireField(fldName)
{
	document.getElementById('lbl' + fldName).style.color="red";
	document.getElementById('lbl' + fldName).style.fontWeight="bold";
	flag = false;
}

function displayAttachmentInput()
{
	document.getElementById("dependent-upload-link").style.display = 'none';
	document.getElementById("dependent-upload").style.display = 'block';
}

function showDiv(optionValue)
{
	//Reset Styles
	resetStyle("FirstName");
	resetStyle("LastName");
	resetStyle("JobTitle");
	resetStyle("EmailAddress");
	resetStyle("PhoneNumber");
	resetStyle("AccountNumber")
	resetStyle("CompanyName")
	resetStyle("StreetAddress")
	resetStyle("POBox")
	resetStyle("City")
	resetStyle("Province")
	resetStyle("PostalCode")
	resetStyle("Country")
	resetStyle("InquiryOther");
	resetStyle("InquiryDescription");
	resetStyle("InquiryOther2");
	resetStyle("DescriptionOfRequest");
	resetStyle("Attachment")

	// first hide all dependent divs
	document.getElementById("dependent-inquiry-other").style.display = 'none';
	document.getElementById("Inquiry-Details-Fieldset").style.display = 'none';
	document.getElementById("dependent-inquiry-description").style.display = 'none';
	document.getElementById("dependent-inquiry-other2").style.display = 'none';
	document.getElementById("dependent-upload-link").style.display = 'none';
	document.getElementById("dependent-upload").style.display = 'none';
	document.getElementById("Contact-Method-Fieldset").style.display = 'none';
	document.getElementById("Submit-Buttons").style.display = 'none';
	document.EmailForm.InquiryDescriptionDropDown.selectedIndex=0;
	
	var inqDesc = document.EmailForm.InquiryDescriptionDropDown;
	
	switch (optionValue)
	{
		//Customer Relations
		case "1":
			window.document.EmailForm.FormType.value="1";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Account','Account');
			inqDesc.options[inqDesc.options.length] = new Option('MyAccount Password Request','MyAccount Password Request');
			inqDesc.options[inqDesc.options.length] = new Option('Subscription','Subscription');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Shipping','Shipping');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Orders & Product Information
		case "2":
			window.document.EmailForm.FormType.value="2";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Product Information','Product Information');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Non Carswell Products
		case "3":
			window.document.EmailForm.FormType.value="3";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Product Information','Product Information');
			inqDesc.options[inqDesc.options.length] = new Option('Account','Account');
			inqDesc.options[inqDesc.options.length] = new Option('Subscription','Subscription');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Shipping','Shipping');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Claims
		case "4":
			window.document.EmailForm.FormType.value="4";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order tracking','Order tracking');
			inqDesc.options[inqDesc.options.length] = new Option('Damaged goods','Damaged goods');
			inqDesc.options[inqDesc.options.length] = new Option('Parts missing','Parts missing');
			inqDesc.options[inqDesc.options.length] = new Option('Wrong product','Wrong product');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Address Changes
		case "5":
			window.document.EmailForm.FormType.value="5";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Company moving','Company moving');
			inqDesc.options[inqDesc.options.length] = new Option('Person moving','Person moving');
			inqDesc.options[inqDesc.options.length] = new Option('Address correction','Address correction');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Technical Support
		case "6":
			window.document.EmailForm.FormType.value="6";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Access (Password, Log in)','Access (Password, Log in)');
			inqDesc.options[inqDesc.options.length] = new Option('Printing inquiries','Printing inquiries');
			inqDesc.options[inqDesc.options.length] = new Option('Installation & Downloading','Installation & Downloading');
			inqDesc.options[inqDesc.options.length] = new Option('Troubleshooting','Troubleshooting');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Reference Support
		case "7":
			window.document.EmailForm.FormType.value="7";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('CD-ROM support','CD-ROM support');
			inqDesc.options[inqDesc.options.length] = new Option('Online service support','Online service support');
			inqDesc.options[inqDesc.options.length] = new Option('Print product support','Print product support');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Cancellations
		case "8":
			window.document.EmailForm.FormType.value="8";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Info on cancellation policy','Info on cancellation policy');
			inqDesc.options[inqDesc.options.length] = new Option('Reinstatement request','Reinstatement request');
			inqDesc.options[inqDesc.options.length] = new Option('Return of a new purchase','Return of a new purchase');
			inqDesc.options[inqDesc.options.length] = new Option('Cancellation of a renewal','Cancellation of a renewal');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//WestlaweCARSWELL
		case "9":
			window.document.EmailForm.FormType.value="9";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Additional passwords','Additional passwords');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Problem resolution','Problem resolution');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Taxnet Pro
		case "10":
			window.document.EmailForm.FormType.value="10";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Additional passwords','Additional passwords');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Problem resolution','Problem resolution');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//HR & Compliance Products
		case "11":
			window.document.EmailForm.FormType.value="11";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Additional passwords','Additional passwords');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Problem resolution','Problem resolution');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break
			
			
		//eReference Library
		case "13":
			window.document.EmailForm.FormType.value="13";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Additional passwords','Additional passwords');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Problem resolution','Problem resolution');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break
			
		//Invoice Copy Request
		case "14":
			window.document.EmailForm.FormType.value="14";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Account','Account');
			inqDesc.options[inqDesc.options.length] = new Option('MyAccount Password Request','MyAccount Password Request');
			inqDesc.options[inqDesc.options.length] = new Option('Subscription','Subscription');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Shipping','Shipping');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break
		// Westlaw China
		case "15":

			window.document.EmailForm.FormType.value="15";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Additional passwords','Additional passwords');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Problem resolution','Problem resolution');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break
		// Consult Carswell
		case "16":

			window.document.EmailForm.FormType.value="16";

			inqDesc.length=1;
			inqDesc.options[inqDesc.options.length] = new Option('Order','Order');
			inqDesc.options[inqDesc.options.length] = new Option('Additional passwords','Additional passwords');
			inqDesc.options[inqDesc.options.length] = new Option('Billing','Billing');
			inqDesc.options[inqDesc.options.length] = new Option('Problem resolution','Problem resolution');
			inqDesc.options[inqDesc.options.length] = new Option('Other','Other');
			break

		//Other
		case "12":
			window.document.EmailForm.FormType.value="12";

			inqDesc.length=1;

			document.getElementById("dependent-inquiry-other").style.display = 'block';
			document.getElementById("Inquiry-Details-Fieldset").style.display = 'block';
			document.getElementById("dependent-upload-link").style.display = 'block';
			document.getElementById("Contact-Method-Fieldset").style.display = 'block';
			document.getElementById("Submit-Buttons").style.display = 'block';
			break


	} //switch

	if (optionValue != 0 && optionValue != 12)
	{
		document.getElementById("Inquiry-Details-Fieldset").style.display = 'block';
		document.getElementById("dependent-inquiry-description").style.display = 'block';
		document.getElementById("dependent-upload-link").style.display = 'block';
		document.getElementById("Contact-Method-Fieldset").style.display = 'block';
		document.getElementById("Submit-Buttons").style.display = 'block';
	}

} //showDiv

function changeInquiryDescription(FormType)
{
	switch (document.EmailForm.InquiryDescription.value)
	{
		case "Other":
			document.getElementById("dependent-inquiry-other2").style.display = 'block';
			break;
		default:
			document.getElementById("dependent-inquiry-other2").style.display = 'none';
			break;
	}
}


function SubmitForm()
{



	strFormType = document.EmailForm.FormType.value;
	strInquiryType = document.EmailForm.InquiryDescription.value;

	strErr = ""; //clear error label
	flag = true; //unset flag

	//unset all style colors
	resetStyle("FirstName");
	resetStyle("LastName");
	resetStyle("JobTitle");
	resetStyle("EmailAddress");
	resetStyle("PhoneNumber");
	resetStyle("AccountNumber")
	resetStyle("CompanyName")
	resetStyle("OrgType")
	resetStyle("StreetAddress")
	resetStyle("City")
	resetStyle("Province")
	resetStyle("PostalCode")
	resetStyle("Country")
	resetStyle("InquiryOther");
	resetStyle("InquiryDescription");
	resetStyle("InquiryOther2");
	resetStyle("DescriptionOfRequest");
	resetStyle("Attachment")
	
	validateForm();
	
	// check postal code
	
	var varPostalCode = document.EmailForm.PostalCodeTextBox.value;
	varPostalCode = trimAll(varPostalCode);
	
	if (varPostalCode.indexOf(" ") != -1)
    {
        requireField("PostalCode");
        strErr = "Space in postal code is not allowed!";
    }
    
	if (flag){
		SubmitInfo();

		document.EmailForm.submit();
	}
	else
		alert(strErr);

}

function trimAll(sString) 
{ 
    while (sString.substring(0,1) == ' ') 
    { 
        sString = sString.substring(1, sString.length); 
    } 
    while (sString.substring(sString.length-1, sString.length) == ' ') 
    { 
        sString = sString.substring(0,sString.length-1); 
    } 
    return sString; 
}

function isValidPostalcode(postalcode)
{
//              if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
//              else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) return true;
//              else return false;
//

        if (postalcode.length == 6 && postalcode.search(/^[A-Z]\d[A-Z]\d[A-Z]\d$/) != -1) return true;
        else if (postalcode.length == 7 && postalcode.search(/^[A-Z]\d[A-Z](-|\s)\d[A-Z]\d$/) != -1) return true;
        else return false;



}

function validateForm()
{
	validRequired("FirstName");
	validRequired("LastName");
	validRequired("JobTitle");
	validRequired("EmailAddress");
	validRequired("PhoneNumber");
	validRequired("AccountNumber");
	validRequired("OrgType");
	validRequired("StreetAddress")
	validRequired("City")
	validRequired("Province")
	validRequired("PostalCode")
	validRequired("Country")
	validRequired("DescriptionOfRequest");
	
	switch(strFormType)
	{
		case "12": // Other
			validRequired("InquiryOther");
			break;

		default: // Every other one
			validRequired("InquiryOther2");
			validRequired("InquiryDescription");
			break;
	}
	if (!flag){strErr = strErr + "Please complete all required fields (shown in bold red)";}
}

function validRequired(fldName)
{
	// First Name Validation
	if ((fldName=="FirstName") && (document.EmailForm.FirstNameTextBox.value==""))
		requireField(fldName);

	// Last Name Validation
	if ((fldName=="LastName") && (document.EmailForm.LastNameTextBox.value==""))
		requireField(fldName);

	// Job Title
	if ((fldName=="JobTitle") && (document.EmailForm.JobTitleTextBox.value==""))
		requireField(fldName);

	// Email Validation
	if ((fldName=="EmailAddress") && (document.EmailForm.EmailAddressTextBox.value==""))
		requireField(fldName);
	if ((fldName=="EmailAddress") && (document.EmailForm.EmailAddressTextBox.value!=""))
	{
		var eMailRegEx = new RegExp("^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$");
		if (!eMailRegEx.test(document.EmailForm.EmailAddressTextBox.value))
		{ 
			requireField(fldName);
			alert('Please enter a valid email address.')
		}
	}

	// Phone Validation
	if (fldName=="PhoneNumber"){
		if (document.EmailForm.PhoneNumberTextBox.value=="")
			requireField(fldName);
		else
		{
			if (document.EmailForm.PhoneNumberTextBox.value!="")
			{
				if (document.EmailForm.PhoneNumberTextBox.value.length < 10) 
				{ 
					requireField(fldName);
					alert('Phone numbers should include at least 10 digits.')
				}
				else
				{	
					x=0
					for (j=0; j<document.EmailForm.PhoneNumberTextBox.value.length; j++) 
					{
						x=x+1 
						if ((document.EmailForm.PhoneNumberTextBox.value.charAt(j) < "0") || (document.EmailForm.PhoneNumberTextBox.value.charAt(j) > "9")) 
						{ 
							x=x-1
						}
					}
					if (x<10)
					{
						requireField(fldName);
						alert('Phone numbers should include at least 10 digits.')
					}
			 	}
			}
		}
	}

	// Account Number Validation
	if ((fldName=="AccountNumber") && (document.EmailForm.AccountNumberTextBox.value!=""))
	{
		var acctNumRegEx = new RegExp("^[a-zA-Z0-9]{1,8}$");
		if (!acctNumRegEx.test(document.EmailForm.AccountNumberTextBox.value))
		{ 
			requireField(fldName);
			alert('Account numbers must contain between 1 and 8 alpha-numeric characters. Please review your account number for accuracy and resubmit.')
		}
	}

	// Organization Type Validation
	if ((fldName=="OrgType") && (document.EmailForm.OrgTypeDropDown.selectedIndex==0))
		requireField(fldName);

	// Street Address Validation
	if ((fldName=="StreetAddress") && (document.EmailForm.StreetAddressTextBox.value==""))
		requireField(fldName);

	// City Validation
	if ((fldName=="City") && (document.EmailForm.CityTextBox.value==""))
		requireField(fldName);

	// Province Validation
	if ((fldName=="Province") && (document.EmailForm.ProvinceTextBox.value==""))
		requireField(fldName);

	// Postal Code Validation
	if ((fldName=="PostalCode") && (document.EmailForm.PostalCodeTextBox.value==""))
		requireField(fldName);

	// Country Validation
	if ((fldName=="Country") && (document.EmailForm.CountryTextBox.value==""))
		requireField(fldName);

	// Inquiry Other Validation
	if ((fldName=="InquiryOther") && (document.EmailForm.InquiryOtherTextBox.value==""))
		
		requireField(fldName);
	
	// Inquiry Description Validation
	if ((fldName=="InquiryDescription") && (document.EmailForm.InquiryDescriptionDropDown.selectedIndex==0))
		requireField(fldName);

	// Inquiry Other2 Validation
	if ((fldName=="InquiryOther2") && (document.EmailForm.InquiryOther2TextBox.value==""))
		if (document.EmailForm.InquiryDescriptionDropDown.value == "Other")
			requireField(fldName);

	// Requests/Comments Validation
	if ((fldName=="DescriptionOfRequest") && (document.EmailForm.DescrptionOfRequestTextArea.value==""))
		requireField(fldName);

	// Attachment Validation
	if ((fldName=="Attachment") && (document.EmailForm.FilAttach.value!=""))
	{
		var ext = document.EmailForm.FilAttach.value;
		ext = ext.substring(ext.length-3,ext.length);
		ext = ext.toLowerCase();
		if ((ext == 'exe')||(ext=='.js')||(ext=='.vb')||(ext=='asp')||(ext=='dll')||(ext=='scr')||(ext=='bat')||(ext=='reg')||(ext=='cab')||(ext=='mp3'))
		{
			alert('You selected a .'+ext+
			' file; Please select a text-based file.');
			flag = false;;
		}
	}
}

