// 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)
{
	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_4.value=="")
	{	
		sbErrorMessage.append(" * Address\n");
		document.getElementById('required_val_4').style.display = "block";
		bValid = false;
	}
	
	if (thisform.val_5.value=="")
	{	
		sbErrorMessage.append(" * City\n");
		document.getElementById('required_val_5').style.display = "block";
		bValid = false;
	}
	
	if (thisform.val_6 != null && thisform.val_6.type == "text" && thisform.val_6.value=="") //(thisform.val_6.type == "select" && thisform.val_6.selectedIndex==0)) 
	{ 
		sbErrorMessage.append(" * State/Province\n");
		document.getElementById('required_val_6').style.display = "block";
		bValid = false;
	}
	
	if (thisform.val_8.selectedIndex==0) 
	{ 
		sbErrorMessage.append(" * Country\n");
		document.getElementById('required_val_8').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_9.value=="")
	{	
		sbErrorMessage.append(" * Phone\n");
		document.getElementById('required_val_9').style.display = "block";
		bValid = false;
	}
	
	if (thisform.val_12535.value=="")
	{	
		sbErrorMessage.append(" * How did you learn about us? \n");
		document.getElementById('required_val_12535').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)
	{
		return false;
	}
	else
	{
		//alert(sbErrorMessage.toString());
		return true;
	}
}

function checkForValue(textbox)
{
	if(bFormSubmitted)
	{
		var div = document.getElementById('required_' + textbox.name);
		
		if(textbox.value == "")
		{
			div.style.display = "block";
		}
		else
		{
			div.style.display = "none";
		}
	}
}
