// Header support file
var ie = (document.all) ? true : false;
var dom = ((document.getElementById) && (!ie)) ? true : false;

//function handleError() { return true; }
//window.onerror = handleError;

var eventLive = false;
var	browser = (document.layers) ? "nn4" : "unknown" ;
	browser = (document.all && !document.getElementById) ? 'ie4' : browser;
	browser = (document.all && document.getElementById) ? 'ie5' : browser;
	browser = (document.all && (navigator.appVersion.indexOf('MSIE 6.') > -1) ) ? 'ie6' : browser;
	browser = (document.all && (navigator.appVersion.indexOf('MSIE 7.') > -1) ) ? 'ie6' : browser;
	browser = (!document.all && document.getElementById) ? 'dom' : browser;
	browser = (navigator.appVersion.indexOf("Mac") != -1) ? 'mac' : browser;
	browser = (navigator.userAgent.indexOf('Opera') > -1) ? 'opera' : browser;

function hl(tdObj, state) {
	var color = (state) ? "#F3DB7A" : "";
	var image = (state) ? "url(img/savii_desk_timeline_bg.gif)" : "";
	if(ie) { 
		tdObj.style.backgroundImage=image;
	}
}

function hl2(tdObj, state) {
	var color = (state) ? "#F3DB7A" : "";
	var image = (state) ? "url(img/savii_desk_timeline_bg2.gif)" : "";
	if(ie) { 
		tdObj.style.backgroundImage=image;
	}
}

function shortLogin(uname, upass) {
	d('mainLoginForm').login_uname.value = uname;
	d('mainLoginForm').login_pass.value = upass;
	d('mainLoginForm').submit();
}

function getAction(action,action2,action3) {
	var doc_ = d("pageNavigator");
	if (doc_) {
		doc_.action_01.value=action;
		if (typeof(action2) != "undefined") {
			doc_.action_02.value=action2;
		}
		if (typeof(action3) != "undefined") {
			doc_.action_03.value=action3;
		}		
		doc_.submit();
	}
}


function changeAction(formname, action) {
	var doc_ = d(formname);
	if (doc_) {
		doc_.action_01.value=action;
		return true;
	} else {
		return false;
	}
}

function submitAction(formname) {
	var doc_ = d(formname);
	if (doc_) {
		doc_.submit();
	}
}


// returns object using shorter form
function d(a) {
	var dd;
	dd = (document.getElementById) ? document.getElementById(a) : (document.all) ? document.all[a] : eval('document.'+a);
	return dd;
}

function opener_d(a) {
	var dd;
	
	if (opener) {
		dd = (document.getElementById) ? window.opener.document.getElementById(a) : (document.all) ? window.opener.document.all[a] : eval('window.opener.document.'+a);
	}

	return dd;
}

function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isBoolean(a) {
    return typeof a == 'boolean';
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
} 

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true; 
	} else {
		return false;
	}
}

function isPhone(str) {
	var phone2 = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; 
	if (str.match(phone2)) {
   		return true;
 	} else {
 		return false;
 	}
}


function isZip(entry) {
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
      if (!reZip.test(entry)) {
          return false;
     }
	return true;
}

function isPostCode(entry){ // CANADIAN CODES ONLY
	entry=entry.replace(' ','');
	strlen=entry.length; 
	if (strlen!=6) {return false;}
	entry=entry.toUpperCase();    // in case of lowercase characters
	// Check for legal characters in string - note index starts at zero
	if ('ABCEGHJKLMNPRSTVXY'.indexOf(entry.charAt(0))<0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(1))<0) {return false;}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2))<0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(3))<0) {return false;}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4))<0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(5))<0) {return false;}
	return true; 
}

function isCorrectCCType(CCN, CCT) {
	var cardType = new Array();
	var cType = -1;
	cardType [0] = {name: "VISA", 
				   length: "13,16", 
				   prefixes: "4"};
	cardType [1] = {name: "MASTERCARD", 
				   length: "16", 
				   prefixes: "51,52,53,54,55"};
	cardType [2] = {name: "AMEX", 
				   length: "15", 
				   prefixes: "34,37"};

	CCN = replaceSymbol(CCN,' ','');
	// check if the card number matches the card type
	for (var i=0; i<cardType.length; i++) {
    	if (CCT.toLowerCase () == cardType[i].name.toLowerCase()) {
      		cType = i;
      		break;
    	}
  	}

	if (cType == -1) {
		// Unsupported Card type
		return false; 
	} else {
		var LengthValid = false;
		var PrefixValid = false; 
		var prefix = new Array ();
		var lengths = new Array ();
		
		prefix = cardType[cType].prefixes.split(",");
		for (i=0; i<prefix.length; i++) {
			var exp = new RegExp ("^" + prefix[i]);
			if (exp.test (CCN)) {
				PrefixValid = true;
				break;
			}
		}
		lengths = cardType[cType].length.split(",");
		for (j=0; j<lengths.length; j++) {
			if (CCN.length == lengths[j]) {
				LengthValid = true;
				break;
			}
		}
		return (PrefixValid && LengthValid) ? true : false;
	}
}

function isCreditCard(CC)  {                         
	var cardType = -1;
	var sum = 0; 
	var multi = 1;
	var cclen=0; 
	var mNumber = "";
    CC = replaceSymbol(CC,' ','');
	cclen = CC.length;
	if (cclen == 0) {  return false; }
	
	for (i = 0; i < cclen; i++)  {
	   mNumber = CC.substring(cclen-i-1,cclen-i);
	   if (isNaN(mNumber)) { 
	   		return false; 
	   }
	   res = parseInt(mNumber ,10) * multi;
	   if (res > 9) {
			sum += (res % 10) + 1;
	   } else {
			sum += res;
	   }
	   multi = (multi == 1) ? 2 : 1;
	}
	return ((sum % 10) == 0) ? true : false;
}


// ----- time clock -----
var initdate = new Date();
var InitTime = initdate.getTime();
var clockTimer__;

function tzChange(TZoneOffset) {
	EventTimeZoneOffset = TZoneOffset;
	var tmpDate = new Date();
	var timeCorrection = getOffsetDifferenceMSeconds(ServerTimeZoneOffset, TZoneOffset);
	var mArray = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	tmpDate.setTime(ServerTime.getTime() + timeCorrection);
	selDate = GetTime(tmpDate, TZoneOffset);
	// correct scheduled time on the form
	d('start[H]').value = selDate.hours;
	d('start[d]').value = tmpDate.getDate();
	d('start[M]').value = tmpDate.getMonth()+1;
	d('start[Y]').value = tmpDate.getFullYear();
	if (selDate.minutes <= 15) d('start[i]').value = 15;
	if (selDate.minutes > 15 && selDate.minutes <= 30) d('start[i]').value = 30;
	if (selDate.minutes > 30 && selDate.minutes <= 45) d('start[i]').value = 45;
	if (selDate.minutes > 45) { d('start[i]').value = 0; d('start[H]').value = parseInt(d('start[H]').value)+1;}
	var tDay = (tmpDate.getDate()<=9) ? "0"+tmpDate.getDate() : tmpDate.getDate();
	d('date_event_start').innerHTML = mArray[tmpDate.getMonth()]+" "+tDay+", "+tmpDate.getFullYear();
	tmpDate.setTime(ServerTime.getTime() + timeCorrection + 45*60*1000);
	selDate = GetTime(tmpDate, TZoneOffset);
	d('end[H]').value = selDate.hours;
	d('end[d]').value = tmpDate.getDate();
	d('end[M]').value = tmpDate.getMonth()+1;
	d('end[Y]').value = tmpDate.getFullYear();
	if (selDate.minutes <= 15) d('end[i]').value = 15;
	if (selDate.minutes > 15 && selDate.minutes <= 30) d('end[i]').value = 30;
	if (selDate.minutes > 30 && selDate.minutes <= 45) d('end[i]').value = 45;
	if (selDate.minutes > 45) { d('end[i]').value = 0; d('end[H]').value = parseInt(d('end[H]').value)+1;}
	var tDay = (tmpDate.getDate()<=9) ? "0"+tmpDate.getDate() : tmpDate.getDate();
	var tMon = (tmpDate.getMonth()<=8) ? "0"+(tmpDate.getMonth()+1) : (tmpDate.getMonth()+1);
	d('date_event_end').value = tmpDate.getFullYear()+"|"+tMon+"|"+tDay;
}

function  printProps(obj, objName) {
  var output = "" ;
  for (var prop in obj) {
    output += objName + "." + prop + " = " + obj[prop] + "\n" ;
  }
  return output ;
}

function getOffsetDifferenceMSeconds(tZone1, tZone2) {
	stime_sec = getOffsetSeconds(tZone1);
	stime_nzone = getOffsetSeconds(tZone2);
	return (stime_sec-stime_nzone)*-1000;
}

function GetTime(tObject, TZoneOffset ) {
	var hours=tObject.getHours();
    var hours2=tObject.getHours();
	var ampm = (hours>12) ? " PM" : " AM";
    var minutes=tObject.getMinutes();
    var seconds=tObject.getSeconds();
	this.hours = hours;
	this.minutes = minutes;
	this.year = tObject.getFullYear();
	var actualZoneOffset = timeZoneOffsetActual(tObject);
	if (minutes<=9) minutes="0"+minutes;
    if (seconds<=9) seconds="0"+seconds;
	if (hours > 12) hours = hours - 12;
    if (hours<=9) hours="0"+hours;
    if (hours2<=9) hours2="0"+hours2;
	this.ctime = hours  + ":" + minutes + ":" + seconds + ampm;
	this.etime = hours2 + ":" + minutes + ":" + seconds;
//this.timeZoneOffset = (typeof(TZoneOffset) != 'undefined') ? (TZoneOffset/100) : actualZoneOffset;
	this.timeZoneMask = (typeof(TZoneOffset) != 'undefined') ? TZoneOffset : getTimeZoneMask(actualZoneOffset);
	this.timeZoneOffsetSeconds = getOffsetSeconds(this.timeZoneMask);
	this.daylightSaving = isDaylightSaving(tObject);
	return this;
}

function isDaylightSaving(tObject) {
	return (((tObject.getTimezoneOffset()/60)*(-1)) != timeZoneOffsetStandard(tObject)) ? true : false;
}

function timeZoneOffsetActual(tObject) {
	return timeZoneOffsetStandard(tObject);
//	return (tObject.getTimezoneOffset()/60)*(-1);
}

function timeZoneOffsetStandard(tObject) {
   var date1 = new Date(tObject.getFullYear(), 0, 1, 0, 0, 0, 0);
   var temp = date1.toGMTString();
   var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
   var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
   return parseInt(hoursDiffStdTime);
}

function getOffsetSeconds(timeZoneOffset) {
   if(timeZoneOffset == "") return 0;	
	var seconds = 0;
	var rExp = /^[0]/;
	if (typeof(timeZoneOffset) == "string") {
		if (timeZoneOffset != '0000') {
			var hours = timeZoneOffset.substring(1,3);
			var minutes = timeZoneOffset.substring(3);
			var aps = parseInt(timeZoneOffset.substring(0,1)+"1");
			hours = parseInt(hours.replace(rExp,""));
			minutes = parseInt(minutes.replace(rExp,""));
			seconds = aps*(hours*3600 + minutes*60);
		}
	}
	return seconds;
}

function getTimeZoneMask(timeDiff) {
	if (isNaN(timeDiff)) return timeDiff;
	var tMask = (timeDiff < 0) ? "-" : (timeDiff > 0) ? "+" : "";
	var tmpLen = Math.abs(timeDiff).toString().length;
	if (Math.abs(timeDiff) > 0 && Math.abs(timeDiff) < 1) {
		tMask += "00" + (Math.abs(timeDiff) * 100);
	} else if (tmpLen > 0 && tmpLen < 4) {
		tMask += "0" + (Math.abs(timeDiff) * 100);
	} else if (tmpLen == 4) {
		tMask += (Math.abs(timeDiff) * 100);
	}
	return tMask;
}

function TimeTick(){
    var tmpTime = new Date();
	var LocalTime = new Date();

	if (typeof(ServerTime) == 'undefined') {
		return false;
	}
	if (typeof(clockTimer) != 'undefined') {
		window.clearTimeout(clockTimer__);
	}

	ServerTime.setTime(ServerTime.getTime() + (tmpTime.getTime() - InitTime) );
	var srvTime = new GetTime(ServerTime, ServerTimeZoneOffset);
	var locTime = new GetTime(LocalTime);
	var timeCorrection = getOffsetDifferenceMSeconds(ServerTimeZoneOffset, locTime.timeZoneMask);
	LocalTime.setTime(ServerTime.getTime() + timeCorrection );


	if (document.getElementById) {
		var xelement  = document.getElementById("timeObj");

/*		if (isDaylightSaving(LocalTime)) {
			LocalTime.setTime(LocalTime.getTime() + 1000*60*60);
		} else if ( ServerDaylightTime) {
			LocalTime.setTime(LocalTime.getTime() - 1000*60*60);
		}
*/
		if (!isDaylightSaving(LocalTime) && ServerDaylightTime) {
			LocalTime.setTime(LocalTime.getTime() - 1000*60*60);
		}

		locTime = new GetTime(LocalTime);
//alert("LocalTime="+LocalTime+"\nlocTime="+locTime);

		if (xelement) {
			xelement.innerHTML = "<div class='clock2'>"+locTime.etime+" (GMT"+locTime.timeZoneMask+")</div>";
		}
		InitTime = tmpTime.getTime();
		clockTimer__ = window.setTimeout("TimeTick()",1000);
	}
}
// -- end time clock ----



function infoWin(wsrc, w, h) {
	var popup = window.open(wsrc,'Information','width='+w+',height='+h+',top=20,left=20,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no');
	popup.focus();
	return true;
}

function emptyWindow(content,w,h) {
	var cW = window.open('empty.htm','Information','width='+w+',height='+h+',menubar=no,location=no,resizable=no,scrollbars=yes,status=no');
	cW.document.open();
	cW.document.write('<html><head><title>Indormation</title><style>body {font-size:12px;font-family:Arial, Helvetica, sans-serif;}</style></head><body>');
	cW.document.write(content);
	cW.document.write('</body></html>');
	cW.document.close();
	cW.focus();
}

function replaceSymbol(str, original, replacement)  {
	var result = "";
	while(str.indexOf(original) != -1) {
	    if (str.indexOf(original) > 0) result = result + str.substring(0, str.indexOf(original)) + replacement;
	    else result = result + replacement;
	    str = str.substring(str.indexOf(original) + original.length, str.length);
	}
	return result + str;
}

function grep(str) {
  var ar = new Array()
  var arSub = 0
  for (var i in this) {
	 if (typeof(this[i]) != "function" && parseInt(this[i].indexOf(str)) != -1) {
		ar[arSub] = this[i];
		arSub++
	 }
  }
  return ar;
}

function key(xVar) {
	var arSub = 0
	for (var i in this) {
		if (typeof(this[i]) != "function" && this[i] == xVar) {
			return true;
	 	}
  	}
  	return false;
}

function Descriptor() {
	this.name = "none";
	this.browser = "unsupported";
	this.detected = false;
	this.supported = false;
	this.version = "0,0,0,0";
}


function showRnet() {
	var dd = d('divINFO');
	var dd_msg = d('divINFO_msg');
	if (dd) {
		dd.style.width=(window.innerWidth) ? window.innerWidth+'px' : document.body.clientWidth+'px';
		dd.style.height=(window.innerHeight) ? (window.innerHeight+30)+'px' : (document.body.clientHeight+30)+'px';
		dd.style.visibility='visible';
	}
	if (dd_msg) {
		dd_msg.style.visibility='visible';
	}
}

function show_countdown(status) {
	if (status) {
		d("launch_stage").style.visibility = "hidden";
		d("progress_bar").style.visibility = "visible";
		d("progress_bar").innerHTML = "<img src='http://"+document.location.host+"/img/progress_bar.gif'>";
		setTimeout("show_countdown(false);", 8000);
	} else {
		d("progress_bar").style.visibility = "hidden";
		d("progress_bar").innerHTML = "&nbsp;";
		d("launch_stage").style.visibility = "visible";
	}
}

function pageChange_class(action,page,frm) {
	frm.action_01.value = action; 
	frm.page.value = page; 
	frm.submit();
	return false;
}

function onLoadWindow() {
	if (typeof(showTime) == 'function') { showTime(); }
	if (typeof(calendarInit) == 'function') { calendarInit(); }
	if (typeof(host_edit_onload) == 'function') { host_edit_onload(); }
	if (typeof(showSAViiDeskComments) == 'function') { showSAViiDeskComments('1'); }
	if (typeof(pdf_load) == 'function') { pdf_load(); }
	if (typeof(front_load) == 'function') {	front_load(); }
	if (typeof(detect_client) == 'function') { detect_client();	}
	if (typeof(detect_host) == 'function') { detect_host();	}
	if (typeof(AddressBookFilter) == 'function') { AddressBookFilter('ALL'); }
}

Array.prototype.grep = grep;
Array.prototype.keyExist = key;

window.onload = onLoadWindow;

