﻿// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
}

// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}

var bFormSubmitted = false;

<!--Function added by callum to check for a change to a required dropdown box-->
<!--Add this line on the object to use-->
<!--onChange ="checkForDropdownSelection(this)"-->

function checkForDropdownSelection(dropdown)
{
	if(bFormSubmitted)
	{
		var div = document.getElementById('required_' + dropdown.name);

		if(dropdown.selectedIndex==0)
		{
			div.style.display = "block";
		}
		else
		{
			div.style.display = "none";
		}
	}
}

function checkForRadioSelect(radios)
{
	if(bFormSubmitted)
	{
		var div = document.getElementById('required_' + radios.name);
		div.style.display = "none";
	}
}

function getProvStat(country)
{
	
	var divRequired = document.getElementById('required_val_8');
	var divRequiredStatProv = document.getElementById('required_val_6');

	if(bFormSubmitted)
	{
		//divRequiredStatProv.style.visibility = "hidden";
	}

	if(country == "")
	{
		document.getElementById('divProvStatValue').innerHTML = "Please select a country";

		if(bFormSubmitted)
		{
			//divRequired.style.visibility = "visible";
		}
	}
	else
	{
		if(bFormSubmitted)
		{
			//divRequired.style.visibility = "hidden";
		}

		var xmlHttp;
		try
		{    // Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{    // Internet Explorer
		try
			{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
			try
				{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				alert("Your browser does not support AJAX!");
				return false;
				}
			}
		}

		xmlHttp.onreadystatechange = function()
		{
		  if(xmlHttp.readyState == 4)
			{
				if(xmlHttp.responseText == 'N/A')
				{
					document.getElementById('divProvStatValue').innerHTML = "N/A";
					document.getElementById('divProvStatLabel').innerHTML = "State/Province:";
				}
				else if(xmlHttp.responseText == 'null')
				{
					document.getElementById('divProvStatValue').innerHTML = "<input type=\"text\" name=\"val_6\" class=\"register\" value=\"\" onKeyUp=\"checkForValue(this)\">";
					document.getElementById('divProvStatLabel').innerHTML = "State/Province:";
					if(bFormSubmitted)
					{
						//divRequiredStatProv.style.visibility = "visible";
					}
				}
				else
				{
					var arrProvStat = xmlHttp.responseText.split(",");
					var sbProvStat = new StringBuilder();

					sbProvStat.append("<select name=\"val_6\" class=\"register\" >")

					for(var i = 1; i < arrProvStat.length; i++)
					{
						sbProvStat.append("<option value=\"" + arrProvStat[i] + "\">" + arrProvStat[i] + "</option>");
					}

					sbProvStat.append("</select><br style=\"clear:left\" />")

					document.getElementById('divProvStatValue').innerHTML = sbProvStat.toString();
					document.getElementById('divProvStatLabel').innerHTML = arrProvStat[0] + ":";
				}
			}
		}

		xmlHttp.open("GET","provStat.asp?country=" + country,true);

		xmlHttp.send(null);
	}
}

function emailCheck (emailStr)
{
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	var emailError = "";

	if (matchArray==null)
	{
		return "Email: address seems incorrect (check @ and .'s)";
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {
		return "Email: The username doesn't seem to be valid.";
	}

	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				return "Email: Destination IP address is invalid!";
	  		}
	  	}

	  	return "";
	}

	var domainArray=domain.match(domainPat);

	if (domainArray==null)
	{
		return "Email: The domain name doesn't seem to be valid.";
	}

	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;

	if ((domArr[domArr.length-1] != "info") &&
		(domArr[domArr.length-1] != "name") &&
		(domArr[domArr.length-1] != "arpa") &&
		(domArr[domArr.length-1] != "coop") &&
		(domArr[domArr.length-1] != "aero"))
	{
		if (domArr[domArr.length-1].length<2 ||
			domArr[domArr.length-1].length>3)
		{
			return "Email: The address must end in a three-letter domain, or two letter country.";
	  	}
	}

	if (len<2)
	{
		return "Email: This address is missing a hostname!";
	}

	return "";
}

function UPTvalidateform(thisform)
{
	//alert(thisform.formsubmit.value);
		
	bFormSubmitted = true;

	var sbErrorMessage = new StringBuilder()
	var bValid = true;

	sbErrorMessage.append("The following information is required:\n\n");

	if (thisform.val_1.value=="")
	{
		sbErrorMessage.append(" * First Name\n");
		document.getElementById('required_val_1').style.display = "block";
		bValid = false;
	}

	if (thisform.val_2.value=="")
	{
		sbErrorMessage.append(" * Last Name\n");
		document.getElementById('required_val_2').style.display = "block";
		bValid = false;
	}
    
    if (thisform.val_7.value=="")
	{
		sbErrorMessage.append(" * ZIP/Postal Code\n");
		document.getElementById('required_val_7').style.display = "block";
		bValid = false;
	}

	if (thisform.val_100673.value=="")
	{
		sbErrorMessage.append(" * Find out about Steamboat \n");
		document.getElementById('required_val_100673').style.display = "block";
		bValid = false;
	}

	<!-- Additional custom Fields can be added here Some samples for different objects are included below -->
	<!--Radio Button check-->
	<!--if (thisform.val_59795[0].checked==false &&thisform.val_59795[1].checked==false)  -->
	<!--{ 		sbErrorMessage.append(" * I would like a subscription to...\n");-->
	<!--	document.getElementById('required_val_59795').style.display = "block";-->
	<!--	bValid = false;	}-->

	/*if (thisform.val_9.value=="")
	{
		sbErrorMessage.append(" * Phone\n");
		document.getElementById('required_val_9').style.display = "block";
		bValid = false;
	}*/
	
	<!--between this line and the next commented line can be moved up to align with form fields-->
	var emailError = emailCheck(thisform.email.value);

	if (emailError == "")
	{
		if (document.getElementById('unsubscribe') && !document.getElementById('unsubscribe').checked)
		{

		}
		//bValid = true;
	}
	else
	{
		sbErrorMessage.append(" * " + emailError + "\n");
		document.getElementById('required_email').style.display = "block";
		bValid = false;
	}
	
	<!--between this line and the above commented line can be moved up to align with form fields-->
	if(bValid)
	{
		//alert("in");
		
		thisform.formsubmit.value = "true";
		
		// Validation is successful, so move the EmailLabs values into fields for MS Dynamics CRM
		//document.dl_leadForm.dl_361416071267246085.value = "Unknown";
		//document.dl_leadForm.dl_361416071267246087.value = thisform.email.value;
		//document.dl_leadForm.dl_361416071267246083.value = thisform.val_1.value;
		//document.dl_leadForm.dl_361416071267246084.value = thisform.val_2.value;
		//document.dl_leadForm.dl_361416071267246088.value = thisform.val_4.value;
		//document.dl_leadForm.dl_361416071267246090.value = thisform.val_5.value;
		//if (thisform.val_8.value) {document.dl_leadForm.dl_361416071267246089.value = thisform.val_6.value;}
		//document.dl_leadForm.dl_361416071267246091.value = thisform.val_7.value;
		//if (thisform.val_8.value) {document.dl_leadForm.dl_361416071267246092.value = thisform.val_8.value;}
		//document.dl_leadForm.dl_361414971755617282.value = thisform.val_9.value;
		//document.dl_leadForm.dl_361416071267246093.value = "How did you learn about Steamboat? " + thisform.val_100673.value + "\n" + "How can we help? " + thisform.val_100674.value;
		
		//document.dl_leadForm.submit();
		document.forms[0].submit();
		
		return false;
	}
	else
	{
		//alert(sbErrorMessage.toString());
		return false;
	}
	
	
}

function checkForValue(textbox)
{
	if(bFormSubmitted)
	{
		var div = document.getElementById('required_' + textbox.name);

		if(textbox.value == "")
		{
			div.style.display = "block";
		}
		else
		{
			div.style.display = "none";
		}
	}
}

