// JavaScript Document

// clears and replaces text in form input fields and textareas
// -----------------------------------------------------------------
function clearText(thefield) {
  if (thefield.defaultValue==thefield.value) { thefield.value = "" }
} 
function replaceText(thefield) {
  if (thefield.value=="") { thefield.value = thefield.defaultValue }
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function bookmark()
{
bookmarkurl="http://www.onlinelawyersource.com"
bookmarktitle="Online Lawyer Source"
if (document.all)
window.external.AddFavorite(bookmarkurl,bookmarktitle)
}

function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}
// performs form verification, invoked from the onSubmit() event handler. 
// The handler should return whatever value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
	var thePosition;
	var email;
	//f.security_key.value=key
    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for invalid fields.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
		if(e.name)
		{
			thePosition=e.name.indexOf("_");
			if(-1==thePosition){
				thePosition=e.name.length;
			}
			if(e.name.substring(0,thePosition).toLowerCase()=='email')
			{
				email=e.name;
			}		
	//        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
			if (e.type == "text" && !e.optional && substr_count($text, 'formfield') == 0) {
				// first check if the field is empty
				if ((e.value == null) || (e.value == "") || (e.value == "Name") || (e.value == " First Name*") || (e.value == " Last Name*") || (e.value == " Address*") || (e.value == " City*") || (e.value == " Email*") ||isblank(e.value)) {
	
					empty_fields += "\n          " + e.name.substring(0,thePosition);
					continue;
				}
				// Now check for fields that are supposed to be numeric.
				if (e.numeric || (e.min != null) || (e.max != null)) { 
					var v = parseFloat(e.value);
					if (isNaN(v) || 
						((e.min != null) && (v < e.min)) || 
						((e.max != null) && (v > e.max))) {
						errors += "- The field " + e.name + " must be a number";
						if (e.min != null) 
							errors += " that is greater than " + e.min;
						if (e.max != null && e.min != null) 
							errors += " and less than " + e.max;
						else if (e.max != null)
							errors += " that is less than " + e.max;
						errors += ".\n";
					}
				}
			}
			else if(!e.optional && substr_count($text, 'formfield') == 0)
			{
				if ((e.value == null) || (e.value == "") ||isblank(e.value)) {
	
					empty_fields += "\n          " + e.name.substring(0,thePosition);
					continue;
				}				
			}
		}
    }
	// check to see if email address field is of valid syntax
	substremail = document.getElementById(email).value.split("@")
	if (substremail.length > 1)
	{
		index = substremail[1].indexOf(".")
		if (index == -1)
 		{
                    errors += "- please enter a valid e-mail address\n";
		}
	}
	else {
                    errors += "- please enter a valid e-mail address\n";
//		alert("Please enter a valid e-mail address")
//		document.form1.email.focus()
//		return false
	}
    // if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    if (!empty_fields && !errors) return true;
    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following empty field(s).\n";
    msg += "Please fill out all required fields and re-submit.\n";
    msg += "______________________________________________________\n\n"
    if (empty_fields) {
        msg += "The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}


function verifystate(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
	var thePosition;
	var email;	
	for(var i = 0; i < f.length; i++) 
	{
		var e = f.elements[i];
		if(e.name)
		{
			if(e.name=='State')
			{
				if(e.value!='State'&&e.value!='')
				{
					return true;	
				}
			}			
		}
	}
    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following empty field(s).\n";
    msg += "Please fill out all required fields and re-submit.\n";
    msg += "______________________________________________________\n\n"
    msg += "Selecting a state is required"+ "\n";
    alert(msg);
    return false;	
}

function getURL(theForm)
{
	var searchBox = theForm.theSection;
	var area = searchBox.options[searchBox.selectedIndex].value;
	if(area=='default')
	{
		alert('Please choose an area of law.');
		return false;			
	}	
	if(theForm.ZipCode.value=='')
	{
		alert('Please Select a State');
		return false;
	}
	if(theForm.additionalSelecttheSection)
	{
		var area = theForm.additionalSelecttheSection.options[theForm.additionalSelecttheSection.selectedIndex].value;		
		switch(area)
		{
			case 'bail_bond': 
				theForm.action = 'http://www.onlinelawyersource.com/other/bail_bond/contact.html';								
			break;
			case 'workers_compensation':
				theForm.action = 'http://www.onlinelawyersource.com/other/workers_compensation/contact.html';						
			break;
			case 'meridia':
				theForm.action = 'http://www.onlinelawyersource.com/drug_recall/meridia/contact.html';						
			break;
			case 'fen_phen':
				theForm.action = 'http://www.onlinelawyersource.com/drug_recall/fen_phen/contact.html';						
			break;
			default:
				theForm.action = 'http://www.onlinelawyersource.com/'+area+'/contact.html';					
		}
	}
	else if(theForm.additionalSelecttheSection2)
	{
		var area = theForm.additionalSelecttheSection2.options[theForm.additionalSelecttheSection2.selectedIndex].value;		
		switch(area)
		{
			case 'bail_bond': 
				theForm.action = 'http://www.onlinelawyersource.com/other/bail_bond/contact.html';								
			break;
			case 'workers_compensation':
				theForm.action = 'http://www.onlinelawyersource.com/other/workers_compensation/contact.html';						
			break;
			case 'meridia':
				theForm.action = 'http://www.onlinelawyersource.com/drug_recall/meridia/contact.html';						
			break;
			case 'fen_phen':
				theForm.action = 'http://www.onlinelawyersource.com/drug_recall/fen_phen/contact.html';						
			break;
			default:
				theForm.action = 'http://www.onlinelawyersource.com/'+area+'/contact.html';					
		}
	}	
	//else if(searchBox.options[searchBox.selectedIndex].value=='divorce')
	//{
	//	theForm.action = 'http://www.onlinelawyersource.com/divorce/family-law/'+replaceAll( theForm.ZipCode.value, "-", "_" )+'_contact.html';
	//	theForm.method = 'get';
		//theForm.action = 'http://www.onlinelawyersource.com/divorce/family-law/contact.html';
	//}
	else if(searchBox.options[searchBox.selectedIndex].value=='product_liability')
	{
		theForm.action = 'http://www.onlinelawyersource.com/liability/index.html';		
	}
	else if(searchBox.options[searchBox.selectedIndex].value=='drug_recall')
	{
		theForm.action = 'http://www.onlinelawyersource.com/drug_recall/drug_recall.html';		
	}
	else if(searchBox.options[searchBox.selectedIndex].value=='more_legal_areas')
	{
		theForm.action = 'http://www.onlinelawyersource.com/other/index.html';		
	}
	else{
		theForm.action = 'http://local.onlinelawyersource.com/'+theForm.ZipCode.value;
		theForm.theSection.name='thecampaign';
		theForm.method='GET';
		//theForm.action = 'http://www.onlinelawyersource.com/new/local/state.php';
	}
}

function changeMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );


    while ( idx > -1 ) {
        str = str.replace( from, to ); 
        idx = str.indexOf( from );
    }

    return str;
}