window.onload = todoOnLoad;
window.onbeforeunload = askConfirm;

var confirmExit = true;

var ReferringUrl = "";

if (document.referrer)
  ReferringUrl = document.referrer.toString();
if (window.opener && window.opener.location)
  ReferringUrl = window.opener.location.toString();


function todoOnLoad() {
	if (window.location.href.indexOf('disclaimer.html') > 0) {
		confirmExit = false;
		changeDisclaimerForm();
	} else {
		disclaimerRedirect();
		exitLinks();
	}
}

function changeDisclaimerForm() {
	/**
	  * If the referrer is from an investor page then change the form action to direct the user to the page they had wanted.
	  * If the referrer is not an investor page then leave the action of the form as the default, (i.e. do nothing).
	  **/
	if (ReferringUrl.indexOf('/investor_relations/') > 0) {
		document.getElementById('disclaimerForm').action = ReferringUrl;
	}
}

function disclaimerRedirect() {
	if (ReferringUrl.indexOf('/investor_relations/') > 0  && ReferringUrl.indexOf('disclaimer.html') < 0) {
		//change class to show
		document.getElementById('fin_info').className = 'show';
	} else if (ReferringUrl.indexOf('disclaimer.html') > 0 && window.location.href.indexOf('acceptTerms=true') > 0) {
		//change class to show
		document.getElementById('fin_info').className = 'show';
	} else {
		/**
		  * The user has not seen and accepted the disclaimer so they need to be redirected there.
		  * After the user accepts the disclaimer we want to send them back to where they where trying to get to.
		  * Not just to the main investor relations page. Hence disclaimer.html will need to change the forms action.
		  **/
		confirmExit = false;
		window.location = "/investor_relations/disclaimer.html"
	}
}

function askConfirm(){
	if (confirmExit && window.location.href.indexOf('/investor_relations/pdfs/') < 0) {
		return "You are now leaving the KHESLC Investor Relations Page.  By accessing any page on KHESLC's Web site that is not accessible without leaving the KHESLC Investor Relations Page, or accessing any page on any other Web site other than KHESLC's Web site, you acknowledge that you have left the KHESLC Investor Relations Page and that the information that may be viewed on such page, but may not be viewed on the KHESLC Investor Relations Page, is not a part of or incorporated within the KHESLC Investor Relations Page.  KHESLC has no responsibility for and makes no representation as to any such information that appears on any Web site other than KHESLC's Web site.";
	}
}

function exitLinks(){
    var myLinks = document.links;
    for(var i=0;i < myLinks.length;i++) {
        if(myLinks[i].target) {
        	myLinks[i].removeAttribute("target");
        }
        var thehref = myLinks[i].href.toString();
        if(thehref.indexOf("/investor_relations/") == -1){
            if (window.attachEvent) {
                    myLinks[i].attachEvent("onclick",function() {confirmExit = true;});
	    } else if (window.addEventListener) {
                    myLinks[i].addEventListener('click',function() {confirmExit = true;},false);
            }
	} else if(myLinks[i].className != 'pdf') {
            if (window.attachEvent) {
                    myLinks[i].attachEvent("onclick",function() {confirmExit = false;});
	    } else if (window.addEventListener) {
                    myLinks[i].addEventListener('click',function() {confirmExit = false;},false);
            }
        }
    }
}

function checkScrollPos(oNode) {
	var iHeight = oNode.scrollHeight;

	if(oNode.scrollTop + oNode.offsetHeight >= iHeight)
	{
		document.getElementById('fsetacceptTerms').className = 'active'; 
		document.getElementById('acceptTerms').disabled=false;

	}
}

function showPDF(url)  {
	var width  = 750;
	var height = 550;
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=yes';
	params += ', scrollbars=no';
	params += ', status=no';
	params += ', toolbar=no';

	newwin=window.open(url,'windowname5', params);
	if (window.focus) {newwin.focus()}
	return false;
}

function msgBuilder(msgArray, blnShow, msgDivName){
	var objDiv = document.getElementById(msgDivName);

	if(blnShow){
		strInnerHTML = "<div id='msg'><h5>Validation Errors:</h5><ol>";

		//place each message inside an html li tag
		for(i=0; i<msgArray.length; i++){
			strInnerHTML = strInnerHTML + "<li class='msg'>" + msgArray[i] + "</li>";
		}
		
		strInnerHTML = strInnerHTML + "</ol></div>";
		
		objDiv.innerHTML = strInnerHTML;
		objDiv.style.visibility = 'visible';
	}
	else{
		objDiv.style.visibility = 'hidden';
	}
}


/*************************************************
 * Author: jproctor
 * 
 * Notes: Validates disclosure page.
         
 ***************************************************/

function validateCkBox(theForm){
	var objChecked = false;
	var msgArray = new Array();
	var showMsg = false;
	var result = true;
	var errColor = "#FFE18F";
	var i = 0;

	if (theForm.acceptTerms.checked) {
		document.getElementById('lblacceptTerms').style.backgroundColor = ''; 
	} else {
		document.getElementById('lblacceptTerms').style.backgroundColor = errColor;
		msgArray[i++] = "To continue you have to accpet the Terms of Use."; 
		msgArray[i++] = "To be able to accept the Terms of Use you need to read all of the Terms of Use."; 
	}

	// Show messages
	if (msgArray.length > 0) {
		result = false;
		showMsg = true;
		msgBuilder(msgArray, showMsg, "msg");
	}
   
     return result;	
}
