//
// DOCUMENT ONLOAD FUNCTIONS
//

function addOnloadEvent(fnc){  // Function to add extra onload events without breaking any already called
  if ( typeof window.addEventListener != "undefined" )
	window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
	window.attachEvent( "onload", fnc );
  }
  else {
	if ( window.onload != null ) {
	  var oldOnload = window.onload;
	  window.onload = function ( e ) {
		oldOnload( e );
		window[fnc]();
	  };
	}
	else
	  window.onload = fnc;
  }
}
// Or to pass arguments- 
//addOnloadEvent(function(){ myFunctionName('myArgument') });



//
// FORM UTILITY FUNCTIONS
//

function GetFormArrayIndex(element) {
	var poz
	for (var i=0;i<element.form.elements.length;i++) {
		if (element===element.form.elements[i]) {
			poz = i
		}
	}
	return poz
}

function setFieldValue(fieldId, value) {
    $('#' + fieldId)[0].value = value;
}

//
// GENERAL UTILITY FUNCTIONS
//

//Gets the filename from a path
function GetFileName(path) { 
	var pos =path.lastIndexOf( path.charAt( path.indexOf(":")+1) ); 
	var filename = path.substring( pos+1);
	return filename;
}

// JS object to check valid integers, decimals and currencies.
// Usage: isValid.currency('1.10');
var isValid = (function(){
    var integerTest = /^\d+$/;
    var decimalTest = /^\d+\.?\d*$/;
    var currencyTest = /^\d+\.\d\d$/;
    return {
        integer: function(n){
        return integerTest.test(n);
        },
        decimal: function(n){
        return decimalTest.test(n);
        },
        currency: function(n){
        return currencyTest.test(n);
        }
    };
})();

function ConfirmMsg(msg) {
return confirm(msg);
}

function PopupWindow(strUrl, width, height, strScroll, windowName) {
	var PopupWin = window.open(strUrl,windowName,"scrollbars=" + strScroll + ",toolbar=no,directories=no,status=no,menubar=no,resizable=yes,width=" + width + ",height=" + height)
	PopupWin.focus();
}

function fitWindowToImageSize() {
    var hasInnerCompat = (window.innerWidth)?true:false;
	iWidth = (hasInnerCompat)?window.innerWidth:document.body.clientWidth;
	iHeight = (hasInnerCompat)?window.innerHeight:document.body.clientHeight;
	iWidth = document.images[0].width - iWidth;
	iHeight = document.images[0].height - iHeight;
	window.resizeBy(iWidth, iHeight-1);			
	self.focus();
}

function clearControlText(sender) {
    if (sender.value == "search" ) {
        sender.value = '';
    }
}

function clearDummyText(sender, dummyValue) {
    if (sender.value == dummyValue) {
        sender.value = "";
    }else if (sender.value == "") {
            sender.value = dummyValue;
    }
}


function GetWindowHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
return(myHeight);
}

function selectmenu(idx){
    idx = idx || 'Home';
    document.getElementById("ctl00_ContentPlaceHolderTop_Top1_li"+idx).className = "current";
    document.getElementById("ctl00_ContentPlaceHolderTop_Top1_lk"+idx).className = "current";    
}


function changeMe(obj){
    obj.src = obj.src.replace(".gif","-ov.gif");
    return false;
}
function resetMe(obj){
    obj.src = obj.src.replace("-ov.gif",".gif");
    return false;
}

//
// COOKIE FUNCTIONS
//


function GetCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function SetCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function DeleteCookie( name, path, domain ) {
    //if (getCookie(name)) {
        SetCookie(name, "null", "Thu, 01-Jan-1970 00:00:01 GMT", path, "", "");
    //}
}

function GetCookieSubKey(cookieName,keyName){
	var cookie=unescape(GetCookie(cookieName));
	if(keyName && CookieHasSubKeys(cookieName)==true){
		var cookieData = GetCookieData(cookie);
		return cookieData[keyName];
	}else{
		return cookie;
	}
}

function GetCookieData(cookie){
	var ret=[];
	var subKeys=cookie.split("&");
	for (var i=0;i<subKeys.length;i++){
		//alert(subKeys[i]);
		var pos=subKeys[i].indexOf("=");
		if(pos!=0){
			//alert("cookieData["+subKeys[i].substring(0,pos)+']='+subKeys[i].substring(pos+1));
			ret[subKeys[i].substring(0,pos)]=subKeys[i].substring(pos+1);
		}
	}
	return ret;
}

function CookieHasSubKeys(cookieName){
    var ret=false;
    var cookie=unescape(GetCookie(cookieName));
    var subKeys=cookie.split("&");

    if(subKeys.length>=2){
    ret=true;}
    else{
    var subKeys=cookie.split("=");

    if(subKeys.length>=1){
    ret=true;}}
    return ret;
}


function SetCookieSubKey(cookieName,keyName,value,expires){
	var thisCookie=unescape(GetCookie(cookieName));
	if(thisCookie){
		if(keyName){
			//alert(keyName);
			var cookieData=GetCookieData(thisCookie);
			cookieData[keyName]=value;
			value="";
			for (var cookie in cookieData){
				value +=cookie+"="+cookieData[cookie]+"&";
			}
			if(value.charAt(value.length-1)=="&"){value=value.substring(0,value.length-1)}
		}
	}
	//alert(cookieName+"\n"+value);
	SetCookie( cookieName, value, expires, '', '', false )
}




//
// IMAGE FUNCTIONS
//

function Image_ResizeMaxHeight(image1) {
    var maxheight = 110
    var percentageDiff
    if (image1.height < maxheight) { return }
    percentageDiff = (maxheight / image1.height) * 100
    image1.height = maxheight
    image1.width = (image1.width / 100) * percentageDiff
}





//
// TIME AND DATE FUNCTIONS
//


    function ToClientLocalTime( dd, MM, yyyy, hh, mm, ss ) {
	  var currTime = new Date(yyyy,MM-1,dd,hh,mm,ss);
	  var minutes = currTime.getMinutes();
	  minutes -= currTime.getTimezoneOffset();
	  currTime.setMinutes(minutes);      
	  var theHour = currTime.getHours().toString();
	  if (theHour.length==1){theHour = "0" + theHour};

	  var theMin = currTime.getMinutes().toString();
	  if (theMin.length==1){theMin = "0" + theMin};

	  var timeDateString 
	  //timeDateString = theHour;
	  //timeDateString += ":";
	  //timeDateString += theMin;
	  //timeDateString += " - ";
	  timeDateString = currTime.getDate()
	  timeDateString += " ";
	  timeDateString += GetMonthName(currTime.getMonth());
	  timeDateString += " ";
	  timeDateString += currTime.getFullYear();            
	  return timeDateString;
    }

    function GetMonthName(monthNumber) {
	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	return month[monthNumber];
    }

    function GetDayName(weekdayNumber) {
	var weekday=new Array(7);
	weekday[0]="Sunday";
	weekday[1]="Monday";
	weekday[2]="Tuesday";
	weekday[3]="Wednesday";
	weekday[4]="Thursday";
	weekday[5]="Friday";
	weekday[6]="Saturday";
	return weekday[weekdayNumber];
    }

