var configuratorHttp;
var enrollHttp;



function configuratorChanged() {
        if (configuratorHttp.readyState==4 || configuratorHttp.readyState=="complete")
         {
		var resp = configuratorHttp.responseText;

		var match = /^<!\-\-SUCCESS:(\d+)\-/.exec(resp.substr(0,50));
		if (match) {
			var zip_code = document.forms['cf'].zip_code.value;
                	document.getElementById("questionarea").innerHTML=resp;
			display_form(match[1], zip_code);	
		} else {
			match = /^<!\-\-FAIL\-/.exec(resp.substr(0,50));
			if (match) { 
                		document.getElementById("questionarea").innerHTML=resp;
			} else { 
	                	document.getElementById("configurator").innerHTML=resp;
                		document.getElementById("enrollment-ajax").innerHTML="";
			}
		}
         }
}
function displayEnrollment() {
        if (enrollHttp.readyState==4 || enrollHttp.readyState=="complete")
         {
                document.getElementById("enrollment-ajax").innerHTML=enrollHttp.responseText;
         }
}


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 (e2)
                  {
                        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                  }
         }
        return xmlHttp;
}


function update_questions(src) {
	configuratorHttp=GetXmlHttpObject();
        if (configuratorHttp===null)
        {
                alert ("Browser does not support HTTP Request");
                return;
        }

        var url = "ajax.php";
        url = url+"?sid="+Math.random();
	var zip_code = src.zip_code.value;
	url = url+"&zip_code="+zip_code;
	var els = src.elements;
	var q = "";
	for (var i=0; i<els.length; i++) {
		if (/^q[0-9]+$/.exec(els[i].name)) {
			if (els[i].checked) { 
				if (q.length > 0) 
					q += ",";
				q += els[i].value;
			}
		}
	}
	if (q.length > 0) {
		url = url + "&q="+q;
	}
        configuratorHttp.onreadystatechange=configuratorChanged;
        configuratorHttp.open("GET",url,true);
        configuratorHttp.send(null);



	return false;
}
function start_over() {

        document.getElementById("enrollment-ajax").innerHTML="";
	document.getElementById("questionarea").innerHTML = '<table class="tablewide" border="0" cellspacing="0" cellpadding="0" style="border-top: 2px solid #d9d9d9;"><tr><td valign="top" class="summary" colspan="4" style="font-size: 1.3em; line-height: 1.4em; color: #000; font-weight: normal;">Shopping for a new plan? Let our Plan Chooser help you decide which Physicians Health Choice plan is right for you. Simply answer a few easy questions and the Plan Chooser will direct you to the plan that is best suited for your needs. Questions? Please call us at 1-866-658-9456 (TTY users may dial 1-800-290-9905). We are available Monday-Friday between the hours of 8:00 am and 5:00 pm.</td></tr><tr><td valign="top" class="full" colspan="4"><form name="cf" id="cf" method="post" action="#" onsubmit="return false;"><span style="display: block; width: 100px; float: left; padding: 6px;"><label for="zip_code"><strong>Zip Code: </strong></label></span><div style="float: left;"><input type="text" id="zip_code" name="zip_code" autocomplete="off" onclick="lookup(this);" maxlength="5" onkeyup="lookup(this);"><br /><div class="suggestionsBox" id="suggestions" style="display: none;"><div class="suggestionList" id="autoSuggestionsList"></div></div></div><div style="clear: both;"></div><div id="configurator"></div></form></td></tr></tbody></table>';
	update_questions(document.forms['cf']);
	return false;
}

function display_form(plan, zip_code) {
	enrollHttp=GetXmlHttpObject();
        if (enrollHttp === null)
        {
                alert ("Browser does not support HTTP Request");
                return;
        }
        url = "form.php";
        url = url+"?sid="+Math.random();
	url = url+"&plan="+plan;
	url += "&zip_code="+zip_code;
/*
        enrollHttp.onreadystatechange=displayEnrollment;
        enrollHttp.open("GET",url,true);
        enrollHttp.send(null);
*/
}

function validate_form(src) {
	var required  = new Array(
				"County",			//0
				"Plan-Choice",			//1
				"Last-Name",			//2
				"First-Name",			//3
				"Middle-Initial",		//4
				"Birth-Date",			//5
				"Sex",				//6
				"Home-Phone-Number",		//7
				"Email-Address",		//8
				"Permanent-Residence",		//9
				"Permanent-City",		//10
				"Permanent-State",		//11
				"Permanent-Zip",		//12
				"Emergency-Contact",		//13
				"Emergency-Phone-Number",	//14
				"Emergency-Relationship"	//15
				
			);
	var minlength = new Array(
				"2",				//0
				"1",				//1
				"2",				//2
				"2",				//3
				"1",				//4
				"5",				//5
				"0",				//6
				"5",				//7
				"6",				//8
				"5",				//9
				"3",				//10
				"2",				//11
				"5",				//12	
				"2",				//13
				"2",				//14
				"2"				//15
			);
	var first = false;
	for (var i=0; i<required.length; i++) {
		var els = document.getElementsByName(required[i]);
		var el = els[0];
		var table = document.getElementById("Table-"+required[i]);
		if (el && el.type == "text") { 
		    if (el.value.length < parseInt(minlength[i])) { 
			if (table && table.style) { 
				el.style.border = "2px solid #9c1c33";
				el.style.background = "#fcfcfc";
				table.style.background = "#ececec";
				if (!first) {
					first = required[i];
				}
			}
		    } else {
			if (table && table.style) { 
				el.style.border = "2px solid #cccccc";
				el.style.background = "#ffffff";
				table.style.background = "#ffffff";
			}
		    }
		} else if (el && (el.type=="radio" || el.type=="checkbox") ) {
			var checked = false;
			for (var j=0; j<els.length; j++) {
				if (els[j].checked) {
					checked = true;
					break;
				}	
			}
			if (!checked) {
				if (table && table.style) { 
					table.style.background = "#ececec";
					if (!first) {
						first = required[i];
					}
				}
			} else {
				if (table && table.style) { 
					table.style.background = "#ffffff";
				}
			}
		}
	}
	if (first) {
		var el = document.getElementById(first);
		if (el) {
			var yPos = findYPos(el);
			window.scroll(0,yPos);
		}
		alert('The highlighted fields are required.  Please correct this and resubmit the form.');
	}
	return false;
}

function findYPos(obj) {
	// set initial offset
	var yPos = -50;
	if (obj.offsetParent) {
		do {
			yPos += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return (yPos > 0) ? yPos : 0;
}
