var debugFlag	= false;

//constants for millisecond/time-unit conversion
var c_seconds	= 1000;
var c_minutes	= 60  * c_seconds;
var c_hours		= 60  * c_minutes;
var c_days		= 24  * c_hours;
var c_years		= 365 * c_days;

//These are the pictures and links 
params = {
	solar: "<a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prods/?cmd=login\"><img alt='SOLAR Login' title='SOLAR Login' src=\"framework/images/solar/solar_login_button.gif\"/></a>",
	solarSH: "<a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prodh/SOUTHAMPTON/HRMS/?cmd=login\"><img alt='Southampton SOLAR Login' title='Southampton SOLAR Login' src=\"framework/images/solar/sh_solar_login_button.gif\"/></a>",
	express: "<br /><a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prods/EMPLOYEE/HRMS/c/SU_SELF_SERVICE.SU_ACCR_WB_ST_EMP.GBL?PORTALPARAM_PTCNAV=SU_ACCR_WB_ST_EMP_GBL&EOPP.SCNode=HRMS&EOPP.SCPortal=EMPLOYEE&EOPP.SCName=ADMN_SOLAR_SYSTEM&EOPP.SCLabel=State%20Time%20Reporting&EOPP.SCFName=HC_TIME_REPORTING&EOPP.SCSecondary=true&EOPP.SCPTcname=PT_PTPP_SCFNAV_BASEPAGE_SCR&FolderPath=PORTAL_ROOT_OBJECT.CO_EMPLOYEE_SELF_SERVICE.SU_EMPLOYEE_FOLDER.HC_TIME_REPORTING.HC_RECORD_TIME.SU_ACCR_WB_ST_EMP_GBL&IsFolder=false\"><img alt='SOLAR Express Login to Time Reporting' title='SOLAR Express Login to Time Reporting' src=\"framework/images/solar/express_login_to_time_reporting.gif\"/></a>",
	expressSH: "<br /><a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prods/EMPLOYEE/HRMS/c/SU_SELF_SERVICE.SU_ACCR_WB_ST_EMP.GBL?PORTALPARAM_PTCNAV=SU_ACCR_WB_ST_EMP_GBL&EOPP.SCNode=HRMS&EOPP.SCPortal=EMPLOYEE&EOPP.SCName=ADMN_SOLAR_SYSTEM&EOPP.SCLabel=State%20Time%20Reporting&EOPP.SCFName=HC_TIME_REPORTING&EOPP.SCSecondary=true&EOPP.SCPTcname=PT_PTPP_SCFNAV_BASEPAGE_SCR&FolderPath=PORTAL_ROOT_OBJECT.CO_EMPLOYEE_SELF_SERVICE.SU_EMPLOYEE_FOLDER.HC_TIME_REPORTING.HC_RECORD_TIME.SU_ACCR_WB_ST_EMP_GBL&IsFolder=false\"><img alt='SOLAR Express Login to Time Reporting' title='SOLAR Express Login to Time Reporting' src=\"framework/images/solar/sh_express_login_to_time_reporting.gif\"/></a>"
};

function Test(func,params,onSuccess,onFailure) {
	this.func = func;
	this.params = params;
	this.onSuccess = onSuccess;
	this.onFailure = onFailure;
	this.validate = function() {return this.func(this.params,this.onSuccess,this.onFailure);};
}


//This will be the master checkClientDate function
function checkClientDate3(serverTime,onSuccess,onFailure) {
	var c_MaxDiff = 30 * c_minutes;
	var serverTimeInMillis = Date.parse(serverTime);
	var clientTime = new Date();
	var serverTime = new Date(serverTimeInMillis);
	
	var timeDiff = clientTime - serverTime;
	
	if(timeDiff > c_MaxDiff) {
		onFailure(timeDiff,serverTime,clientTime);
		return false;
	}
	if(onSuccess != null)onSuccess();
	return true;
}

function checkClientLanguage(HTTP_ACCEPT_LANGUAGE_STRING,onSuccess,onFailure) {
	var languagePrefix = HTTP_ACCEPT_LANGUAGE_STRING.substring(0,2);
	
	if(languagePrefix != 'en' && languagePrefix != 'EN') {
		onFailure(HTTP_ACCEPT_LANGUAGE_STRING);
		return false;
	}
	if(onSuccess != null)onSuccess();
	return true;
}

//OLD STUFF
function checkClientDate(serverTime) {
	var c_MaxDiff = 30 * c_minutes;
	var serverTimeInMillis = Date.parse(serverTime);
	var clientTime = new Date();
	var serverTime = new Date(serverTimeInMillis);

	var diff = clientTime - serverTime;
	
	if(debugFlag){
		document.write("Max allowed difference: " + c_MaxDiff + " milliseconds<br/><br/>");
	}
	
	if(debugFlag){
		document.write("The locale time on the client is: " + clientTime.toLocaleString() + "<br/>");
		document.write("The time on the server is: " + serverTimeInMillis + " milliseconds<br/>");
		document.write("The time on the client is: " + clientTime.getTime() + " milliseconds<br/>");
		document.write("The computed difference is: " + diff + " milliseconds<br/>");
	}

	if(diff > c_MaxDiff) {
	
	//compute the floor of each time unit and subtract it from the total time difference
	var years = Math.floor(diff / c_years);
		diff = diff - (years*c_years);
	var days = Math.floor(diff / c_days);
		diff = diff - (days*c_days);
	var hours = Math.floor(diff / c_hours);
		diff = diff - (hours*c_hours);
	var minutes = Math.floor(diff / c_minutes);
		diff = diff - (minutes*c_minutes);

	document.writeln("Your computer's clock is <i>not</i> correct.<br/><br/>");
	document.write("The UTC time on the server is:<br/> " + serverTime.toUTCString() + "<br/><br/>");
	document.write("The UTC time on your computer is:<br/> " + clientTime.toUTCString() + "<br/><br/>");
	document.write("Your computer's clock is approximately " + years + " years, " + days + " days, " + hours + " hours, " + minutes + " minutes ahead of the server's clock");
	} else {
		document.writeln("<a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prods/?cmd=login\"><img alt='SOLAR Login' title='SOLAR Login' src=\"framework/images/solar/solar_login_button.gif\"/></a>");	
	}
	
}

function checkClientDateExpress(serverTime) {
	var c_MaxDiff = 30 * c_minutes;
	var serverTimeInMillis = Date.parse(serverTime);
	var clientTime = new Date();
	var serverTime = new Date(serverTimeInMillis);

	var diff = clientTime - serverTime;
	
	if(debugFlag){
		document.write("Max allowed difference: " + c_MaxDiff + " milliseconds<br/><br/>");
	}
	
	if(debugFlag){
		document.write("The locale time on the client is: " + clientTime.toLocaleString() + "<br/>");
		document.write("The time on the server is: " + serverTimeInMillis + " milliseconds<br/>");
		document.write("The time on the client is: " + clientTime.getTime() + " milliseconds<br/>");
		document.write("The computed difference is: " + diff + " milliseconds<br/>");
	}

	if(diff > c_MaxDiff) {
	
	//compute the floor of each time unit and subtract it from the total time difference
	var years = Math.floor(diff / c_years);
		diff = diff - (years*c_years);
	var days = Math.floor(diff / c_days);
		diff = diff - (days*c_days);
	var hours = Math.floor(diff / c_hours);
		diff = diff - (hours*c_hours);
	var minutes = Math.floor(diff / c_minutes);
		diff = diff - (minutes*c_minutes);

	document.writeln("Your computer's clock is <i>not</i> correct.<br/><br/>");
	document.write("The UTC time on the server is:<br/> " + serverTime.toUTCString() + "<br/><br/>");
	document.write("The UTC time on your computer is:<br/> " + clientTime.toUTCString() + "<br/><br/>");
	document.write("Your computer's clock is approximately " + years + " years, " + days + " days, " + hours + " hours, " + minutes + " minutes ahead of the server's clock");
	} else {
		document.writeln("<br /><a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prods/EMPLOYEE/HRMS/c/SU_SELF_SERVICE.SU_ACCR_WB_ST_EMP.GBL?PORTALPARAM_PTCNAV=SU_ACCR_WB_ST_EMP_GBL&EOPP.SCNode=HRMS&EOPP.SCPortal=EMPLOYEE&EOPP.SCName=ADMN_SOLAR_SYSTEM&EOPP.SCLabel=State%20Time%20Reporting&EOPP.SCFName=HC_TIME_REPORTING&EOPP.SCSecondary=true&EOPP.SCPTcname=PT_PTPP_SCFNAV_BASEPAGE_SCR&FolderPath=PORTAL_ROOT_OBJECT.CO_EMPLOYEE_SELF_SERVICE.SU_EMPLOYEE_FOLDER.HC_TIME_REPORTING.HC_RECORD_TIME.SU_ACCR_WB_ST_EMP_GBL&IsFolder=false\"><img alt='SOLAR Express Login to Time Reporting' title='SOLAR Express Login to Time Reporting' src=\"framework/images/solar/express_login_to_time_reporting.gif\"/></a>");	
	}
	
}

function checkClientDateSH(serverTime) {
	var c_MaxDiff = 30 * c_minutes;
	var serverTimeInMillis = Date.parse(serverTime);
	var clientTime = new Date();
	var serverTime = new Date(serverTimeInMillis);

	var diff = clientTime - serverTime;
	
	if(debugFlag){
		document.write("Max allowed difference: " + c_MaxDiff + " milliseconds<br/><br/>");
	}
	
	if(debugFlag){
		document.write("The locale time on the client is: " + clientTime.toLocaleString() + "<br/>");
		document.write("The time on the server is: " + serverTimeInMillis + " milliseconds<br/>");
		document.write("The time on the client is: " + clientTime.getTime() + " milliseconds<br/>");
		document.write("The computed difference is: " + diff + " milliseconds<br/>");
	}

	if(diff > c_MaxDiff) {
	
	//compute the floor of each time unit and subtract it from the total time difference
	var years = Math.floor(diff / c_years);
		diff = diff - (years*c_years);
	var days = Math.floor(diff / c_days);
		diff = diff - (days*c_days);
	var hours = Math.floor(diff / c_hours);
		diff = diff - (hours*c_hours);
	var minutes = Math.floor(diff / c_minutes);
		diff = diff - (minutes*c_minutes);

	document.writeln("Your computer's clock is <i>not</i> correct.<br/><br/>");
	document.write("The UTC time on the server is:<br/> " + serverTime.toUTCString() + "<br/><br/>");
	document.write("The UTC time on your computer is:<br/> " + clientTime.toUTCString() + "<br/><br/>");
	document.write("Your computer's clock is approximately " + years + " years, " + days + " days, " + hours + " hours, " + minutes + " minutes ahead of the server's clock");
	} else {
		document.writeln("<a class=\"solar_login_button\" href=\"https://psns.cc.stonybrook.edu/psp/he90prodh/SOUTHAMPTON/HRMS/?cmd=login\"><img alt='Southampton SOLAR Login' title='Southampton SOLAR Login' src=\"framework/images/solar/sh_solar_login_button.gif\"/></a>");
	}
	
}

function checkClientDate2(serverTime,onSuccess,onFailure) {
	var c_MaxDiff = 30 * c_minutes;
	var serverTimeInMillis = Date.parse(serverTime);
	var clientTime = new Date();
	var serverTime = new Date(serverTimeInMillis);
	
	var timeDiff = clientTime - serverTime;
	
	if(timeDiff > c_MaxDiff) {
		onFailure(timeDiff,serverTime,clientTime);
	} else {
		onSuccess();
	}
}