function PhoneCheck(sPhoneNumFld, bSubmitForm)
{
	var sPhoneNum = document.getElementById(sPhoneNumFld).value;
	var re = /(^(([+]1\s?)?((\([1-9][0-9]{2}\)[-\s]?)|([1-9][0-9]{2}[-\s]?)))[1-9][0-9]{2}[-\s]?[0-9]{4}(\sx\s*[0-9]{2,5})?$)|(^(\+\d{1,3}\s?)?(\(\d{1,5}\)|\d{1,5})\s?\d{3}\s?\d{0,7}$)/;
	
	if(!sPhoneNum.match(re))
	{
		alert("Phone must be in form: (###) ###-#### x###  OR  +## ### ### #### x### for international");
	}
	else if(bSubmitForm)
	{
		document.getElementById("Form1").submit();
	}
}

function PrepSubmit()
{
	var fillInAllFields = 0;
	if (document.getElementById("Fst").value=='First Name') {fillInAllFields = 1;}
	if (document.getElementById("Lst").value=='Last Name') {fillInAllFields = 1;}
	if (document.getElementById("Org").value=='Organization Name (if app)') {fillInAllFields = 1;}
	if (document.getElementById("Phn").value=='Contact Phone') {fillInAllFields = 1;}
	if (document.getElementById("Eml").value=='Email Address') {fillInAllFields = 1;}
	if (document.getElementById("Fst").value=='') {fillInAllFields = 1;}
	if (document.getElementById("Lst").value=='') {fillInAllFields = 1;}
	if (document.getElementById("Org").value=='') {fillInAllFields = 1;}
	if (document.getElementById("Phn").value=='') {fillInAllFields = 1;}
	if (document.getElementById("Eml").value=='') {fillInAllFields = 1;}

	if (fillInAllFields == 1)
	{
		alert("Please enter all required fields.");
	}
	else
	{
		cookie = getCookie("CampaignID");
		if (cookie != null)
		{
			//CampaignID
			document.getElementById("Hidden5").value = cookie;
		}
		//LeadSource
        document.getElementById("Hidden4").value = "Marketing";
    		
        //PageYouWereOn
        document.getElementById("Hidden3").value = strPageYouWereOn;
		                               
		document.getElementById("Form1").submit();
	}
}

function HandleFormError()
{
	document.getElementById("Fst").value=URLDecode(getQueryStringParameter("Fst"));
	document.getElementById("Lst").value=URLDecode(getQueryStringParameter("Lst"));
	document.getElementById("Org").value=URLDecode(getQueryStringParameter("Org"));
	document.getElementById("Phn").value=URLDecode(getQueryStringParameter("Phn"));
	document.getElementById("Eml").value=URLDecode(getQueryStringParameter("Eml"));
	document.getElementById("Cnt").value=URLDecode(getQueryStringParameter("Cnt"));
	document.getElementById("lblError").innerText = URLDecode(getQueryStringParameter("Msg"));
}

function HandleFormSuccess()
{
	setCookieFor6Months("RequestID",getQueryStringParameter("RequestID"));
}

function HandleCampaign(campaignValue, campaignType)
{
  setCookie("CampaignID",campaignValue);
  setCookie("CampaignType",campaignType);
}

function SetLeadRoutingFlds(topHalf, topEmail, topDeptId, bottomEmail, bottomDeptId, bSubmitForm, sCanadaEmail, sIntlEmail, bUseGeographicRouting)
{
	var cmpSz = document.getElementById("CmpSz");
	var notifyEmail = document.getElementById("NotifyEmail");
	var targetDept = document.getElementById("td");
	var country = document.getElementById("Cnt")
	var state = document.getElementById("Sta")
	var cmpSzLen = 0;
	var cmpSzSelection = 0;
	
	if (cmpSz != null && notifyEmail != null && targetDept != null)
	{
		cmpSzLen = cmpSz.length - 1;
		if (topHalf == null) {topHalf = Math.round(cmpSzLen/2);}
		cmpSzSelection = cmpSz.selectedIndex;
		if (cmpSzSelection != 0)
		{
			if (cmpSzSelection <= topHalf)
			{
				notifyEmail.value = topEmail;
				targetDept.value = topDeptId;
			}
			else
			{
				notifyEmail.value = bottomEmail;
				targetDept.value = bottomDeptId;
			}
		}
	}

	if (country != null && state != null &&bUseGeographicRouting)
	{
		if (country.value == "Canada")
		{
			notifyEmail.value = sCanadaEmail;
			targetDept.value = "0000000000000016";
		}
		else if (state.value.length == 2 && country.value != "USA")
		{
			country.value = "USA"
		}
		else if (country.value != "USA")
		{
			notifyEmail.value = sIntlEmail;
			targetDept.value = "0000000000000050"; //International Sales
		}
	}
	
	if(bSubmitForm)
	{
		document.getElementById("Form1").submit();
	}
}

if (getQueryStringParameter("Msg") != "")
{
	HandleFormError();
}

if (getQueryStringParameter("RequestID") != "")
{
	HandleFormSuccess();
}

if (getQueryStringParameter("s_cid") != "")
{
	document.getElementById("CampaignID").value=URLDecode(getQueryStringParameter("s_cid"));
	HandleCampaign(getQueryStringParameter("s_cid"), "cid");
}
else if (getQueryStringParameter("s_kwcid") != "")
{
    var kwd = URLDecode(getQueryStringParameter("s_kwcid"));
    if (kwd.indexOf("|")>0)
    {
        var str = kwd.split("|");
        document.getElementById("CampaignID").value=str[1];
	    document.getElementById("PI_Keyword").value=str[0];
    }
    else
    {
	    document.getElementById("CampaignID").value=kwd;
	}

    HandleCampaign(getQueryStringParameter("s_kwcid"), "kwcid");
}

if (getQueryStringParameter("st") != "")
{
	document.getElementById("st").value=URLDecode(getQueryStringParameter("st"));
}

if (getQueryStringParameter("ls") != "")
{
	document.getElementById("ls").value=URLDecode(getQueryStringParameter("ls"));
}

if (getQueryStringParameter("aid") != "")
{
	document.getElementById("aid").value=URLDecode(getQueryStringParameter("aid"));
}

if (getQueryStringParameter("re") != "")
{
	document.getElementById("re").value=URLDecode(getQueryStringParameter("re"));
}

if (getQueryStringParameter("pt") != "")
{
    if (document.getElementById("pt") != null)
	    document.getElementById("pt").value=URLDecode(getQueryStringParameter("pt"));
	else if (document.getElementById("C_PI_Prtnr_ID1") != null)
	    document.getElementById("C_PI_Prtnr_ID1").value = URLDecode(getQueryStringParameter("pt"));
}

if (getQueryStringParameter("pte") != "")
{
    if (document.getElementById("C_PI_Prtnr_Email1") != null)
	    document.getElementById("C_PI_Prtnr_Email1").value = URLDecode(getQueryStringParameter("pte"));
}

if (getQueryStringParameter("td") != "")
{
	document.getElementById("td").value=URLDecode(getQueryStringParameter("td"));
}

var trackerCode = "UA-4172443-4";
if (typeof(overwritePageTracker) != "undefined")
{
    trackerCode = overwritePageTracker;
}

var pageTracker = _gat._getTracker(trackerCode);
pageTracker._trackPageview();
