// Auto-tab from one field to the next
function TabNext(strStartField,strEndField) {

	try {
		// Determine if length of value equals maxlength
		if (strStartField.getAttribute && strStartField.value.length == strStartField.getAttribute("maxlength"))
			// Put focus on the element passed
			document.getElementById(strEndField).focus();
			//document.getElementById(strEndField).select();
	} catch(e) {}
}

// alert language to be placed on all email links
function EmailAlert() {
	return confirm('No attorney-client relationship with Hodgson Russ will be established by sending e-mail to us. Receipt of correspondence or information does not create a lawyer-client relationship between you and Hodgson Russ LLP or any of its individual attorneys, and any such information received will not be deemed confidential or privileged.   Do not send us personal information or information relating to a case, transaction, or potential representation until you speak with one of our lawyers and get authorization to send that information to us.');
}


// find all email links and attach a title and the function with the alert
AttachEmailAlert = function() {
	var elements = document.getElementsByTagName('A');
	
	for (i=0;i<elements.length;i++){
		if(elements[i].href.substring(0,7) == 'mailto:') {
			elements[i].onclick=function(){return EmailAlert();}
			elements[i].title = "No attorney-client relationship with Hodgson Russ will be established by sending e-mail to us. Receipt of correspondence or information does not create a lawyer-client relationship between you and Hodgson Russ LLP or any of its individual attorneys, and any such information received will not be deemed confidential or privileged.   Do not send us personal information or information relating to a case, transaction, or potential representation until you speak with one of our lawyers and get authorization to send that information to us.";
		} else {}
	}
}

if(window.addEventListener) window.addEventListener('load', AttachEmailAlert, false); // gecko, safari, konqueror and standard
else if(document.addEventListener) document.addEventListener('load', AttachEmailAlert, false); // opera 7
else if(window.attachEvent) { // win/ie
	window.attachEvent('onload', AttachEmailAlert);
} else { // mac/ie5
	if(typeof window.onload == 'function') {
		var existing = onload;
		window.onload = function() {
			existing();
			AttachEmailAlert();
		}
	} else {
		window.onload = function() {
			AttachEmailAlert();
		}
	}
}

function ClearSearch() {
	try {
		var element = window.document.getElementById('SearchKeywords');
		if(element.value=='Search'){
			element.value='';
		}else{}
	} catch(e) {}
}

function WriteSearch() {
	try {
		var element = window.document.getElementById('SearchKeywords');
		if(element.value==''){
			element.value='Search';
		}else{}
	} catch(e) {}
}


ReplaceSearch = function() {
	try {
		var elements = window.document.getElementById('SearchKeywords');
		if(window.addEventListener){	
			elements.addEventListener('focus', ClearSearch, true);
			elements.addEventListener('blur', WriteSearch, true);	
		}else{
			elements.attachEvent("onfocus",ClearSearch);
			elements.attachEvent("onblur",WriteSearch);
		}
	} catch(e) {}
}


if(window.addEventListener) window.addEventListener('load', ReplaceSearch, false); // gecko, safari, konqueror and standard
else if(document.addEventListener) document.addEventListener('load', ReplaceSearch, false); // opera 7
else if(window.attachEvent) { // win/ie
	window.attachEvent('onload', ReplaceSearch);
} else { // mac/ie5
	if(typeof window.onload == 'function') {
		var existing = onload;
		window.onload = function() {
			existing();
			ReplaceSearch();
		}
	} else {
		window.onload = function() {
			ReplaceSearch();
		}
	}
}





// Manage display and hide of Email to Friend app and Bookmarks app


function CloseSelf(ElementID) {
	try {
		var obj = document.getElementById(ElementID);
		obj.style.display='none';
	}
	catch(e){}
}

function DisplayElement(ElementID) {
	try {
		var obj = document.getElementById(ElementID);
		obj.style.display='inline';
	}
	catch(e){}
}

function AttachHelpClass(ElementID) {
	try {
		var obj = document.getElementById(ElementID);
		obj.className='HelpBox';
	}
	catch(e){}
}

function AttachHelpClose(ElementID,CloserElementID) {
	try {
		// Attach to the element
		var obj = document.getElementById(CloserElementID);
		obj.onclick=function(){CloseSelf(ElementID);}
	}
	catch(e){}
}

function getElementPosition(elemID) {
   	try {
		var offsetTrail = document.getElementById(elemID);
		var offsetLeft = 0;
		var offsetTop = 0;
		while (offsetTrail) {
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if (navigator.userAgent.indexOf("Mac") != -1 && 
			typeof document.body.leftMargin != "undefined") {
			offsetLeft += document.body.leftMargin;
			offsetTop += document.body.topMargin;
		}
		//alert("left:" + offsetLeft + ", " + "top:" + offsetTop);
		return {left:offsetLeft, top:offsetTop};
	}catch(e){
		return {left:100, top:100};
	}

	//return "left:" + offsetLeft + ", " + "top:" + offsetTop;
}

function ShowHelp(ElementID,CallerID,strWidth,strOffsetTop,strOffsetLeft) {
	try {
		var CallerTop = getElementPosition(CallerID).top;
		var CallerLeft = getElementPosition(CallerID).left;
		//alert('CallerTop: '+CallerTop+'; CallerLeft: '+CallerLeft);
		var obj = document.getElementById(ElementID);
		var ParentTop = CallerTop + strOffsetTop;
		var ParentLeft = CallerLeft - strOffsetLeft;
		//alert('ParentTop: '+ParentTop+'; ParentLeft: '+ParentLeft);
		obj.style.display = 'block';
		obj.style.width = strWidth + 'px';
		if(CallerLeft == 0||CallerLeft == -1){
			obj.style.left = '45%';
		}else{
			obj.style.left = ParentLeft + 'px';
		}
		if(CallerTop == 0||CallerTop == -1){
			obj.style.top = '35%';
		}else{
			obj.style.top = ParentTop + 'px';
		}
	}
	catch(e){}
}

function InitializeHelpBox(strBoxId,strLinkID,strCloser) {
	try {
		AttachHelpClose(strBoxId,strCloser);
		AttachHelpClass(strBoxId);
		DisplayElement(strLinkID);
	}
	catch(e){}
}






// Basic validation for Email to Friend Application, can be used on other forms



function generateError(elemID,errMsg) {
	try {
		var obj = document.getElementById(elemID);
		obj.className="error";
		try{
			var obj2 = document.getElementById('lbl'+elemID);
			obj2.className="error";
		}catch(e){}
		putFocusOnElement(elemID);
		//alert(errMsg);
	}catch(e){}
}


function putFocusOnElement(elemID){
	// Put focus on an element by passing its ident
	try{
		document.getElementById(elemID).focus();
	}
	catch(e){}
}

function ValidateBasic(elemID,errMsg,bolRequired){
	var strError = false;
	// Initiate basic validation, at least for required fields
	try{
		if(document.getElementById(elemID).value == '' && bolRequired == 1){
			strError = 1;
			generateError(elemID,errMsg);
			CollectErrors(errMsg);
			return strError;
		}else{
			strError = false;
			var obj = document.getElementById(elemID);
			obj.className="";
			try{
				var obj2 = document.getElementById('lbl'+elemID);
				obj2.className="";
			}catch(e){}
			return 0;
		}
	}
	catch(e){}
}

function ValidateEmail(elemID,errMsg,bolRequired){
	var strError = false;
	var obj = document.getElementById(elemID);
	// Initiate basic validation, at least for required fields
	try{
		// is it blank and required?
		
		if(obj.value == '' && bolRequired == 1){
			strError = 1;
			generateError(elemID,errMsg);
			CollectErrors(errMsg);
			return strError;
		// is it formatted as an email address?
		} else if(((obj.value.indexOf(".") < 3) || (obj.value.indexOf("@") < 2)) && obj.value != ''){
			strError = 1;
			generateError(elemID,errMsg);
			CollectErrors(errMsg);
			return strError;
		}else{
			strError = false;
			obj.className="";
			try{
				var obj2 = document.getElementById('lbl'+elemID);
				obj2.className="";
			}catch(e){}
			return 0;
		}
	}
	catch(e){}
}



function CollectErrors(errMsg){
	var errorText;
	var ErrBox = document.getElementById('ErrMsg');
	ErrBox.innerText = errMsg + '\n' + ErrBox.innerText;
	//alert(ErrBox.innerText);
}


function CheckFields() {
	try {
		var ErrBox = document.getElementById('ErrMsg');
		var strError = 0
		strError = strError + Validations();

		if((ErrBox.innerText != '') && (strError > 0)){
			alert(ErrBox.innerText);
			ErrBox.innerText = '';
		}

		//alert(strError);
		if(strError < 1){
		}else{
			//if(IsSubmit == "true"){
				//stop the submit
				return false;
			//}
		}

	}catch(e){}
}


// Move email link to another node

function MoveNode(SourceNode,TargetNode) {
	try {
		var MovingNode =  document.getElementById(SourceNode);
		var NewNode =  document.getElementById(TargetNode);

		NewNode.appendChild(MovingNode);
	} catch(e) {}
}


// Hide PDF link, or show it and append file link



function ShowPDFLink(PDFPath) {
	try {
		var LinkNode =  document.getElementById('PracticeGroupPDFLink');
		var Path =  PDFPath;

		if(Path == ''){
			LinkNode.style.display = 'none';
		}else{
			LinkNode.href = Path;
		}
	} catch(e) {}
}



function WriteFlash(SWFPath,SWFWidth,SWFHeight,SWFParams,objID,elmID,imgPath,altText) {
	try {
		var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
		if (swfobject.hasFlashPlayerVersion("9.0.0")) {
			var str = '\n';
			var NewParams = '';
			str+='	<object type="application\/x-shockwave-flash" id="' + objID + '" data="' + SWFPath + '" width="' + SWFWidth + '" height="' + SWFHeight + '">\n';
			str+='		<param name="wmode" value="transparent">\n';
			str+='		<param name="movie" value="' + SWFPath + '">\n';
			if(typeof(SWFParams) != "undefined"){
				var SWFParamsArray = SWFParams.split(';');
				for (var i=0; i<SWFParamsArray.length; i++) {
					var EachParam = SWFParamsArray[i].split(',');
					NewParams+='		<param name="' + EachParam[0] + '" VALUE="' + EachParam[1] + '">\n';
				}
				str+=NewParams;
			}
			str+='	<\/object>\n';
			document.getElementById(elmID).innerHTML = str;
		} else {
			document.getElementById(elmID).innerHTML = '<a href="\/"><img id="'+elmID+'" src="'+imgPath+'" width="'+SWFWidth+'" height="'+SWFHeight+'" alt="'+altText+'" \/><\/a>';
		}
	} catch(e) {}
}



//function to replace cells holding checkboxen/radio buttons with paragraphs
ReplaceCustomFormCells = function() {
	try {
		//find all the tables
		var elements = document.getElementsByTagName('table');

		for (i=0;i<elements.length;i++){

			//find a table with class name starting with "o" while allowing for other classes
			if(elements[i].className.substring(0,2) == 'o ' && elements[i].className.substr(2,29) != 'CustomFormCheckListHorizontal' && elements[i].className.substr(2,29) != 'CustomFormRadioListHorizontal') {

				// get the parent element of the found table
				var ParentElem = elements[i].parentNode;

				// get the cells from that table
				var ChkCells = elements[i].getElementsByTagName('td');

				// loop through each cell
				for (j=0;j<ChkCells.length;j++){

					// get only the items from the first column
					if(j%2==0){

						// ignore empty cells
						if(ChkCells[j].childNodes.length > 0){

							// create an element, give it a class (not supported by IE7)
							var block = document.createElement('p');
							block.setAttribute('class','ins');

							// add the first node (checkbox) and then the new first node (label) to each new element
							block.appendChild(ChkCells[j].childNodes[0]);
							block.appendChild(ChkCells[j].childNodes[0]);

							// add this new element to the container
							ParentElem.appendChild(block);
						} else {}
					} else {}
				}

				// loop through each cell, again
				for (j=0;j<ChkCells.length;j++){

					// get only the items from the second column, duplicating code from above
					if(j%2==1){

						// ignore empty cells
						if(ChkCells[j].childNodes.length > 0){

							// create an element, give it a class (not supported by IE7)
							var block = document.createElement('p');
							block.setAttribute('class','ins');

							// add the first node (checkbox) and then the new first node (label) to each new element
							block.appendChild(ChkCells[j].childNodes[0]);
							block.appendChild(ChkCells[j].childNodes[0]);

							// add this new element to the container
							ParentElem.appendChild(block);
						} else {}
					} else {}
				}

				// hide the table that started all this
				elements[i].style.display = 'none';
			} else {}
		}
	} catch(e) {}
}
