// 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 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;
    // 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) {
				// first check if the field is empty
				if ((e.value == null) || (e.value == "") || (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";
					}
				}
			}
		}
    }
	// 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 verifyphone(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)
		{
			thePosition=e.name.indexOf("_");
			if(-1==thePosition){
				thePosition=e.name.length;
			}
			if(e.name=='day_phone')
			{
				var day_phone=e.value;
			}
			if(e.name=='evening_phone')
			{
				var evening_phone=e.value;
			}				
		}
	}
	var theInfo=CheckPhoneNumber(day_phone);
	var theInfo2=CheckPhoneNumber(evening_phone);
	if (theInfo || theInfo2) 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 += "A 10 Digit phone number is required"+ "\n";
    alert(msg);
    return false;	
}

function CheckPhoneNumber(TheNumber) {
	var valid = 1
	var GoodChars = "0123456789()-+ "
	var nums = "0123456789"
	var i = 0
	if (TheNumber=="") {
		// Return false if number is empty
		valid = 0
	}
	var count=0;
	for (i =0; i <= TheNumber.length -1; i++) {
		if (nums.indexOf(TheNumber.charAt(i)) != -1) {
			count=count+1;
		}
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
// Note: Remove the comments from the following line to see this
// for loop in action.
// alert(TheNumber.charAt(i) + " is no good.")
			valid = 0
		} // End if statement
	} // End for loop
	if(count<10)
	{
		valid = 0
	}
	return valid
}


function addField(theKey)
{
	var theForm = document.getElementById('contact');
	var currentElement = document.createElement("input");
	currentElement.setAttribute("type", "hidden");
	currentElement.setAttribute("name", "security_key");
	currentElement.setAttribute("id", "security_key");
	currentElement.setAttribute("value", theKey);
	theForm.appendChild(currentElement);	
	/*var theHidden=document.createElement("hidden");
	theHidden.setAttribute("id", 'security_key');	
	theHidden.setAttribute("name", 'security_key');	
	theHidden.setAttribute("value", theKey);	
	var theForm = document.getElementById('contact');
	theForm.appendChild(theHidden);
	
	var num = theForm.elements.length;
	var objSource;
	for (var i = num -1 ; i >= 0; i--) 
	{
		objSource = theForm.elements[i];
		if ((objSource.type != "button")&&(objSource.type != "submit")) 
		{
			//alert(objSource.id)
			//how can I add newElement to objTargetForm?
		}
	}
	*/
	//document.getElementById('security_key').value=theKey;
}

function displayPhoneText(theElement,phone,Cl,Ca,St,Me,Pa,We,ip) 
{
	theElement.parentNode.innerHTML=phone;
	stsnd('1',Cl,Ca,St,Me,Pa,We,ip);
}

function displayWebText(theElement,url,Cl,Ca,St,Me,Pa,We,ip) 
{
	theElement.parentNode.innerHTML=url;
	stsnd('2',Cl,Ca,St,Me,Pa,We,ip);
}

function stsnd(theType,Cl,Ca,St,Me,Pa,We) 
{
		var url='http://www.onlinelawyersource.com/new/inc/click.php';
		if(theType=='1')
		{
			var poststr = "info="+Cl+"___"+Ca+"___"+St+"___"+Me+"___"+Pa+"___"+We+';';
		}
		if(theType=='2')
		{
			var poststr = "site="+Cl+"___"+Ca+"___"+St+"___"+Me+"___"+Pa+"___"+We+';';			
		}
		http_request = false;
		//alert(theType);
		if (window.XMLHttpRequest) 
		{
			http_request = new XMLHttpRequest();
			var thankyou='Are you sure that you want to leave our site?';
		} 
		else if (window.ActiveXObject) 
		{
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
			var thankyou=false;
		}
		if (!http_request) 
		{
			alert('Giving up :( Cannot create an XMLHTTP instance');
			return false;
		}
		http_request.onreadystatechange = alertContents;
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		http_request.send(poststr);
		if(thankyou)
		{
			//pausecomp(1000);
		}
}

function alertContents() 
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200|| http_request.status == 0)
		{
			//alert(http_request.responseText);
		}
	}

}

var map;
var geocoder;

function gmLoad(theAddress) {
  map = new GMap2(document.getElementById("map"));
  map.setCenter(new GLatLng(34, 0), 13);
  map.addControl(new GSmallMapControl());
  geocoder = new GClientGeocoder();
  showLocation(theAddress);return false;
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
	alert("Sorry, we were unable to geocode that address");
  } else {
	place = response.Placemark[0];
	point = new GLatLng(place.Point.coordinates[1],
						place.Point.coordinates[0]);
	marker = new GMarker(point);
	map.addOverlay(marker);
	marker.openInfoWindowHtml(place.address + '<br>' +
	  '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
  }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(theAddress) {
  geocoder.getLocations(theAddress, addAddressToMap);
}
//]]>

function displayPrice(rank,state,campaign)
{
	
	var prices = getPrices(campaign, rank);
	var theHTML='<table style="width=400px;">';
	theHTML+='<tr>';
		theHTML+='<td><h2>Premier</h2></td>';
		theHTML+='<td><h2>Featured</h2></td>';
		theHTML+='<td><h2>Basic</h2></td>';
	theHTML+='</tr>';
	theHTML+='<tr>';
		theHTML+='<td><p>$'+prices['0']+' Per Month</p></td>';
		theHTML+='<td><p>$'+prices['1']+' Per Month</p></td>';
		theHTML+='<td><p>$'+prices['2']+' Per Month</p></td>';
	theHTML+='</tr>';
	theHTML+='</table>';
	var theDiv=document.getElementById('thePricing');
	theDiv.innerHTML=theHTML;/**/
}

function getPrices(campaign, rank)
{
	if(rank>=0&&rank<=4)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(1285,785,85);
		}
		else
		{
			return Array(785,485,65);
		}
	}
	if(rank>=5&&rank<=14)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(985,625,55);
		}
		else
		{
			return Array(650,385,55);
		}
	}
	if(rank>=15&&rank<=49)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(745,445,45);
		}
		else
		{
			return Array(385,225,40);
		}
	}
	if(rank>=50&&rank<=99)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(515,280,30);
		}
		else
		{
			return Array(275,180,30);
		}
	}
	if(rank>=100&&rank<=149)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(350,180,25);
		}
		else
		{
			return Array(175,110,25);
		}
	}	
	if(rank>=150&&rank<=249)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(175,95,20);
		}
		else
		{
			return Array(125,85,15);
		}
	}	
	if(rank>=250)
	{
		if(campaign=='pi'||campaign=='mm')
		{
			return Array(100,65,15);
		}
		else
		{
			return Array(100,65,15);
		}
	}		
}