
//used to submit form when user clicks enter key commented out until new API ready
/*
var pressedEnter = false;
function IE(){
	if ((window.event && window.event.keyCode == 13)&&(event.srcElement.type != "")) {
		pressedEnter = true;
		doSearch();
	}	
}
function NS(e){
	if ((e.which == 13)&&(e.target.type != "")){
		pressedEnter = true;
		doSearch();
	}
}
if (document.all){
	document.onkeydown=IE;
}else{
	document.captureEvents(Event.ONKEYDOWN);
	document.onkeydown=NS;
}
*/
//end submit code

function changeActionAndMethodOnFormAndSubmit(newAction, newMethod)
{
    var theForm = document.forms['mainform'];
    if (!theForm) {
        theForm = document.Form1;
    }
    theForm.action = newAction;
    theForm.method = newMethod;
    theForm.submit();
}


function frminit(){	
    document.forms[0].reset();
	var prevSearch 	= getCookie("MH-basicSearch");
	document.forms[0].rt.value="";
	buildForm();
	if (prevSearch != null){
		loadPrevSearch(prevSearch);
		deleteCookie('MH-basicSearch', '', '');
	}
	deleteCookie('MH-advancedSearch', '', '');
}
//may be removed if no ratings checkbox
function checkRated(){
	var doc = document.forms[0];
	var form = doc.ft.value;
	if (form=="1"||form=="2"){
		var val = doc.cr.options[doc.cr.selectedIndex].value; 
		if (!((val == 1)||(val == 2)||(val == 3)||(val == 4)||(val == 5)||(val == 202)||(val == ""))){
			doc.rate.disabled = true;
		} else {
			doc.rate.disabled = false;
		}
	}
}
function loadPrevSearch(args){
	var formObj		= document.forms[0];
	var arParams	= args.split("^");
	var val			= "";	
	for (a = 0; a < arParams.length; a++){
		val = arParams[a].split("=");
		val1 = unescape(val[1]);	
		if (val1 != ""){ 
			switch(val[0])
			{
				//formType?
				case "k2" : 
					document.forms[0].k2.value = val1;
					continue;
				case "ft" : 
					formObj.ft.value = val1;
					continue;
				case "fn" : 
					formObj.fn.value = val1;
					continue;
				case "ln" : 
					formObj.ln.value = val1;
					continue;
				case "nm" : 
					formObj.nm.value = val1;
					continue;
				case "ct" : 
					formObj.ct.value = val1;
					continue;
				case "st" : 
					formObj.st.value = val1;
					continue;
				case "cr" : 
					formObj.cr.value = val1;
					continue;
				case "pa" : 
					formObj.pa.value = val1;
					continue;
				case "kw" : 
					if(val1==""){
						formObj.kw.value = "Enter Other";
					}else{
						formObj.kw.value = val1;
					}
					continue;
				case "rt" : 
					formObj.rate.checked = "true";			
					continue;
				default : 
					continue;
			}	
		}	
	}
	buildForm();
	// delete the cookie
	//deleteCookie("MH-basicSearch", "/", "");
}
function buildForm(){
	var formObj		= document.forms[0];
	var form = document.forms[0].ft.value;
	switch(form)
	{
   		case "1" : // Lawyers		
			showState("false");			
			showLabel("block","none","none","none","block","none");
			disableFields(false,false,false,false,false,false,false,false,false);
			formObj.fn.focus();
			break;
   		case "2" : // Law Firms		
			showState("false");
			showLabel("block","none","none","none","block","none");
			disableFields(true,true,false,false,false,false,false,false,false);		
			formObj.nm.focus();	
       		break;
       	case "6" : // In-house Counsel		
			showState("false");			
			showLabel("none","none","block","none","none","block");	
			disableFields(false,false,false,false,false,false,false,false,false);
			formObj.fn.focus();
			break;
		case "3" : // Corporate Legal Department	
			showState("false");
			showLabel("none","none","block","none","block","none");			
			disableFields(true,true,false,false,false,false,true,true,true);
			formObj.nm.focus();
			break;			
   		case "4" : // U.S. Government
			showState("true");
			showLabel("none","block","none","none","block","none");
			disableFields(false,false,false,true,true,true,true,true,true);
			formObj.fn.focus();
			break;
		case "5" : //U.S. Law School Faculty Members
			showState("true");
			showLabel("none","none","none","block","block","none");
			disableFields(false,false,false,false,false,true,true,true,true);
			formObj.fn.focus();
			break;
   		default : // Not valid form submission
			break;
	}
}
function disableFields(bool,bool2,bool3,bool4,bool5,bool6,bool7,bool8,bool9){
	var formObj = document.forms[0];	
	formObj.fn.disabled 		= bool;
	grayOut(formObj.fn,bool);
	formObj.ln.disabled 		= bool2;
	grayOut(formObj.ln,bool2);
	formObj.nm.disabled 		= bool3;
	grayOut(formObj.nm,bool3);
	formObj.ct.disabled 		= bool4;
	grayOut(formObj.ct,bool4);
	formObj.st.disabled 		= bool5;
	formObj.cr.disabled 		= bool6;
	formObj.pa.disabled 		= bool7;
	formObj.kw.disabled 		= bool8;
	grayOut(formObj.kw,bool8);
	formObj.rate.disabled 		= bool9;
}
function grayOut(obj,bool){
	if (bool == true){
		obj.style.backgroundColor = "#EEEEEE";
	}else{
		obj.style.backgroundColor = "";
	}
}
function showLabel(val,val2,val3,val4,val5,val6){
	document.getElementById("div1").style.display = val;
	document.getElementById("div2").style.display = val2;
	document.getElementById("div3").style.display = val3;
	document.getElementById("div4").style.display = val4;
	document.getElementById("aop1").style.display = val5;
	document.getElementById("aop2").style.display = val6;
}
var isMember = "true";
var remember = "";
function showState(bool){
	var prevVal = document.getElementById("st").value;
	if(bool == "true"){
		document.getElementById("sp1").style.display = "none";
		document.getElementById("sp2").style.display = "block";
		loadStates(document.getElementById("st"));
		isMember = "true";
		remember = document.forms[0].cr.value;
		document.forms[0].cr.value = "1";
		if (prevVal > 0 && prevVal < 56)
			document.getElementById("st").value = prevVal;
	}else if (isMember == "true"){
		document.getElementById("sp1").style.display = "block";
		document.getElementById("sp2").style.display = "none";
		loadAllStatesProvinces(document.getElementById("st"));
		isMember = "false";
		document.forms[0].cr.value = remember;
		document.getElementById("st").value = prevVal;
	}
	
}
//This will change with the new Endeca API
function doSearch(){
	var formObj		= document.forms[0];
	replaceDiacriticals(formObj.fn);
	replaceDiacriticals(formObj.ln);
	replaceDiacriticals(formObj.nm);
	replaceDiacriticals(formObj.ct);
	var firstName 	= trimString(formObj.fn.value);
	if (!(checkWildCard(firstName))){
		return;
	}
	var lastName 	= trimString(formObj.ln.value);
	if (!(checkWildCard(lastName))){
		return;
	}
	var firmName 	= trimString(formObj.nm.value);
	if (!(checkWildCard(firmName))){
		return;
	}
	var city  		= trimString(formObj.ct.value);
	var state		= formObj.st.value;
	var country		= formObj.cr.value;
	var aop			= formObj.pa.value;
	var other		= formObj.kw.value;
	if(other=="Enter Other"){
		other="";
		formObj.kw.value="";
	}
	var sRated		= checkit(formObj.rate); // ratind
	if (sRated == "Y"){
		formObj.rt.value = "Y";
	}
	var form = formObj.ft.value;
	
	//build cookie values
	var cookieVals = "ft=" + form + "^fn=" + firstName + "^ln=" + lastName + "^nm=" + firmName + "^ct=" + city + "^st=" + state + "^cr=" + country + "^pa=" + aop + "^kw=" + other + "^rt=" + sRated;
	// TBD var stype		= "B";
	
	//Build parameters
	if (firstName != "")
		firstName = " First Name = " + firstName;
	if (lastName != "")
		lastName = " Last Name = " + lastName;
	if (firmName != "")
		firmName = " Firm/School/Gov/Company Name = " + firmName;
	if (city != "")
		city = " City = " + city;
	if (state != "")
		state = " State = " + state;
	if (country != "")
		country = " Country = " + country;
	if (aop != "")
		aop = " Area of Practice = " + aop;
	if (other != "")
		other = " Enter Other = " + other;
	if (sRated != "")
		sRated = " Rated = " + sRated;
		
				
	
	switch(form)
	{
   		case "1" : // Lawyers	
			if ((firstName == "")&&(lastName == "")&&(firmName == "")&&(city == "")&&(state == "")&&(country == "")&&(aop == "")&&(other == "")&&(sRated=="")){
				err();
				return;
			}	
			break;
   		case "2" : // Law Firms		
			if ((firmName == "")&&(city == "")&&(state == "")&&(country == "")&&(aop == "")&&(other == "")&&(sRated=="")){
				err();
				return;
			}		
       		break;
       	case "6" : // In-house Counsel	
			if ((firstName == "")&&(lastName == "")&&(firmName == "")&&(city == "")&&(state == "")&&(country == "")&&(aop == "")&&(other == "")&&(sRated=="")){
				err();
				return;
			}
			break;
		case "3" : // Corporate Legal Department	
			if ((firmName == "")&&(city == "")&&(state == "")&&(country == "")){
				err();
				return;
			}
			break;			
   		case "4" : // Government
			if ((firstName == "")&&(lastName == "")&&(firmName == "")){
				err();
				return;
			}
			break;
		case "5" : // U.S. Law School Faculty Members
			if ((firstName == "")&&(lastName == "")&&(firmName == "")&&(city == "")&&(state == "")){
				err();
				return;
			}	
			break;
   		default : // Not valid form submission
			break;
	}
	// before submit set cookie
	setCookie("MH-basicSearch", cookieVals);
	//var pname = "/Results" + ".a" + "spx";
	var pname = "http://www.martindale.com/Results" + ".a" + "spx";
	//"http://www.martindale.com/Results" + ".a" + "spx";
	if(document.forms[0] && document.forms[1]){
	    document.forms[0].action = pname;
        document.forms[1].ft.value = document.forms[0].ft.value;
        document.forms[1].fn.value = document.forms[0].fn.value;
        document.forms[1].ln.value = document.forms[0].ln.value;
        document.forms[1].nm.value = document.forms[0].nm.value;
        document.forms[1].ct.value = document.forms[0].ct.value;
        document.forms[1].st.value = document.forms[0].st.value;
        document.forms[1].cr.value = document.forms[0].cr.value;
        document.forms[1].pa.value = document.forms[0].pa.value;
        document.forms[1].kw.value = document.forms[0].kw.value;
        document.forms[1].rt.value = document.forms[0].rt.value;
        document.forms[1].PRV.value = document.forms[0].PRV.value;

	    document.forms[1].submit();
	    doClear();
	}
}
function err(){
/*
	if (pressedEnter){
		pressedEnter = false;
		doArt();
	}else{*/
		alert("You must first enter your search criteria before submitting this form.");
	//}
}
function checkit(obj){
	var bool;
	if (obj.checked){
		bool = "Y";
	}else{
		bool = "";
	}
	return bool;
}
function prevChecked(val, obj){
	if((val=="Y")||(val=="10")){
		obj.checked = true;
	}else{
		obj.checked = false;		
	}
}
function checkState(){
	var doc = document.forms[0];
	var val = doc.st.options[doc.st.selectedIndex].value; 
	if (val != "") { 
		var isCanada = false;
		for(i=0; i<canada.length; i++){
			if (canada[i][0] == val){
				isCanada = true;
			}
		}
		if (isCanada){
			doc.cr.options[2].selected = true;
		}else{//USA
			doc.cr.options[1].selected = true;
		}
	}else{
		doc.cr.options[0].selected = true;
	}
	checkRated();
}
function checkCountry() {
	document.forms[0].st.options[0].selected = true;
	checkRated();
}
function setValue(){
	if(document.forms[0].kw.value!="Enter Other"){
		document.forms[0].kw.value="Enter Other";
		document.forms[0].kw.style.color = "#999999";
		doOnce = true;
	}
}
var doOnce = true;
function aopValue(obj, lim){
	if(trimString(obj.value)!=""){
		if (doOnce){
			document.forms['forms[0]'].pa[0].selected = true;
			doOnce = false;
		}
		validateSize(obj, lim);
	}
}
function doArt(){
	var formObj		= document.forms[0];
	replaceDiacriticals(formObj.k2);
	var art 	= trimString(formObj.k2.value);
	if (art==""){
		alert("You must first enter your search criteria before submitting this form.");
	}else{
	    //build cookie values
	    var cookieVals = "k2=" + art;
	    // before submit set cookie
	    setCookie("MH-basicSearch", cookieVals);
		var pname = "http://www.martindale.com/legal-articles/Results" + ".a" + "spx";
	    formObj.action = pname;
	    
    	if(document.forms[0] && document.forms[2]){
	        document.forms[2].rt.value = document.forms[0].rt2.value;
	        document.forms[2].wr.value = document.forms[0].wr.value;
	        document.forms[2].k2.value = document.forms[0].k2.value;
	        document.forms[2].PRV.value = document.forms[0].PRV2.value;	    
	        document.forms[2].submit();
	    }
	}
	
}
function checkKw(obj){
	if (obj.value == "Enter Other"){
		obj.value = "";
		obj.style.color = "#333333";
	}
}
function doClear(){
	var val = document.forms[0].ft.value;
	document.forms[0].reset();
	document.forms[0].ft.value = val;
	buildForm();
}
window.onload = frminit;