function CreateAction(theForm)
{
	var regvar = /^[a-zA-Z\- ]+$/;
	if (!theForm.lead_personfirstname.value || theForm.lead_personfirstname.value.length < 2)
	{
		alert('Please enter First name');
		return false;
	}
	if (!regvar.test(theForm.lead_personfirstname.value))
	{
		alert('Please enter a valid first name');
		return false;
	}
	if (!theForm.lead_personlastname.value)
	{
		alert('Please enter Last name');
		return false;
	}
	if (!regvar.test(theForm.lead_personlastname.value))
	{
		alert('Please enter a valid last name');
		return false;
	}
	if (!theForm.lead_companyname.value)
	{
		alert('Please enter Company name');
		return false;
	}
	if (!regvar.test(theForm.lead_companyname.value))
	{
		alert('Please enter a valid company name');
		return false;
	}
	if (!theForm.lead_personemail.value)
	{
		alert('Please enter Email');
		return false;
	}
	var regvar = /^[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4}(,\s*[a-z0-9\._-]+@[a-z0-9\._-]+\.[a-z]{2,4})*$/;
	if (!regvar.test(theForm.lead_personemail.value)) 
	{
		alert('Please enter a valid email address');
		return false;
	}

	SaveValues2Cookies(theForm);

	if (document.WEB2LEAD.SELECTWorkFlow == null)
		document.WEB2LEAD.action = "http://office.belmar.ca/crm/eware.dll/SubmitLead?RuleID=";
	else 
		document.WEB2LEAD.action = "http://office.belmar.ca/crm/eware.dll/SubmitLead?RuleID = 10206";
		/*
+document.WEB2LEAD.SELECTWorkFlow.options[document.WEB2LEAD.SELECTWorkFlow.selectedIndex].value;
    */
	return true;
}

function SaveValues2Cookies(theForm)
{
	setCookie("cook_lead_personfirstname",theForm.lead_personfirstname.value);
	setCookie("cook_lead_personlastname",theForm.lead_personlastname.value);
	setCookie("cook_lead_companyname",theForm.lead_companyname.value);
	setCookie("cook_lead_personphonenumber",theForm.lead_personphonenumber.value);
	setCookie("cook_lead_personemail",theForm.lead_personemail.value);
	setCookie("cook_lead_companyaddress1",theForm.lead_companyaddress1.value);
	setCookie("cook_lead_companycity",theForm.lead_companycity.value);
	setCookie("cook_lead_companystate",theForm.lead_companystate.value);
	setCookie("cook_lead_companypostcode",theForm.lead_companypostcode.value);
	setCookie("cook_lead_details",theForm.lead_details.value);

	if(theForm.lead_companycrm.checked == true)
		setCookie("cook_lead_companycrm",'true');
	else
		setCookie("cook_lead_companycrm",'');
	
	if(theForm.lead_companycrmcom.checked)
		setCookie("cook_lead_companycrmcom",'true');
	else
		setCookie("cook_lead_companycrmcom",'');

	if(theForm.lead_companyact.checked)
		setCookie("cook_lead_companyact",'true');
	else
		setCookie("cook_lead_companyact",'');
	
}

function PreSetValues()
{
	var frm=document.getElementById("WEB2LEAD");
	frm.lead_personfirstname.value=getCookie("cook_lead_personfirstname");
	frm.lead_personlastname.value=getCookie("cook_lead_personlastname");
	frm.lead_companyname.value=getCookie("cook_lead_companyname");
	frm.lead_personphonenumber.value=getCookie("cook_lead_personphonenumber");
	frm.lead_personemail.value=getCookie("cook_lead_personemail");
	frm.lead_companyaddress1.value=getCookie("cook_lead_companyaddress1");
	frm.lead_companycity.value=getCookie("cook_lead_companycity");
	frm.lead_companystate.value=getCookie("cook_lead_companystate");
	frm.lead_companypostcode.value=getCookie("cook_lead_companypostcode");
	frm.lead_details.value=getCookie("cook_lead_details");

	frm.lead_companycrm.checked=(getCookie("cook_lead_companycrm") == 'true') ? 'checked' : '';
	frm.lead_companycrmcom.checked=(getCookie("cook_lead_companycrmcom") == 'true') ? 'checked' : '';
	frm.lead_companyact.checked=(getCookie("cook_lead_companyact") == 'true') ? 'checked' : '';
}


function PreCheckValues(f)
{
	if (getCookie("cook_lead_personfirstname") && getCookie("cook_lead_personlastname") && getCookie("cook_lead_personemail") && getCookie("cook_lead_companyname"))
	{
		if(f) { MM_openBrWindow("pdf/" + f, 'myWin',''); }
		else { MM_openBrWindow("thanks.htm", 'myWin',''); }
	}
	else
	{
		document.location.href = 'podcast-downloads.htm?f='+f;
	}
}


function setCookie(c_name,value)
{
	var expiredays=7;
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	  { 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) 
			c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
	  } 
	}
	return "";
}

function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) 
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
			return pair[1];
	} 
//	alert('Query Variable ' + variable + ' not found');
}

function MM_openBrWindow(theURL,winName,features)
{ //v2.0
  	window.open(theURL,winName,features);
}

function CreateAction1(theForm)
{
	if (CreateAction(theForm))
	{
		var f= getQueryVariable("f");
		if(f) { MM_openBrWindow("pdf/" + f, 'myWin',''); }
		else { MM_openBrWindow("thanks.htm", 'myWin',''); }
		return true;
	}
	return false;
}

function checkToChangeURL(aTag, fName)
{	
	if(getCookie('cook_lead_personfirstname') != "")
	{
		MM_openBrWindow("pdf/" + fName, 'myWin','')
		return false;
	}
	return true;
}