//// OTW Functions
//var ol = window.onload;

//window.onload = function() {
//	if (ol) ol();
//	  
//	  if (document.getElementById('dashboardArea').childNodes.length > 1
//	  || (document.all && document.getElementById('dashboardArea').childNodes.length == 1)) {
//	  	var isPrimary = (document.getElementById('primaryPage') != null);
//	  	//document.getElementById('leftWrapper').style.height = (isPrimary)?'900px':'750px';
//	  	
//	  }
//	  
//}

// OTW Functions
var PageImagesFolder = '/resources/1/Page Images/';
var HeaderImagePlaceHolder = 'headerImage';
var SitemapPageUrl = '/sitemap.aspx'; // Used for footer link
var pageTitle = 'Personal Injury, Insurance Attorneys, Accident Lawyer and Complex Litigation Attorneys in Sacramento, CA';
$(document).ready(function() {
	
	document.title = (document.title == 'Home') ? pageTitle : document.title + " | " + pageTitle;
	
	//document.title += " | Personal Injury, Insurance Attorneys, Accident Lawyer and Complex Litigation Attorneys in Sacramento, CA";
	
	//var pl = $('#'+HeaderImagePlaceHolder);
	//if (pl.length > 0) {
/*
		$.ajax({
			url:getURL(),
			method: 'get',
			success: function () {modifyHeaderImage(true, this.url);},
			error: function (e) {
			  if (e.status == 200) modifyHeaderImage(true, this.url);
			  else modifyHeaderImage(false, [PageImagesFolder, 'default.jpg'].join(''));
			}
		});
*/
		// Clone header links to footer
		$('.bottommenu').remove();
		var div,i,item;
		div = document.createElement('DIV');
		footerLink('/', 'Home', div);		
		for (i=0; i<menubar.srItems.length; i++) {
			item = menubar.srItems[i];
			footerLink(item.url, item.capt, div);
		}
		footerLink(SitemapPageUrl, 'Sitemap', div, true);
		$(div).prependTo('#footerLinks');
	//}

});

function footerLink(url, html, parentNode, skipSpacing) {
  var a = document.createElement('A');
  a.href = url;
  a.innerHTML = html.replace(' ','&nbsp;');
  $(parentNode).append(a);
  if(!skipSpacing) $(parentNode).append('&nbsp;| ');
}

// Clears Header Image place holder and appends a new <img> tag
function modifyHeaderImage(found, img) {
	if (found) img = getURL();
	pl = $('#'+HeaderImagePlaceHolder)[0];
	$(pl).children().remove();
	
	var i = document.createElement('IMG');
	i.src = img;
        pl.appendChild(i);
}

// Returns image's URL
function getURL() {
	var pageName = window.location.href.split('/').pop().split('.')[0];
	return [PageImagesFolder,
		pageName,
		'.jpg',
		'?r=',
		Math.random()].join('');
}

// Function retrieved from: http://cass- hacks.com/articles/code/js_url_encode_decode/
function URLEncode (clearString) {
 	var output = '';
 	var x = 0;
 	clearString = clearString.toString();
 	var regex = /(^[a-zA-Z0-9_.]*)/;
 	
	while (x < clearString.length) {
 		var match = regex.exec(clearString.substr(x));
 
 		if (match != null && match.length > 1 && match[1] != '') {
 			output += match[1];
 			x += match[1].length;
 		} else {
 			if (clearString[x] == ' ')
 			output += '+';
 		else {
 			var charCode = clearString.charCodeAt(x);
 			var hexVal = charCode.toString(16);
 			output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
 		}
 		x++;
 	}
 }
 return output;
} 

// Retrieved from: http://www.htmlcodetutorial.com/forms/index_famsupp_157.html
function submitenter(myfield, e)
{
	 var keycode;
	 
	 if (window.event)
	 	keycode = window.event.keyCode;
	 else if (e)
	 	keycode = e.which;
	 else
	 	return true;
	 
	 if (keycode == 13)
	 {
	 	SearchSite();
	 	return false;
	 }
	 else
	 	return true;
}

//Called by the Google custom search AJAX Api in site_search.aspx
function getParameterByName( name ) 
{ 
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
  var regexS = "[\\?&]"+name+"=([^&#]*)"; 
  var regex = new RegExp( regexS ); 
  var results = regex.exec( window.location.href ); 
  if( results == null ) 
    return ""; 
  else 
    return decodeURIComponent(results[1].replace(/\+/g, " ")); 
}

function SearchSite()
{
	document.location.href='http://www.kcrlegal.com/site_search.aspx?q='+
	URLEncode(document.getElementById('cse-main-search-text').value);
}

function SearchSite_UsingBuildInInsiteSearch()
{
	document.location.href='http://www.kcrlegal.com/search.aspx?q='+
	URLEncode(document.getElementById('cse-main-search-text').value);
}

//Sample from Sierra Forever Family
function SearchSite_UsingInlineGCE()  
{  
document.location.href='http://www.kcrlegal.com/site_search.aspx?cx='+
URLEncode('004024901464580080544:fxn6ydm7kxk')+'&ie=UTF-8&q=' +   
URLEncode(document.getElementById('cse-main-search-text').value) + '&cof='+
URLEncode('FORID:11')+'&sa=Search'; 
}

