// Initialize window onload function
window.onload = setVideoPopUp

// BEGIN GLOBAL VARIABLES
	var visibleLayer = null;
	var rootdomain="http://"+window.location.hostname

// END GLOBAL VARIABLES

// BEGIN FUNCTIONS
function transfer_form_values( form_name  )
{
	var source_form  = document.forms[form_name];
	
	for (var i = 0; i < document.forms.length; i++)
	{
		var f = document.forms[i];
		if ( f.FirstName ) f.FirstName.value = source_form.firstName.value;
		if ( f.LastName ) f.LastName.value = source_form.lastName.value;
		if ( f.Email ) f.Email.value = source_form.email.value;
		if ( f.WhereLive ) f.WhereLive.selectedIndex = source_form.WhereLive.selectedIndex;
	}
	
}



function track_click( a_tag )
{
	
	if ( document.images )
	{
		var log_url = "/outgoing/" + a_tag.href.replace( 'http://', '' );
		( new Image()).src = log_url;
		
	}
	return true;
	
}

function ajaxinclude(url) {
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else
		return false
	page_request.open('GET', url, false) //get page synchronously 
	page_request.send(null)
	writecontent(page_request)
}

function writecontent(page_request){
	if (window.location.href.indexOf("http")==-1 || page_request.status==200)
	document.write(page_request.responseText)
}

function setExternalCategory( category_footprint, source_id )
{
	
	if ( opener.LegalIssueChanged )
	{
		opener.LegalIssueChanged( category_footprint );
	}
	else
	{
		opener.document.getElementById(source_id).innerHTML = category_footprint;
		opener.document.getElementById("practiceArea").value = category_footprint;

	}
	
	self.close();
}

function setVideoPopUp()
{
	var vidLink = document.getElementsByName('VideoLink')
	for (var i = 0; i < vidLink.length; i++)
	{
		vidLink[i].onclick = videoPopUp
	}
}

function videoPopUp()
{
	var url = this.href

	// Open new window
	var videoWindow = window.open(url, 'videoWindow', 'scrollbars=yes,resizable=yes,width=505,height=400')
		
	// Don't let the anchor element change the page
	return false
}

// Validate Need An Attorney Form
function validateForm( form_name )
{
	// Regular expressions for the form validation
	var isAlpha = /^[a-z ']+$/i
	var isNum = /^\d+$/
	var isAllSpace = /^\s+$/
	var isEmail = /^[\w\d-_]+@[\w\d\.-_]+$/
	
	var f = document.forms[form_name];
		
	if (f.firstName.value == "")
	{
		alert("Please tell us your first name.")
		return false
	}
	if (f.lastName.value == "")
	{
		alert("Please tell us your last name.")
		return false
	}
	if (!f.email.value.match(isEmail))
	{
		alert("Not a valid email.")
		return false
	}
	if (f.WhereLive.value == "0")
	{
		alert("Please let us know what state you live in.")
		return false
	}
	if (f.practiceArea.value == "")
	{
		alert("Please select a legal issue.")
		return false
	}
}

function validateEmailFriendForm()
{
	var isEmail = /^[\w\d-_]+@[\w\d\.-_]+$/
	
	var f = document.forms['form2'];
	
	if (!f.friend_email.value.match(isEmail))
	{
		alert("Friend's email is not valid.")
		return false
	}
	if (!f.your_email.value.match(isEmail))
	{
		alert("Your email is not valid.")
		return false
	}


}

// BEGIN SIDE NAV
function changeFlyout(layerName)
{
	if(layerName == null || layerName == '')
	{
		return;
	}

	if (visibleLayer == null)
	{
		expand(layerName);
		visibleLayer = layerName;
		return;
	}

	if (visibleLayer == layerName)
	{
		expand(layerName);
		visibleLayer = null;
		return;
	}
	expand(visibleLayer);
	expand(layerName);
	visibleLayer = layerName;
	return;
}
	
function showLayer(layerName){
	if(layerName == null || layerName == '')
	{
		return;
	}
	expand(layerName);
}
	
function setOverflow( id, overflow )
{
	if (document.all) 
	{
		document.all[id].style.overflow = overflow;
	}
	else
	{
		document.getElementById(id).style.overflow = overflow;
	}
	
}

function delayedShowLayer( id, wait_time )
{
	var referrer = Url.decode( document.referrer );
	if (( referrer.match( "lawyer|laywer|attorney|atorney|attorny|law.firm|law.suit|lawsuit|legal" ) ) && 
		( !referrer.match( "legalview" ) ) )
	{	
		window.setTimeout( "showLayer('" + id + "')", wait_time );
	}
}	

function injury_change( dropdown, desc_box_name, short_height, long_height, columns )
{
	var desc_box = get_control_by_id(desc_box_name) ;

	if ( dropdown.selectedIndex == 1 ) 
	{
		desc_box.style.display = '';
	}
	else
	{
		desc_box.style.display = 'none';
	}

	
	
	for ( var i = 0; i < columns.length; i++ )
	{
		var column = get_control_by_id( columns[i] );
		
		if (column)
		{
				if ( dropdown.selectedIndex == 1 ) 
				{
					column.style.height = long_height;
				}
				else
				{
					column.style.height = short_height;
				}
			
		}
		
						
	
	}

	
	
}

function get_control_by_id(x)
{
	if (document.all) 
	{
		c = document.all[x];
	}
	else
	{
		c = document.getElementById(x);
	}

	return c;
}
	
function expand(x) {
  if (document.all) {
    if (document.all[x].style.display == 'none') {
      document.all[x].style.display = ''
    } else if (document.all[x].style.display == '') {
      document.all[x].style.display = 'none';
    }
  } else {
		

    if (document.getElementById(x).style.display == 'none') {
      document.getElementById(x).style.display = ''
    } else if (document.getElementById(x).style.display == '') {
      document.getElementById(x).style.display = 'none';
    }
  }
}

//-->
// END SIDE NAV

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

// END FUNCTIONS
