var context ="";
var contactInfo = "";
var captchaVal = "";
function validate()
{
	
	var txtFirstName = document.getElementById("fname");
	if (txtFirstName.value.length == 0)
	{
		alert("Please enter your first name.");
		txtFirstName.focus();
		return(false);
	}
	contactInfo = "&firstname=" + txtFirstName.value;
	var txtLastName = document.getElementById("lname");
	if (txtLastName.value.length == 0)
	{
		alert("Please enter your last name.");
		txtLastName.focus();
		return(false);
	}
	contactInfo = contactInfo + "&lastname=" + txtLastName.value;
	var txtAddress = document.getElementById("address");
	if (txtAddress.value.length == 0)
	{
		alert("Please enter your address.");
		txtAddress.focus();
		return(false);
	}
	contactInfo = contactInfo + "&address=" + txtAddress.value;
	var txtCity = document.getElementById("city");
	if (txtCity.value.length == 0)
	{
		alert("Please enter your city.");
		txtCity.focus();
		return(false);
	}
	contactInfo = contactInfo + "&city=" + txtCity.value;
	var txtState = document.getElementById("state");
	if (txtState.value == 0)
	{
		alert("Please enter your state.");
		txtState.focus();
		return(false);
	}
	contactInfo = contactInfo + "&state=" + txtState.value;
	var txtZip = document.getElementById("zip");
	if (txtZip.value.length == 0)
	{
		alert("Please enter your zip.");
		txtZip.focus();
		return(false);
	}
	contactInfo = contactInfo + "&zip=" + txtZip.value;
	var txtPhone = document.getElementById("phone");
	if (!(isPhoneNumber(txtPhone.value)))			
	{
		alert("Please enter in a valid phone number");				
		txtPhone.focus();
		return(false);
	}
	contactInfo = contactInfo + "&phone=" + txtPhone.value;
	var txtCountry = document.getElementById("country");
	if (txtCountry.value.length > 0)			
	{				
		contactInfo = contactInfo + "&country=" + txtCountry.value;
	}
	var ddlBestTime = document.getElementById("besttime2Call");
	if (ddlBestTime.value.length > 0)			
	{				
		contactInfo = contactInfo + "&BestTime=" + ddlBestTime.value;
	}
	var txtEmail = document.getElementById("email");
	if (!isEmail(txtEmail.value))
	{
		alert("Please enter in a valid email address");				
		txtEmail.focus();
		return(false);
	}
	else
	{				
		contactInfo = contactInfo + "&email=" + txtEmail.value;
	}
	return(true);
	
}
function showMoreInfoPanel(chk,url,contextName)
{
	if (chk.checked)
	{
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
	 	{
			alert ("Browser does not support HTTP Request")
	 		return;
	 	}
	 	chk.parentNode.className="chkPanel";	 
		context = contextName;
		xmlHttp.async = false;
		if (contextName == "divMeds")
			xmlHttp.onreadystatechange=stateChangedMeds
		if (contextName == "divSecurities")
			xmlHttp.onreadystatechange=stateChangedTkr;
		if (contextName == "divProducts")
			xmlHttp.onreadystatechange=stateChangedProd;
		xmlHttp.open("GET","/includes/" + url);
		xmlHttp.send(null)
	}
	else	
	{
	 	chk.parentNode.className="";
		div = document.getElementById(contextName)
		if (div != null)
		{
			div.innerHTML = "";
			div.className = "divDDLBoxNothing";
			div.style.height = "";
		}
	}
}
function showMeds(chk)
{
	return;
	showMoreInfoPanel(chk, 'initMedList.php', 'divMeds');
	/*
	if (chk.checked)
	{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 	chk.parentNode.className="chkPanel";	 
		context = "divMeds";	
		xmlHttp.async = false; 
		xmlHttp.onreadystatechange=stateChangedMeds
		xmlHttp.open("GET","/includes/initMedList.php")
		xmlHttp.send(null)
	}
	else	
	{
	 	chk.parentNode.className="";
		div = document.getElementById('divMeds')
		if (div != null)
		{
			div.innerHTML = "";
			div.className = "divDDLBoxNothing";
			div.style.height = "";
		}
	}
	*/
}
var timerId = 0;
var txtBoxId = '';
var focusAttempts;
function setTextFocus()
{
	txt = document.getElementById(txtBoxId);
	if (txt != null)
	{
		txt.focus;		
	}
	else
	{
		setTimeout("setTextFocus", 1000);
	}
	if (focusAttempts++ > 5)
	{
		clearTimeout ( timerId );
	}		
}
function stateChangedMeds()
{
	try
	{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
		 div = 	 document.getElementById(context);
		 if (div != null)
		 {
			div.className = "divMiscInfoSection";
		 	div.innerHTML = xmlHttp.responseText ;
		 	txtBoxId = "txtMedName";
		 	txt = document.getElementById(txtBoxId);
		 	if (txt != null)
		 	{
			 	txt.focus();
		 	}
			 else
			 {
			 	timerId = setTimeout("setTextFocus", 1000);
		 }
	 	}
		 else
		 {
		 	//alert('missing block' + context);
	 	}
	 }
 	} 
	catch(err)
	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function checkForSubmit(txt)
{}
function showList(txt,ddlName, contextName,url)
{
	e = window.event;
	try
	{
		if(e.keyCode == 40)
		{
			ddl = document.getElementById(ddlName);
			if (ddl != null)
			{
				txt.value = ddl.options[0].value;
				ddl.focus();
				ddl.options[0].selected = true;
			}
			return;		
		}
	}
	catch(err)
	{}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
	 ddl = document.getElementById(ddlName);
	 if (ddl == null)
	 {
		 txt.parentNode.parentNode.style.height="200px";
	}
	context = contextName;	
	div = document.getElementById(context);
	if (div != null)
	{
		if (txt != null)
		{			
			val = txt.value;
			if (val.length==0)
			{
				 txt.parentNode.style.height="";
			}			
		}
		else
		{
			val = '';
		}
	}	
	xmlHttp.onreadystatechange=processShowList 
	xmlHttp.open("GET","/includes/" + url + txt.value)
	xmlHttp.send(null)
}

function showMedList(txt)
{
	showList(txt, 'ddlMeds', 'divMedList', 'showMedList.php?med=')
}
function processShowList()
{
	try
	{ 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			div = 	 document.getElementById(context);
			if (div != null)
			{
				if (xmlHttp.responseText.length > 0)
				{
					div.innerHTML=xmlHttp.responseText;
				}
				else
				{
					div.innerHTML=xmlHttp.responseText;
					switch(div.id.substring(3,4))
					{
						case "M":
						case "P":
						case "S":
							break;
						default:
						alert(div.id.substring(3,4))
					}
				}
				
			}
			else
			{
				alert('missing block' + context);
			}
		}
 	} 
	catch(err)
	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}	
}
function removeMedList()
{
	context = "divMedList";	
	div = document.getElementById(context);
	if (div != null)
		div.className = "divDDLBoxNothing";
		div.parentNode.style.height = "0px";
}
function showPersonalInjury()
{
//notavailable()
}
function showProducts(chk)
{
	return;
	showMoreInfoPanel(chk, 'initProductLiabilityList.php', 'divProducts');

}
function showProdLiabList(txt)
{
	showList(txt, 'ddlProds', 'divProdLiabList', 'showProductLiabilityList.php?prod=')
}
function stateChangedProd()
{
	try
	{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
		 div = 	 document.getElementById(context);
		 if (div != null)
		 {
			div.className = "divMiscInfoSection";
		 	div.innerHTML = xmlHttp.responseText ;
		 	txtBoxId = "txtProdLiabName";
		 	txt = document.getElementById(txtBoxId);
		 	if (txt != null)
		 	{
			 	txt.focus();
		 	}
			 else
			 {
			 	timerId = setTimeout("setTextFocus", 1000);
		 }
	 	}
		 else
		 {
		 	//alert('missing block' + context);
	 	}
	 }
 	} 
	catch(err)
	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function show401K()
{
}
function showSecurities(chk)
{
	return;
	showMoreInfoPanel(chk, 'initTkrList.php', 'divSecurities');
	/*	
	context = "divSecurities";	
	if (chk.checked)
	{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	 {
	 alert ("Browser does not support HTTP Request")
	 return
	 }
		xmlHttp.async = false; 
		xmlHttp.onreadystatechange=stateChangedTkr
		xmlHttp.open("GET","/includes/initTkrList.php")
		xmlHttp.send(null)
	}
	else	
	{
		div = document.getElementById(context)
		if (div != null)
		{
			div.innerHTML = "";
			div.className = "divDDLBoxNothing";
		}
			
	}
	*/
}

function stateChangedTkr()
{
	try
	{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 { 
		 div = 	 document.getElementById(context);
		 if (div != null)
		 {
			div.className = "divMiscInfoSection";
		 	div.innerHTML = xmlHttp.responseText ;
		 	txtBoxId = "txtTkrName";
		 	txt = document.getElementById(txtBoxId);
		 	if (txt != null)
		 	{
			 	txt.focus();
		 	}
			 else
			 {
			 	timerId = setTimeout("setTextFocus", 1000);
		 }
	 	}
		 else
		 {
		 	//alert('missing block' + context);
	 	}
	 }
 	} 
	catch(err)
	{
		alert(err.description);
		alert(xmlHttp.responseText);
	}
}
function showTkrList(txt)
{
	showList(txt, 'ddlTicker', 'divSecuritiesList', 'showTkrList.php?tkr=')
}

function removeTkrList()
{
	context = "divSecurities";	
	div = document.getElementById(context);
	if (div != null)
		div.className = "divDDLBoxNothing";
}
function checkToRemove(ddl)
{
	if (ddl.id == 'ddlTicker')
	{		
		txtBoxId = 'txtTkrName';
		divId = 'divSecurities';
	}
	if (ddl.id == 'ddlProds')
	{
		txtBoxId = 'txtProdLiabName';
		divId = 'divProducts';
	}
	if (ddl.id == 'ddlMeds')
	{
		txtBoxId = 'txtMedName'
		divId = 'divMeds';
	}
	txt = document.getElementById(txtBoxId );
	if (ddl.value == txt.value)
		ddl.parentNode.innerHTML = "";
	div = document.getElementById(divId);	
	div.style.height = "";
}
function showOtherInvestment(chk)
{
//notavailable()
}
function showMalpractice(chk)
{}
function showBankruptcy(chk)
{
//notavailable()
}
function showOther(chk)
{
//notavailable()
}
function notavailable()
{
	alert('This has not been implemented yet');
}

function ddlFillTextBox(ddl)
{	
	if (ddl.id == 'ddlTicker')
		txtBoxId = 'txtTkrName'
	if (ddl.id == 'ddlMeds')
		txtBoxId = 'txtMedName'
	if (ddl.id == 'ddlProds')
		txtBoxId = 'txtProdLiabName'
	
	txt = document.getElementById(txtBoxId);
	if (txt != null)
	{
		txt.value = ddl.value;
	}		
}
function closeDiv(div)
{
	if(div != null)
	{
		div.innerHTML = "";
		div.className = "showNothing";
		div.parentNode.style.height= "";
	}
}
function refreshCaptcha()
{
	img = document.getElementById("imgCaptcha");
	// add an extension to make the system think it's been changed
	if (img != null)
		img.src = "/images/create_Captcha.php?" + Math.random();
	 
}
function updateCaptcha()
{
	img = document.getElementById("imgCaptcha");
	if (img != null)
		img.src = "/images/create_Captcha.php";
}

/*************** CONSTANTS DON'T CHANGE OR ASSIGN ***********/
var USE_OF_MEDICATION = 1;
var STOCK_MARKET_LOSS = 2;
var DEFECTIVE_PRODUCT = 3;
var PERSONAL_INJURY = 4;
var MEDICAL_MALPRACTICE = 5;
var A401K_LOSS = 6;
var OTHER_INVESTMENT_LOSS = 7;
var BANKRUPTCY = 8;
var OTHERLAWYER = 9;
var FALSEADVERTISING = 10;
/***********************************************************/

function submitReferal()
{
	if (!validate())
	{
		return false;
	}
	txtDesc = document.getElementById('txtExplanation');	
	if (txtDesc != null && txtDesc.value.length > 255)
	{
		alert("you'll need to be briefer");
		return false;
	}
	// is a check boxed checked;
	nLawSuits = 0;
	chks = document.getElementsByName('chkLawyerType');
	medName = "";
	stockName = "";
	productLName = "";
	otherName = "";
	lTypes = "";
	if (chks != null)
	{
		for (var nCheck = 0; nCheck < chks.length; nCheck++)
		{
			if (chks[nCheck].checked)
			{
				switch(parseInt(chks[nCheck].value ))
				{
					case USE_OF_MEDICATION: 
						++nLawSuits;
						// if the check box is med did the say which med
						txt = document.getElementById('txtMedName');								
						if  ( txt != null && (txt.value.length > 0))
						{ 
							medName = "&medName=" + txt.value;
						}
						else
						{
							/*** 
							alert('Please select a medication');
							if  ( txt != null)
								txt.focus()
							return false;
							***/
							medName = "" ;
						}
						break;
					case STOCK_MARKET_LOSS:
						++nLawSuits;
						// if the check box is stock market did they say which stock
						txt = document.getElementById('txtTkrName');								
						if  ( txt != null && (txt.value.length > 0))
						{ 
							stockName = "&stockName=" + txt.value;
						}
						else
						{
							/********
							alert('Please select a stock');
							if  ( txt != null)
								txt.focus()
							return false;
							********/
							stockName = "";
						}
						break;
					case DEFECTIVE_PRODUCT: 
						++nLawSuits;
						// if the check box is product did they say which product
						txt = document.getElementById('txtProdLiabName');								
						if  ( txt != null && (txt.value.length > 0))
						{ 
							productLName = "&productLName=" + txt.value;
						}
						else
						{
							/************
							alert('Please select a product');
							if  ( txt != null)
								txt.focus()
							return false;
							*********/
							productLName = "";
						}
						break;
					case PERSONAL_INJURY: 
					case MEDICAL_MALPRACTICE: 
					case A401K_LOSS: 
					case OTHER_INVESTMENT_LOSS: 
					case BANKRUPTCY: 
					case FALSEADVERTISING:
		// need to insert an other box for uses to enter
					case OTHERLAWYER: 
						++nLawSuits;
					break;
				}
				if (lTypes.length == 0)
					lTypes = chks[nCheck].value;
				else
					lTypes = lTypes + ',' + chks[nCheck].value ;

			}
		}
	}
	if (nLawSuits == 0)
	{
		alert("Select a type of lawyer you want.");
		chks[0].focus();
		return false;
	}
	// did they enter a valid code;
	txtCode = document.getElementById('txtCaptcha');	
	if (txtCode != null && (txtCode.value.length > 0))
	{
		code = txtCode.value;
	}
	else
	{
		alert("Please enter the code you see at the bottom.");
		if (txtCode != null)
			txtCode.focus();
		return false;
	}		
	
	// if you have reached this point the initial validations should be good
	context = "submittalResults";
	div = document.getElementById(context)
	div.innerHTML = "submitting...";
	xmlHttp=GetXmlHttpObject()		
	var url = "includes/submitReferral.php";
	var params = "codeVal=" + code + contactInfo + "&count=" + nLawSuits + "&lTypes=" + lTypes + medName + stockName + otherName + productLName;
	if (txtDesc != null)	
	{
		params = params + "&desc="+txtDesc.value;
	}
	xmlHttp.open("POST", url, true);
	
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.send(params);	
	// notavailable();
}
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		alert ("too many characters. please be briefer")
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}

