

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"+currentlanguagecode);

	if(document.forms[0].rt != null)
	{
	
	    document.forms[0].rt.value="";
	    buildForm();
	    if (prevSearch != null){
		    loadPrevSearch(prevSearch);
		    deleteCookie('MH-basicSearch'+currentlanguagecode, '', '');
	    }
	}
	deleteCookie('MH-advancedSearch', '', '');
}
//may be removed if no ratings checkbox
function checkRated(){
	var doc = document.forms[0];
	if(doc.ft !=null){
	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 "rt" : 
					formObj.rate.checked = "true";			
					continue;
				default : 
					continue;
			}	
		}	
	}
	buildForm();
	// delete the cookie
	//deleteCookie("MH-basicSearch", "/", "");
}

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.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")
	{
		isMember = "true";
		remember = document.forms[0].cr.value;
		document.forms[0].cr.value = defaultcountry;
		if (prevVal > 0 && prevVal < 56)
			document.getElementById("st").value = prevVal;
	}
	else if (isMember == "true")
	{
		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 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=" + "" + "^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 (sRated != "")
		sRated = " Rated = " + sRated;
		
			
			
	
	switch(form)
	{
	
   		case "1" : // Lawyers	
			if ((firstName == "")&&(lastName == "")&&(firmName == "")&&(city == "")&&(state == "")&&(country == "")&&(aop == "")&&(sRated=="")){
				err();
				return;
			}	
			break;
   		case "2" : // Law Firms		
			if ((firmName == "")&&(city == "")&&(state == "")&&(country == "")&&(aop == "")&&(sRated=="")){
				err();
				return;
			}		
       		break;
       	case "6" : // In-house Counsel	
			if ((firstName == "")&&(lastName == "")&&(firmName == "")&&(city == "")&&(state == "")&&(country == "")&&(aop == "")&&(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
	//debugger;

	
	setCookie("MH-basicSearch"+currentlanguagecode, cookieVals);
	//	var pname = "http://www.martindale.com/Results.aspx";
	var pname = "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].rt.value = document.forms[0].rt.value;
        document.forms[1].PRV.value = document.forms[0].PRV.value;
        document.forms[1].frm.value = "advanced";
        
	   document.forms[1].submit();
	   
	    doClear();
	}
}

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() {

if(document.forms[0].st!=null)
{
	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"+currentlanguagecode, 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;



function getCookie(name)
 { 
    var cook = document.cookie;
	var index = cook.indexOf(name + "=");
    if (index == -1) return null;
   	index = cook.indexOf("=", index) + 1;
    var endstr = cook.indexOf(";", index);
    if (endstr == -1) endstr = cook.length;
    return unescape(cook.substring(index, endstr));
}

var today = new Date();
var expiry = new Date(today.getTime() + 30 * 60 * 1000); //set to expire after 30 min.

function setCookie(name, value) 
{
   	if (value != null && value != "")
   	{
      	document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
	}
	cook = document.cookie;
}

// this deletes the cookie when called ex. deleteCookie('cookie name', '/', '')
function deleteCookie( name, path, domain )
 {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
//end cookie

