var xmlHttp,forms,fields,data,url;
var attribute,method;
var ie = document.all;
var nn6 = document.getElementById &&! document.all;

function drawExtraFile(){
	
	var amount,newAmount,formMeat,extra,categorySelect
		
	amount = document.getElementById("fileamount").value;
	
	newAmount = parseInt(amount) + 1;
	
	document.getElementById("fileamount").value= newAmount;
	
	formMeat = document.getElementById("formMeat").innerHTML;
	
	extra = '<br /><br />File Name ' + newAmount + ': <input type="text" name="file_name' + newAmount + '" /><br />Choose a file to upload: <input name="uploadedfile' + newAmount + '" type="file" /><br /><br />Access : <select name="file_access' + newAmount + '"><option value="agent">Agent</option><option value="member">Member</option><option value="everyone">Everyone</option></select>';
		
	document.getElementById("formMeat").innerHTML = formMeat + extra;
	
}

function drawForm(people)
{
	//alert('Starting to draw form');
	var form,j,result,plan,price,planId;	
	j = 0;
	form = "";
	planId = document.getElementById("planId").value;
	result = document.getElementById("fam"+people);
	//alert('Grabbing data from form for PEOPLE:' + people);
	
	form = form + '<form class="quote" name="quoteForm' + people + '" id="person' + people + '"><div><fieldset><label for="gender1">Gender</label><select name="gender" id="gender' + people + '"><option value="m">Male</option><option value="f">Female</option></select></fieldset><fieldset><label for="age">Age</label><select name="age" id="age' + people + '">';
	//alert('starting loop');
	for(j=0;j<65;j++){
		form = form + '<option value="' + j + '">' + j + '</option>';
	}			
	//alert('finished loop');
form = form + "</select></fieldset><fieldset><label for='location1'>Location</label><select name='location' id='location" + people + "'><option value='1'>Dade County</option><option value='2'>Broward County</option></select></fieldset><fieldset><input type='button' name='GetPlan' id='GetPlan" + people + "' value='Get Family Quote' onclick=getFamPlan('" + people + "'); /></fieldset></div><div id='plans" + people + "'>&nbsp;</div><input type='hidden' name='type' id='type" + people + "' value='"+type+"'><input type='hidden' name='people' id='people" + people + "' value='"+people+"'><input type='hidden' name='people' id='planId' value='"+planId+"'></form><div id='fam"+(parseInt(people)+1)+"'>&nbsp;</div>";
//alert('finished form');
result.innerHTML = form;
}

function newMileage()
{ 
	//alert("ok running getPlan()");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		//alert("Your browser does not support AJAX!");
		return;
	}
	var mileage,zip,selectArea;
	mileage = document.createAccountForm.miles.value;
	zip = document.createAccountForm.zip.value;
	selectArea = document.getElementById("pcpList");
	
	if(zip.length == 5){	
		selectArea.innerHTML = "LOADING...";
		url="buildPcpSelect.php?miles=" + mileage + "&zip=" + zip;
		xmlHttp.onreadystatechange=showNewPcpSelect;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}else{
		zip.style.backgroundColor = 'red';	
	}
}

function  showNewPcpSelect() 
{ 
	if (xmlHttp.readyState==4)
	{
		var results
		results = document.getElementById("pcpList");
		results.innerHTML = xmlHttp.responseText;
	}
}


function formatPhone(number,element,action)
{ 
	//alert("ok running getPlan()");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert("Your browser does not support AJAX!");
		return;
	}
	
	
	if(number.length > 0){	
		url="formatPhone.php?number=" + number + "&element=" + element + "&action=" + action;
		//alert(url);
		xmlHttp.onreadystatechange=showFormattedPhone;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}

function  showFormattedPhone() 
{ 
	if (xmlHttp.readyState==4)
	{
		var element,results,splitter;
		
		results = xmlHttp.responseText;
		//alert(results);
		splitter = results.split("|");
		element = document.getElementById(splitter[2]);
		if(splitter[3] == 'on'){
			element.value = splitter[0];
		}else if(splitter[3] == 'off'){
			element.value = splitter[1];	
		}
		
	}
}

function formatDOB(dob,element)
{ 
	//alert("ok running getPlan()");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert("Your browser does not support AJAX!");
		return;
	}
	
	
	url="formatDOB.php?dob=" + dob + "&element=" + element;
	//alert(url);
	xmlHttp.onreadystatechange=showFormattedDOB;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function  showFormattedDOB() 
{ 
	if (xmlHttp.readyState==4)
	{
		var element,results,splitter;
		
		results = xmlHttp.responseText;
		//alert(results);
		splitter = results.split("|");
		element = document.getElementById(splitter[2]);
		if(splitter[1].length > 2){
			element.value = splitter[1];
		} else {
			if(splitter[0].length > 1){
				element.value = splitter[1];	
			} else {
				element.value = "MM/DD/YYYY";	
			}
			
		}
		
	}
}


function getPlan()
{ 
	//alert("ok running getPlan()");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		//alert("Your browser does not support AJAX!");
		return;
	}
	var age,gender,location;
	age 		= document.quoteForm.age1.value;
	gender 		= document.quoteForm.gender1.value;
	location 	= document.quoteForm.location1.value;
	type 		= document.quoteForm.type.value;
	people 		= document.quoteForm.people.value;
	//alert("Age:" + age + " Gender:" + gender + " Local:" + location + " Type:" + type + " People:" + people);
	url="getQuote.php?age=" + age + "&gender=" + gender + "&location=" + location + "&type=" + type + "&people=" + people;
	xmlHttp.onreadystatechange=bringPlan;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getFamPlan(people)
{ 
	//alert("ok running getFamPlan()");
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		//alert("Your browser does not support AJAX!");
		return;
	}
	//alert('setting variables');
	var age,gender,location,form,planId;
	//alert('Variables set PEOPLE:' + people);
	
	age 		= document.getElementById("age"+people).value;
	gender 		= document.getElementById("gender"+people).value;
	location 	= document.getElementById("location"+people).value;
	type 		= document.getElementById("type"+people).value;
	planId		= document.getElementById("planId").value;
	//alert("Age:" + age + " Gender:" + gender + " Local:" + location + " Type:" + type + " People:" + people + " PlanID:" + planId);
	if(parseInt(people) > 1){
		url="getQuote.php?age=" + age+ "&gender=" + gender + "&location=" + location + "&type=" + type + "&people=" + people + "&planId=" + planId + "&next=t";
	}else{
		url="getQuote.php?age=" + age+ "&gender=" + gender + "&location=" + location + "&type=" + type + "&people=" + people;
	}
	xmlHttp.onreadystatechange=bringPlan;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getFlexPlan(people)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert("Your browser does not support AJAX!");
		return;
	}
	var age,gender,location,form,planId;
	//alert('Variables set PEOPLE:' + people);
	
	age 		= document.getElementById("age"+people).value;
	gender 		= document.getElementById("gender"+people).value;
	location 	= document.getElementById("location"+people).value;
	planId		= document.getElementById("planId").value;
	//alert("Age:" + age + " Gender:" + gender + " Local:" + location + " People:" + people + " PlanID:" + planId);
	if(parseInt(people) > 1){
		url="getFlexQuote.php?age=" + age+ "&gender=" + gender + "&location=" + location + "&people=" + people + "&planId=" + planId + "&next=t";
	}else{
		url="getFlexQuote.php?age=" + age+ "&gender=" + gender + "&location=" + location + "&people=" + people + "&planId=" + planId;
	}
	xmlHttp.onreadystatechange=bringFlexPlan;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function  bringPlan() 
{ 
	if (xmlHttp.readyState==4)
	{
		var splitter,results,showPlanResults;
		showPlanResults = "";
		splitter = "";
		splitter = xmlHttp.responseText;
		//alert("This is the text back from the bringPlan Javascript FUNCTION:" + splitter);
		splitter = splitter.split("@@");
		//alert("Ok SPLITTER SPLIT:" + splitter[0] + " 2:" + splitter[1] + " 3:" + splitter[2]);
		results = document.getElementById("plans"+splitter[0]);
		//alert("Results: plans" + splitter[0].trim());
		results.innerHTML = splitter[1];
		if(splitter[2] != null){
			var planId,age,gender,location,people,type;
			showPlanResults = splitter[2].split(":");
			planId = showPlanResults[0];
			age	= showPlanResults[1];
			gender = showPlanResults[2];
			location = showPlanResults[3];
			people = showPlanResults[4];
			type = showPlanResults[5];
			
			if(type == 'F' && people > 1){
				showPlan(planId,gender,type,age,people,people,'plan','y','1');
			}
		}
		
	}
}


function  bringFlexPlan() 
{ 
	if (xmlHttp.readyState==4)
	{
		var splitter,results,showPlanResults;
		splitter = "";
		showPlanResults = "";
		splitter = xmlHttp.responseText;
		//alert("This is the text back from the bringPlan Javascript FUNCTION:" + splitter);
		splitter = splitter.split("@@");
		results = document.getElementById("plans"+splitter[0]);
		results.innerHTML = splitter[1];
		if(splitter[2] != null){
			var planId,age,gender,location,people,type;
			showPlanResults = splitter[2].split(":");
			planId = showPlanResults[0];
			age	= showPlanResults[1];
			gender = showPlanResults[2];
			location = showPlanResults[3];
			people = showPlanResults[4];
			type = showPlanResults[5];
			
			if(people > 1){
				showFlexPlan(planId,gender,type,age,people,people,'plan','y','1');
			}
		}
		
	}
}

function showPlan(id,gender,type,age,people,total,option,optVal,recalculate)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		//alert("Your browser does not support AJAX!");
		return;
	}
	
	var getPlanButton = document.getElementById("GetPlan"+people);
	//getPlanButton.disabled = "true";
	//alert("ID:" + id + " Gender:" + gender + " Type:" + type + " People:" + people + " Total People:" + total + " Option:" + option + " Opt Val:" + optVal + " Recalculate:" + recalculate);
	url="getPlan.php?id=" + id + "&gender=" + gender + "&age=" + age + "&type=" + type + "&people=" + people + "&total=" + total + "&option=" + option + "&optval=" + optVal + "&recalculate=" + recalculate;
	xmlHttp.onreadystatechange=displayPlan;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showFlexPlan(id,gender,type,age,people,total,option,optVal,recalculate)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		//alert("Your browser does not support AJAX!");
		return;
	}
	
	var getPlanButton = document.getElementById("GetPlan"+people);
	//getPlanButton.disabled = "true";
	//alert("ID:" + id + " Gender:" + gender + " Type:" + type + " People:" + people + " Total People:" + total + " Option:" + option + " Opt Val:" + optVal + " Recalculate:" + recalculate);
	url="getFlexPlan.php?id=" + id + "&gender=" + gender + "&age=" + age + "&type=" + type + "&people=" + people + "&total=" + total + "&option=" + option + "&optval=" + optVal + "&recalculate=" + recalculate;
	xmlHttp.onreadystatechange=displayFlexPlan;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function displayPlan() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		
		var text = "";
		var responseValue = xmlHttp.responseText;
		var results = document.getElementById("col2");
		var subtotal,splitter,array,size,name,price,description,type,people,peopleTotal,total,iddataarray,gender,age,i,maxCount,counter,categoryName,ageStart,ageEnd;
		
		//alert('Made it through the variables ' + responseValue);
		//Get the value back from the query ID:NAME:PRICE:TYPE:PEOPLE:AGE
		iddataarray = responseValue.split("@@");
		id = trim(iddataarray[0]);
		array	= iddataarray[1].split("@");
		total = 0;
		peopleTotal = 0;
		i = 0;
		counter = 1;
		
		maxCount = array.length;
		text = "<form name=\"registerPlanForm\" action=\"quote.php\" method=\"POST\"><input type=\"hidden\" name=\"action\" value=\"CREATEACCOUNT\"><input type=\"hidden\" name=\"id\" id=\"planId\" value=\""+ id + "\"><div id=\"quoteDetails\"><div id=\"quoteTop\">";
		//alert('starting loop');
		for(i=0;i<maxCount;i++){	
			splitter = array[i].split("|");
			name = splitter[0];
			price = splitter[1];
			description = splitter[2];
			type = splitter[3];
			age = splitter[4];
			people = splitter[5];
			totalPeople = splitter[6];
			gender = splitter[7];
			categoryName = splitter[9];
			ageStart = splitter[10];
			ageEnd = splitter[11];
			
			total = parseInt(total) + parseInt(price);
			subtotal = 0;
			
			//alert('split up the response data for Register Form: ID:' + id + " NAME:" + name + " PRICE:" + price + " DESC:" + description + " TYPE:" + type + " AGE: " + age + " PEOPLE:" + people + " GENDER:" + gender);
			
			var dental = document.getElementById("dental"+counter);
			var optical = document.getElementById("optical"+counter);
			var planoptions = document.getElementById("planoptions"+counter);
			var optionsresult = "";
			var planPic = "";
			
			if(name == 'Plan A'){
				planPic = "images/planAQuote.png";
			}else if(name == 'Plan B'){
				planPic = "images/planBQuote.png";
			}			
			
			if(i == 0){
				text = text + '<input type="hidden" name="type" id="type" value="'+ type + '"><div id="planName">' + categoryName + ' ' + name + '</div><div class="clear"></div></div><div id="quoteBody"><div id="quoteDescription"><p>' + description + '</p><hr/>';	
			}
			
			if(people == 1){
				totalPeople = 2;	
			}
			
			text = text + '<div id="person"><input type="hidden" name="gender'+people+'" id="gender'+people+'" value="' + gender + '"><input type="hidden" name="age'+people+'" id="age'+people+'" value="' + age + '"><div class="title"> <strong>Applicant ' + people + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$' + price + '</strong></div><br />';
			//alert('Got the first part of the text: \n' + text);
			subtotal = parseInt(subtotal) + parseInt(price);
			
			 optionsresult = "<div class=\"planCategory\">\n<div class=\"categoryTitle\">Optional Supplemental Plans</div><div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=dental id='dental"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);\"></div><div class=\"newPlanName\">Dental Plan</div><div class=\"newPlanPrice\">$6</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=dental',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div><div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=optical id='optical"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value);\"></div><div class=\"newPlanName\">Optical Plan</div><div class=\"newPlanPrice\">$5</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=optical',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>\n</div>";	
			
			if(dental.checked){
			 	text = text + 'Dental Plan &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$6<br><input type="hidden" name="dental'+counter+'" value="y" />';
			 	total = parseInt(total) + 6;
				subtotal = parseInt(subtotal) + 6;
				//optionsresult = "<p>Optional Plan Supplements<br><input type=checkbox name=dental id='dental"+counter+"' value=n onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);\" CHECKED>Dental Plan<br>";
				optionsresult = "<div class=\"planCategory\">\n<div class=\"categoryTitle\">Optional Supplemental Plans</div><div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=dental id='dental"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);\" CHECKED></div><div class=\"newPlanName\">Dental Plan</div><div class=\"newPlanPrice\">$6</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=dental',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>";
				
				if(optical.checked){
					optionsresult = optionsresult + "<div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=optical id='optical"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value);\" CHECKED></div><div class=\"newPlanName\">Optical Plan</div><div class=\"newPlanPrice\">$5</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=optical',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>\n</div>";
				}else{
					optionsresult = optionsresult + "<div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=optical id='optical"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value);\"></div><div class=\"newPlanName\">Optical Plan</div><div class=\"newPlanPrice\">$5</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=optical',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>\n</div>";
				}
			}else{
				text = text + '<input type="hidden" name="dental'+counter+'" value="n" />';	
			}
			
			//alert("Got Dental? : \n" + text);
			if(optical.checked){
				text = text + 'Optical Plan &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$5<br><input type="hidden" name="optical'+counter+'" value="y" />';
				total = parseInt(total) + 5;	
				subtotal = parseInt(subtotal) + 5;
				optionsresult = "<div class=\"planCategory\">\n<div class=\"categoryTitle\">Optional Supplemental Plans</div>";
				if(dental.checked){
					optionsresult = optionsresult + "<div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=dental id='dental"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);\" CHECKED></div><div class=\"newPlanName\">Dental Plan</div><div class=\"newPlanPrice\">$6</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=dental',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>";
				}else{
					optionsresult = optionsresult + "<div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=dental id='dental"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);\"></div><div class=\"newPlanName\">Dental Plan</div><div class=\"newPlanPrice\">$6</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=dental',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>";
				}
				optionsresult = optionsresult + "<div class=\"newPlan\">\n<div class=\"radioButton\"><input type=checkbox name=optical id='optical"+counter+"' value=y onclick=\"javascript:showPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value);\" CHECKED></div><div class=\"newPlanName\">Optical Plan</div><div class=\"newPlanPrice\">$5</div><div class=\"readMore\"><a href=\"javascript:TINY.box.show('summaryBenefits.php?b=optical',1,0,0,1);\"><img src='images/magnifyingGlass.png' /> Read More</a></div>\n</div>\n</div>";				
			}else{
				text = text + '<input type="hidden" name="optical'+counter+'" value="n" />';
			}
			
			text = text + 'Sub Total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$' + subtotal + "<hr /></div>";
			//alert("Got Optical? : \n" + text);
			counter = counter + 1;
		 }
		 if(type == 'I'){
 		 text = text + '<div id="submitOptions"><div class="familyTotal"><strong>Total</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>$' + total + '</strong></div><div class=\"quoteMessage\">Call us now at 305-648-4008 if you have any questions or if you would like to enroll in Preferred Medical Plan.</div><br /><input type="submit" name="submit" value="" class="forminfobutton"><div class="clear"></div></div></div></div></div></div><div id="quoteBottom"></div></div>'; 	
		 }else if(type == 'F'){
			if(parseInt(maxCount) < 2){
				text = text + '<div id="submitOptions"><div class="familyTotal"><strong>Total</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>$' + total + '</strong></div><div class=\"quoteMessage\">Call us now at 305-648-4008 if you have any questions or if you would like to enroll in Preferred Medical Plan.</div><br />';
				text = text + "<input type=button name=add value='   Add Person' class='formaddPersonbutton' onclick=\"Javascript:window.location='quote.php?a=F&next=t&planId=" + id + "&total=" + totalPeople + "'\"><input type='submit' name='submit' value='' disabled='true' class='forminfobutton' ><div class='clear'></div></div></div></div></div><div id='quoteBottom'></div></div>";	
			}else{
				text = text + '<div id="submitOptions"><div class="familyTotal"><strong>Total</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>$' + total + '</strong></div><div class=\"quoteMessage\">Call us now at 305-648-4008 if you have any questions or if you would like to enroll in Preferred Medical Plan.</div><br />';
				text = text + "<input type=button name=add value='   Add Person' class='formaddPersonbutton' onclick=\"Javascript:window.location='quote.php?a=F&next=t&planId=" + id + "&total=" + totalPeople + "'\"><input type='hidden' name='totalCount' value='" +totalPeople+"' /><input type='submit' name='submit' value='' class='forminfobutton'><input type=button name=startover value='   Start Over' onclick=Javascript:window.location='quote.php?a=F&action=STARTOVER' class='formstartoverbutton'><div class='clear'></div></div></div></div></div><div id='quoteBottom'></div></div>";	
			}		
		 }
		 //alert('finished loop');
		 text = text + '</form>'; 	
		
		//alert("Got Optical? : \n" + text);
		//alert('Made the text form response');
		//alert(text);
		results.innerHTML = text;
		planoptions.innerHTML = optionsresult;
	}
}



function displayFlexPlan() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		
		var text = "";
		var responseValue = xmlHttp.responseText;
		var results = document.getElementById("col2");
		var subtotal,splitter,array,size,name,price,description,type,people,peopleTotal,total,iddataarray,gender,age,i,maxCount,counter;
		
		//alert('Made it through the variables ' + responseValue);
		//Get the value back from the query ID:NAME:PRICE:TYPE:PEOPLE:AGE
		iddataarray = responseValue.split("@@");
		id = iddataarray[0];
		array	= iddataarray[1].split("@");
		total = 0;
		peopleTotal = 0;
		i = 0;
		counter = 1;
		
		maxCount = array.length;
		text = '<form name="registerPlanForm" action="flex.php" method="POST"><input type="hidden" name="action" value="CREATEACCOUNT"><input type="hidden" name="id" id="planId" value="'+ id + '"><div id="quoteDetails"><div id="quoteTop">';
		//alert('starting loop');
		for(i=0;i<maxCount;i++){	
			splitter = array[i].split("|");
			name = splitter[0];
			price = splitter[1];
			description = splitter[2];
			type = splitter[3];
			age = splitter[4];
			people = splitter[5];
			totalPeople = splitter[6];
			gender = splitter[7];
			total = parseInt(total) + parseInt(price);
			subtotal = 0;
			
			//alert('split up the response data for Register Form: ID:' + id + " NAME:" + name + " PRICE:" + price + " DESC:" + description + " TYPE:" + type + " AGE: " + age + " PEOPLE:" + people + " GENDER:" + gender);
			
			var dental = document.getElementById("dental"+counter);
			var optical = document.getElementById("optical"+counter);
			var planoptions = document.getElementById("planoptions"+counter);
			var optionsresult = "";
			var planPic = "";
			if(name == 'MediFlex'){
				planPic = "images/planFxQuote.png";
			}else if(name == 'MediFlex Plus'){
				planPic = "images/planFlexPlusQuote.png";
			}else if(name == 'MediFlex Plus Urgent'){
				planPic = "images/planFlexUrgentQuote.png";
			}
			
			if(i == 0){
				text = text + '<div id="planIcon"><img src="' + planPic + '"/></div><input type="hidden" name="type" id="type" value="'+ type + '"><div id="planName">' + name + '</div><div class="clear"></div></div><div id="quoteBody"><div id="quoteDescription"><p>' + description + '</p></div><hr/>';	
			}
			
			if(people == 1){
				totalPeople = 2;	
			}
			
			text = text + '<div id="person"><input type="hidden" name="gender'+people+'" id="gender'+people+'" value="' + gender + '"><input type="hidden" name="age'+people+'" id="age'+people+'" value="' + age + '"><div class="title"> <strong>Applicant ' + people + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$' + price + '</strong></div><br />';
			//alert('Got the first part of the text: \n' + text);
			subtotal = parseInt(subtotal) + parseInt(price);
			
			 optionsresult = "<p>Optional Plan Supplements<br><input type=checkbox name=dental id='dental"+counter+"' value=y onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);>Dental Plan<br /><input type=checkbox name=optical id='optical"+counter+"' value=y onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value);>Optical Plan<br /></p>";	
			
			if(dental.checked){
			 	text = text + 'Dental Plan &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$6<br><input type="hidden" name="dental'+counter+'" value="y" />';
			 	total = parseInt(total) + 6;
				subtotal = parseInt(subtotal) + 6;
				optionsresult = "<p>Optional Plan Supplements<br><input type=checkbox name=dental id='dental"+counter+"' value=n onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value); CHECKED>Dental Plan<br>";
				
				if(optical.checked){
					optionsresult = optionsresult + "<input type=checkbox name=optical id='optical"+counter+"' value=n onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value); CHECKED>Optical Plan<br /></p>";
				}else{
					optionsresult = optionsresult + "<input type=checkbox name=optical id='optical"+counter+"' value=y onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value);>Optical Plan<br /></p>";
				}
			}else{
				text = text + '<input type="hidden" name="dental'+counter+'" value="n" />';	
			}
			
			//alert("Got Dental? : \n" + text);
			if(optical.checked){
				text = text + 'Optical Plan &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$5<br><input type="hidden" name="optical'+counter+'" value="y" />';
				total = parseInt(total) + 5;	
				subtotal = parseInt(subtotal) + 5;
				optionsresult = "<p>Optional Plan Supplements<br>";
				if(dental.checked){
					optionsresult = optionsresult + "<input type=checkbox name=dental id='dental"+counter+"' value=n onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value); CHECKED>Dental Plan<br />";
				}else{
					optionsresult = optionsresult + "<input type=checkbox name=dental id='dental"+counter+"' value=y onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','dental',this.value);>Dental Plan<br />";
				}
				optionsresult = optionsresult + "<input type=checkbox name=optical id='optical"+counter+"' value=n onclick=javascript:showFlexPlan('" + id + "','" + gender + "','" + type + "','" + age + "','" + counter + "','" + counter + "','optical',this.value); CHECKED>Optical Plan<br /></p>";				
			}else{
				text = text + '<input type="hidden" name="optical'+counter+'" value="n" />';
			}
			
			text = text + 'Sub Total&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$' + subtotal + "<hr /></div>";
			//alert("Got Optical? : \n" + text);
			counter = counter + 1;
		 }

		if(parseInt(maxCount) < 2){
			text = text + '<div id="submitOptions"><strong>Total</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>$' + total + '</strong><br />';
			text = text + "<input type=button name=add value='   Add Person' class='formaddPersonbutton' onclick=Javascript:window.location='flex.php?action=FORM&next=t&id=" + id + "&total=" + totalPeople + "'><input type='submit' name='submit' value='   Register' class='formregisterbutton' ><div class='clear'></div></div></div><div id='quoteBottom'></div></div>";	
		}else{
			text = text + '<div id="submitOptions"><strong>Total</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>$' + total + '</strong><br />';
			text = text + "<input type=button name=add value='   Add Person' class='formaddPersonbutton' onclick=Javascript:window.location='flex.php?action=FORM&next=t&id=" + id + "&total=" + totalPeople + "'><input type='hidden' name='totalCount' value='" +totalPeople+"' /><input type='submit' name='submit' value='   Register' class='formregisterbutton'><input type=button name=startover value='   Start Over' onclick=Javascript:window.location='flex.php?id="+ id + "&action=STARTOVER' class='formstartoverbutton'><div class='clear'></div></div></div><div id='quoteBottom'></div></div>";	
		}		
		text = text + '</form>'; 	
		
		results.innerHTML = text;
		planoptions.innerHTML = optionsresult;
	}
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function setPass(id){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		//alert("Your browser does not support AJAX!");
		return;
	}
	
	var div = document.getElementById("pass"+id);
	var data = document.getElementById("newpass"+id);
	
	url="ajax.php?action=changeagentpass&aid="+id+"&np="+data.value;
	//xmlHttp.onreadystatechange=displayFlexPlan;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	
	var result = xmlHttp.responseText;
	
	div.innerHTML = "<input type=\"button\" name=\"passchanger\" value=\"Change\" onclick=\"changePass('"+id+"');\" >";
	if(result == "success"){
		div.style.bgColor = "#ECECEC";
	} else {
		div.style.bgColor = "#FFFF00";	
	}
}

function changePass(id){ 
	var div = document.getElementById("pass"+id);	
	div.innerHTML = "<input type=\"text\" id=\"newpass"+id+"\" /> <input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"cancelPass('"+id+"');\" > <input type=\"button\" name=\"passchanger\" value=\"Save\" onclick=\"setPass('"+id+"');\" >";
}

function cancelPass(id){ 
	var div = document.getElementById("pass"+id);	
	div.innerHTML = "<input type=\"button\" name=\"passchanger\" value=\"Change\" onclick=\"changePass('"+id+"');\" >";
}


//Generic XML HTTP Object
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
