// form.js

//******************************
function myIPIFormTestConnection()
{
	alert('connected to form.js, 5-22-08, 7:01pm');	
}
//******************************

//****************************
function myIPIFormGetCookie()
{
	var strName = 'paper';
	var strResult = "No cookie found";
	var strCookie = " " + document.cookie + ";";
	var strSearchName = " " + strName + "=";
	var iStartCookie = strCookie.indexOf(strSearchName);
	var iEndCookie = 0;	
	
	if(iStartCookie != -1)
	{
		iStartCookie += strSearchName.length;
		iEndCookie = strCookie.indexOf(";", iStartCookie);
		strResult = unescape(strCookie.substring(iStartCookie, iEndCookie));
	}//end if
	return strResult;
}// end function
//************************

//*********************
function myIPIFormSetCookie()
{
	var strName = 'paper';
	var strValue = 'ok';	
	
	var objDate = new Date();	
	var iExpTime = 1 * 24 * 60 * 60 * 1000;
	
	objDate.setTime(objDate.getTime() + iExpTime);
	
	var strExpires = '; expires=' + objDate.toGMTString();
//alert(strExpires);	
	document.cookie = strName + '=' + strValue + strExpires;
	
//alert(document.cookie);
}//end function
//******************

//***************************
function myIPIFormValidate(objForm)
{
	var bReturn = null;
	
	var bFirstNameOk   = myIPIValidateEmpty('first_name');
	var bLastNameOk    = myIPIValidateEmpty('last_name');
	var bCompanyOk     = myIPIValidateEmpty('company');
	var bEmailOk       = myIPIValidateEmpty('email');
	var bCountryOk     = myIPIValidateEmpty('country');	
	var bRegionOk      = myIPIValidateEmpty('00N300000015jUt');	

	var bLeadSourceOk  = myIPIValidateEmpty('lead_source');

	var bEmailFormatOk = null;
	if(bEmailOk)
	{
		//check further that the email is formatted correctly
		bEmailFormatOk = myIPIValidateEmailFormat('email');
	}
	
	var bFormOk = (bFirstNameOk  &&
				   bLastNameOk   &&
				   bCompanyOk    &&
				   bEmailOk      &&
				   bRegionOk     &&
				   bLeadSourceOk &&
				   bEmailFormatOk);
	
	if(bFormOk)
	{		
		myIPIFormSetCookie();
//myIPIFormGetCookie();
		objForm.submit();			
		bReturn = true;
	}//end if
	else
	{
		bReturn = false;
	}//end else
	
	return bReturn;
}//end function
//********************************

//*****************************

function myIPIValidateEmpty(strID)
{
	var bReturn = null;
	var objInputField = document.getElementById(strID);	
	var strErrorSpanID = strID + '_error';
	var objErrorSpan = document.getElementById(strErrorSpanID);
	
	if(objInputField.value.length == 0)
	{	
		var strErrorMessage = "Please enter a value.";
		objErrorSpan.innerHTML = strErrorMessage;
		bReturn = false;
	}//end if
	else
	{
		var strClearErrorMessage = '';
		objErrorSpan.innerHTML = strClearErrorMessage;	
		bReturn = true;
	}
	
	return bReturn
}//end function

//*************************

//************************
function myIPIValidateEmailFormat(strID)
{
	var bReturn = null;
	
	var objInputField = document.getElementById(strID);	
	var strErrorSpanID = strID + '_format_error';
	var objErrorSpan = document.getElementById(strErrorSpanID);
	
	var regexpEmailFormatPattern = /^(.)+@(.)+(\.(.)+)+$/;
	
	bReturn = regexpEmailFormatPattern.test(objInputField.value);

	if(bReturn)
	{	
		//match found
		var strClearErrorMessage = '';
		objErrorSpan.innerHTML = strClearErrorMessage;
	}//end if
	else
	{
		var strErrorMessage = "Please enter an email address<br>(for example, info@ipinfusion.com).";
		objErrorSpan.innerHTML = strErrorMessage;
	}//end else
	
	return bReturn;
}//end function

//******************************************

//******************************************

function myIPIWriteWhitePaperFormCode(strThankYouURL, strLeadSource)
{
	//<!------------------ Salesforce HTML -------------------------->	
	 
	document.write('<form id="form_lead" name="form_lead"   action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">');

	document.write('<input type="hidden" name="oid" value="00D300000001bnL">');

	document.write('<input type="hidden" name="retURL" value="' + strThankYouURL + '">');

// for debugging only -- sends email instead of entering into salesforce db
//document.write('<input type="hidden" name="debug" value="1">');
//document.write('<input type="hidden" name="debugEmail" value="reagan.jew@access-company.com">');

	document.write('<p><strong>Register to download this white paper.</strong></p>');

	document.write('<p>');
	document.write('<label for="salutation" >');	
	document.write('<span class="form-label">Salutation</span>');
	document.write('<select  id="salutation" name="salutation" class="form-select">');
	document.write('<option value="">--None--</option>');
	document.write('<option value="Mr.">Mr.</option>');
	document.write('<option value="Ms.">Ms.</option>');
	document.write('<option value="Mrs.">Mrs.</option>');
	document.write('<option value="Dr.">Dr.</option>');
	document.write('<option value="Prof.">Prof.</option>');
	document.write('</select>');
	document.write('</label>');
	document.write('</p>');

	document.write('<p>');
	document.write('<label for="first_name">');
	document.write('<span class="form-label">First Name*</span>');
	document.write('<input  maxlength="40" type="text" id="first_name" size="20" name="first_name" class="form-text"');
	document.write('onBlur="myIPIValidateEmpty(\'first_name\');" />');
	document.write('<span id="first_name_error" class="error"></span>');
	document.write('</label>');
	document.write('</p>');

	document.write('<p>');
	document.write('<label for="last_name">');
	document.write('<span class="form-label">Last Name*</span>');
	document.write('<input id="last_name" maxlength="80" name="last_name" size="20" type="text" class="form-text" ');
	document.write('onBlur="myIPIValidateEmpty(\'last_name\');" />');
	document.write('<span id="last_name_error" class="error"></span>');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="title">');
	document.write('<span class="form-label">Title</span>');
	document.write('<input id="title" maxlength="40" name="title" size="20" type="text" class="form-text" />');
	document.write('</label>');
	document.write('</p>');

	document.write('<p>');
	document.write('<label for="company">');
	document.write('<span class="form-label">Company*</span>');
	document.write('<input id="company" maxlength="40" name="company" size="20" type="text" class="form-text" ');
	document.write('onBlur="myIPIValidateEmpty(\'company\');"/>');
	document.write('<span id="company_error" class="error"></span>');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="email">');
	document.write('<span class="form-label">Email*</span>');
	document.write('<input id="email" maxlength="80" name="email" size="20" type="text" class="form-text" ');
	document.write('onBlur="myIPIValidateEmpty(\'email\');"/>');
	document.write('<span id="email_error" class="error"></span>');
	document.write('<span id="email_format_error" class="error"></span>');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="phone">');
	document.write('<span class="form-label">Telephone</span>');
	document.write('<input id="phone" maxlength="40" name="phone" size="20" type="text" class="form-text" />');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="street">');
	document.write('<span class="form-label">Address</span>');
	document.write('<textarea name="street" class="form-text-area"></textarea>');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="city">');
	document.write('<span class="form-label">City</span>');
	document.write('<input  id="city" maxlength="40" name="city" size="20" type="text" class="form-text" />');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="state">');
	document.write('<span class="form-label">State/Province</span>');
	document.write('<input id="state" maxlength="20" name="state" size="20" type="text" class="form-text" />');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="zip">');
	document.write('<span class="form-label">Zip</span>');
	document.write('<input  id="zip" maxlength="20" name="zip" size="20" type="text" class="form-text" />');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="country">');
	document.write('<span class="form-label">Country*</span>');
	document.write('<input id="country" maxlength="40" name="country" size="20" type="text" class="form-text" ');
	document.write('onBlur="myIPIValidateEmpty(\'country\');"/>');
	document.write('<span id="country_error" class="error"></span>');
	document.write('</label>');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<label for="region">');
	document.write('<span class="form-label">Region*</span>');
	document.write('<select  id="00N300000015jUt" name="00N300000015jUt" title="Region" class="form-select" ');
	document.write('onBlur="myIPIValidateEmpty(\'region\');">');
	document.write('<option value="">--None--</option>');
	document.write('<OPTION VALUE="AFRICA">AFRICA</OPTION>');
//	document.write('<OPTION VALUE="CHINA">CHINA</OPTION>');
	document.write('<OPTION VALUE="INDIA">INDIA</OPTION>');
	document.write('<OPTION VALUE="JAPAN">JAPAN</OPTION>');
	document.write('<OPTION VALUE="KOREA">KOREA</OPTION>');
	document.write('<OPTION VALUE="SOUTH AMERICA">SOUTH AMERICA</OPTION>');
	document.write('<OPTION VALUE="MIDDLE EAST">MIDDLE EAST</OPTION>');
	document.write('<OPTION VALUE="TAIWAN">TAIWAN</OPTION>');
	document.write('<OPTION VALUE="US CENTRAL">US CENTRAL</OPTION>');
	document.write('<OPTION VALUE="US EAST">US EAST</OPTION>');
	document.write('<OPTION VALUE="US WEST">US WEST</OPTION>');
	document.write('<OPTION VALUE="ASIA PACIFIC">ASIA PACIFIC</OPTION>');	
	document.write('<OPTION VALUE="EUROPE EAST">EUROPE EAST</OPTION>');	
	document.write('<OPTION VALUE="EUROPE NORTH">EUROPE NORTH</OPTION>');
	document.write('<OPTION VALUE="EUROPE SOUTH">EUROPE SOUTH</OPTION>');	
	document.write('<OPTION VALUE="EUROPE CENTRAL">EUROPE CENTRAL</OPTION>');	
	document.write('<OPTION VALUE="ISRAEL">ISRAEL</OPTION>');	
	document.write('<OPTION VALUE="OTHER">OTHER</OPTION>');	
	document.write('</select>');
	document.write('<span id="00N300000015jUt_error" class="error"></span>');
	document.write('</label>');
	document.write('</p>');

	document.write('<p>');
	document.write('<label for="lead_source">');
	document.write('<input type="hidden" id="lead_source" name="lead_source" value="' + strLeadSource + '" />');
	document.write('<span id="lead_source_error" class="error"></span>');
	document.write('</label>');	
	document.write('</p>');

	document.write('<p class="form-label" style="width:200px;">');
	document.write('* Required information');
	document.write('</p>');
	
	document.write('<p>');
	document.write('<input type="button" value="Submit" onClick="myIPIFormValidate(this.form);" class="form-label"/>');
	document.write('</p>');
	document.write('</form>');

}//end function
//****************************
//******************************
function myIPIGetQueryString()
{
	var strQueryString = document.location.search;

	var objQueryString = new Object();
	var iQuestionMarkOffset = 1;
	strQueryString = strQueryString.substring(iQuestionMarkOffset, strQueryString.length); 
	var strArgPairs = strQueryString.split('&');

	for(var iIndex = 0; iIndex < strArgPairs.length; iIndex++)
	{ 
		var iNotFound = -1;
		var iPosition = strArgPairs[iIndex].indexOf('='); 
		if(iPosition != iNotFound)
		{
			var strArgName = strArgPairs[iIndex].substring(0, iPosition); 
			var strArgValue = strArgPairs[iIndex].substring(iPosition + 1); 

			objQueryString[strArgName] = unescape(strArgValue); 
		}//end if
	}//end for	
	return objQueryString;
}//end function
//****************************
//**************************