// // NOTE: All Functions included in this file have a prefix of "ssc_"
// 
var CONST_SSC_STR = "SchedulingSmart"
var CONST_SSC_STR_ALL_LOW_CASE = "schedulingsmart"
var CONST_WIN_STR = "win"
var CONST_SSC_WIN_MAX = 16
var CONST_SSC_ESWIN_MAX=3
var CONST_SSC_EPWIN_MAX=3
var CONST_SSC_EVWIN_MAX=3

var screen_ht = screen.availHeight;
var screen_wd = screen.availWidth;
var screen_left = 0;
var screen_top = 10;

var last_mod_date_tou = ""
var last_mod_date_pss = " (last updated September 2004)."
var last_mod_date_tou_format = "19 January 2004"
var last_mod_date_pss_format = "19 January 2004"
var legal_year = "2004"

FULL= "toolbar,directories,resizable,status,scrollbars,menubar,location,"
NONE= "scrollbars,resizable,status,"
MENUONLY= "menubar,scrollbars,resizable,status,"
ALL = ""

serverurl = "http://www.schedulingsmart.com/"
appurl = "http://www.schedulingsmart.com/ssmuiprd/"
appservlet =  "http://www.schedulingsmart.com/ssmuiprd/servlet/icss"
appSSLservlet = "http://www.schedulingsmart.com/ssmuiprd/servlet/icss"

appdomain = "; domain=.schedulingsmart.com"

FPURL  = serverurl+"about/forgot_password.htm";
TOUURL  = serverurl+"about/terms_of_use.htm";
COPYURL = serverurl+"about/terms_of_use.htm#Section5";
PSSURL  = serverurl+"about/privacy_policy.htm"; 
EXITURL = serverurl+"exit.htm";
CONTACTUS = serverurl+"about/contact_us.htm";
PUBLIC_CONTACTUS = serverurl+"about/public_contact_us.htm";
FAQ = serverurl+"about/faq.htm";

var_submit_win = "navigation.submit_win"
var_work_id = "navigation.workId"
var_action = "navigation.action"
var_task_id = "navigation.taskId"   
var_cache_id = "navigation.cacheId"
var_new_task = "navigation.newTask"
var_ssl = "navigation.ssl"
var_get_page = "getPage"
var_flow    ="navigation.flow"
var_proceed = true

function ssc_w(astring) {
  document.writeln(astring);
}
function ssc_turnOn(imageName) {
	if (document.images) {
		document[imageName].src = eval(imageName + "on.src");
	}
}

function ssc_turnOff(imageName) {
	if (document.images) {
		document[imageName].src = eval(imageName + "off.src");
	}
}
/*
function ssc_spacer(w,imagepath) {
	document.writeln("<img src='" + imagepath + "spacer.gif' width='" + w + "' HEIGHT='1'>")
}
*/

function ssc_popup(url) {
	popupWin = window.open(url + '?getPage=popup.jsp' + '&' + var_ssl + '=' + ssc_isSSL(url), 'popup', 'width=400,height=240,left=300,top=300') 
}

function ssc_popupPage(url,winsize,winname,winparms) {
	if (winsize == "S") scalar = 0.33;
	else if (winsize == "M") scalar = 0.50;
	else if (winsize == "L") scalar = 0.75;
	else scalar = 1.0;
	w_ht = Math.round(scalar * screen_ht);
	w_wd = Math.round(scalar * screen_wd);
	w_left = (screen_wd - w_wd) - 20;
	winparms += "left=" + w_left + ",top=" + screen_top + ",screenX=" + w_left + ",screenY=" + screen_top + ",";
	winparms += "height=" + w_ht + ",width=" + w_wd;
	if (window.nonsscWin) {
		if (!nonsscWin.closed) nonsscWin.close();
	}
	nonsscWin = window.open(url,winname,winparms);
	return;
}

function ssc_popupPageFull(url,winsize,winname,winparms) {
	var scalar = 1.0;
	w_ht = Math.round(scalar * screen_ht);
	w_wd = Math.round(scalar * screen_wd);
	winparms += "left=" + 0 + ",top=" + screen_top + ",screenX=" + 0 + ",screenY=" + screen_top + ",";
	winparms += "height=" + w_ht + ",width=" + w_wd;
	if (window.nonsscWin) {
		if (!nonsscWin.closed) nonsscWin.close();
	}
	nonsscWin = window.open(url,winname,winparms);
	return;
}
function ssc_popupForgotPassword() {
	ssc_popupPageFull(FPURL,"","fp",NONE);
}
function ssc_popupTOU() {
	ssc_popupPage(TOUURL,"L","ssclegal",NONE);
}
function ssc_popupCopyRight() {
	ssc_popupPage(COPYURL,"L","ssclegal",NONE);
}
function ssc_popupPSS() {
	ssc_popupPage(PSSURL,"L","ssclegal",NONE);
}
function ssc_popupContactUs() {
	ssc_popupPageFull(CONTACTUS,"","contact_us",NONE);
}
function ssc_popupPublicContactUs() {
	ssc_popupPageFull(PUBLIC_CONTACTUS,"","contact_us",NONE);
}
function ssc_popupFaq() {
	ssc_popupPageFull(FAQ,"","faq",NONE);
}
function ssc_popupExit(url) {
	w_top = Math.round((screen_ht-300)*0.5);
	w_left= Math.round((screen_wd-460)*0.5);
	winparms = "width=460,height=200,";
	winparms += "left=" + w_left + ",top=" + w_top;
	exitWin=window.open(serverurl+'exit.htm', 'popup', winparms)		
	ssc_popupPage(url,"X","nonssc",FULL);
	exitWin.focus();
}


function ssc_getSignOffPage() {
        document.frm_sign_off.elements[var_work_id].value = "GetSignOffWork";
        document.frm_sign_off.elements[var_action].value = "forward";
        document.frm_sign_off.elements[var_task_id].value = "GetSignOffTask";
        document.frm_sign_off.elements[var_cache_id].value = "sys_sign_off";
        document.frm_sign_off.elements[var_new_task].value = true;
        document.frm_sign_off.elements[var_get_page].value = "sys_sign_off.jsp";
        document.frm_sign_off.target = CONST_SSC_STR_ALL_LOW_CASE;
        document.frm_sign_off.method = "post";
        document.frm_sign_off.submit();
      
}

function ssc_getScheduleCenterPage() {
        document.frm_header_footer.elements[var_new_task].value = true;
        document.frm_header_footer.elements[var_task_id].value = "ScheduleCenterTask";
        document.frm_header_footer.elements[var_action].value = "forward";
        document.frm_header_footer.elements[var_work_id].value = "ScheduleCenterWork";
	document.frm_header_footer.elements[var_flow].value = "btn_refresh";
        return true;
}
function ssc_isSSL(url) {
	if (url.indexOf("http") == -1 )
	{ return ssc_isSSL(location.protocol) }
	if (url.indexOf("https") == -1 ) 
	{ return false }
	else 
	{ return true }
}

function submitClick(newWin, windowName, windowType, url, form, method, para) {
	//if (var_proceed) {
		var_proceed = false;
		windownames = ssc_getWindowName(windowName);
		var w_ht = screen_ht - 60;
		var w_wd = screen_wd - 20;
		windowType += "left=10,top=10,screenX=10,screenY=10,";
		windowType += "height=" + w_ht + ",width=" + w_wd;
		if (method == "get") {
			para = var_submit_win + '=' + escape(windownames) + '&' + para + '&' + var_ssl + '=' + ssc_isSSL(url)
			if (newWin == true) {
				var_proceed = true;
				
				if (ssc_checkWindowNum()) {
					
					switch (windowName.substr(0, 2))
					{
						case "es" :
							if (ssc_checkESWindowNum(windowName))
							{
								document.cookie= windownames + "=" + escape(windownames)
								newWindow = window.open(url + '?' + para, windowName, windowType)
								newWindow.window.focus()
							} else {
								alert("Maximum allowable opened windows for editing schedule has been reached. Please close unused editing schedule windows before proceeding further.");
								return;		
							}
							break;
						case "ep":					
							if (ssc_checkEPWindowNum(windowName))
							{
								document.cookie= windownames + "=" + escape(windownames)
								newWindow = window.open(url + '?' + para, windowName, windowType)
								newWindow.window.focus()
							} else {
								alert("Maximum allowable opened windows for proforma has been reached. Please close unused proforma windows before proceeding further.");
								return;		
							}
							break;
						case "ev":
							if (ssc_checkEVWindowNum(windowName))
							{
								document.cookie= windownames + "=" + escape(windownames)
								newWindow = window.open(url + '?' + para, windowName, windowType)
								newWindow.window.focus()
							} else {
								alert("Maximum allowable opened windows for vessel profile has been reached. Please close unused vessel profile windows before proceeding further.");
								return;		
							}
							break;
						default:
							document.cookie= windownames + "=" + escape(windownames)
							newWindow = window.open(url + '?' + para, windowName, windowType)
							newWindow.window.focus()
					}
					
				}
			} else {
				window.location.href = url + "?" + para;
			}
		} else if (method == "post") {
			form.method = method
			if (newWin == true) {
				var_proceed = true;
				if (ssc_checkWindowNum()) {
					
					switch (windowName.substr(0, 2))
					{
						case "es" :
							if (ssc_checkESWindowNum(windowName)) {
								document.cookie= windownames + "=" + escape(windownames)
								newWindow = window.open('', windowName, windowType)
								newWindow.window.focus()
								form.target = windowName
							} else {
								alert("Maximum allowable opened windows for editing schedule has been reached. Please close unused editing schedule windows before proceeding further.");
								return;
							}
							break;
						case "ep":					
							if (ssc_checkEPWindowNum(windowName)) {
								document.cookie= windownames + "=" + escape(windownames)
								newWindow = window.open('', windowName, windowType)
								newWindow.window.focus()
								form.target = windowName
							} else {
								alert("Maximum allowable opened windows for proforma has been reached. Please close unused proforma windows before proceeding further.");
								return;
							}
							break;
						case "ev":
							if (ssc_checkEVWindowNum(windowName)) {
								document.cookie= windownames + "=" + escape(windownames)
								newWindow = window.open('', windowName, windowType)
								newWindow.window.focus()
								form.target = windowName
							} else {
								alert("Maximum allowable opened windows for vessel profile has been reached. Please close unused vessel profile windows before proceeding further.");
								return;
							}
							break;
						default:
							document.cookie= windownames + "=" + escape(windownames)
							newWindow = window.open('', windowName, windowType)
							newWindow.window.focus()
							form.target = windowName 
					}
					
				}
			} else {
				form.target = "_self"
			}
			if (form.elements[var_submit_win] != null)
				form.elements[var_submit_win].value = escape(windownames);                
			if (form.elements[var_ssl] != null)
				form.elements[var_ssl].value = ssc_isSSL(form.action);            
			form.submit()
		} else {
			return
		}
	//} else { 
	//	alert('Please wait while processing...')
	//}
}

function ssc_getWindowName(aWinName) {
	if (aWinName == "")
  		return escape(CONST_WIN_STR + self.name);
	else
		return escape(CONST_WIN_STR + aWinName);
}

function ssc_confirmCancel (WindowTitle) {
	location.href = serverurl+"errormessages/confirmcancel.htm?" + escape(WindowTitle);
}

function ssc_setMyCookie() {
	var winnames = CONST_WIN_STR + self.name;
	ssc_everyMinute();
	self.focus(); 
}

function ssc_setSpecialCookie(aWinName) {
	var winnames = CONST_WIN_STR + aWinName
	ssc_everyMinute();
	self.focus(); 
}

function ssc_addCookie(aWinName) {
	var todays_date = new Date();
	var expires_date = new Date(todays_date.getTime() + 20000); // 20 sec from now	
	document.cookie = aWinName + "=" + escape(aWinName) + "; expires=" + expires_date.toGMTString() ;
}

function ssc_deleteCookie(id) {
	expireDate = new Date
	expireDate.setYear(expireDate.getYear() - 1)
	document.cookie = id + "=; expires=" + expireDate.toGMTString() ;
}
 
function ssc_everyMinute() { 
	ssc_addCookie(CONST_WIN_STR + self.name)
	setTimeout('ssc_everyMinute()',15000);
}

function ssc_closeSelf() {
	ssc_deleteCookie("win" + self.name)
}

function ssc_checkWindowNum() {
	if (document.cookie.split(";").length < CONST_SSC_WIN_MAX) {

		return true
	} else {
		
		alert("Maximum allowable opened windows has been reached. Please close unused " + CONST_SSC_STR + " windows before proceeding further.")
		return false
	}       
}
function ssc_checkESWindowNum(windowName) {
	
	var count=0;
	var cookieArr = document.cookie.split(";");
	var tmpName=" win" + windowName + "=" + "win" + windowName ;
	for(var i=0;i < cookieArr.length;i++) {
		var c=cookieArr[i];
		
		if (c==tmpName)
		{
			return true
		}
		if (c.substr(0, 6)==' wines')
		{
			count++;
		}
	}

	if (count >= CONST_SSC_ESWIN_MAX )
	{			
		return false
	} else {
		return true
	}
}

function ssc_checkEPWindowNum(windowName) {
	var count=0;
	var cookieArr = document.cookie.split(";");
	var tmpName=" win" + windowName + "=" + "win" + windowName ;
	for(var i=0;i < cookieArr.length;i++) {
		var c=cookieArr[i];
		if (c==tmpName)
		{
			return true
		}
		
		if (c.substr(0, 6)==' winep')
		{
			count++;
		}
	}

	if (count >= CONST_SSC_EPWIN_MAX )
	{			
		return false
	} else {
		return true
	}
}

function ssc_checkEVWindowNum(windowName) {
	var count=0;
	var cookieArr = document.cookie.split(";");
	var tmpName=" win" + windowName + "=" + "win" + windowName ;
	for(var i=0;i < cookieArr.length;i++) {
		var c=cookieArr[i];
		if (c==tmpName)
		{
			return true
		}
		if (c.substr(0, 6)==' winev')
		{
			count++;
		}
	}

	if (count >= CONST_SSC_EVWIN_MAX )
	{			
		return false
	} else {
		return true
	}
}

function ssc_resetCookies() {
	DeleteCookie("SSM_LOGGED_IN","","");
	if (document.cookie!="") {
		thisCookie = document.cookie.split(";")
		expireDate = new Date
		expireDate.setYear(expireDate.getYear() - 1)
		for (i=0; i < thisCookie.length; i++) {
			cookieName= thisCookie[i].split("=")[0]
			cookieValue = thisCookie[i].split("=")[1]
			if (cookieName.substring(0,1)==" ") { aWin = (cookieName.substring(1,4)==CONST_WIN_STR)}
			else  {aWin = (cookieName.substring(0,3)==CONST_WIN_STR)}
			if (aWin) {
				  if ((cookieValue.substring(3) != self.name) && (cookieValue.substring(3) != CONST_SSC_STR_ALL_LOW_CASE)) {
					document.cookie = cookieName + "=;expires=" + expireDate.toGMTString()
				  }
			}      
		}                               
	}
}


function ssc_closeAllWindows(){

    if (document.cookie != "")
    {
      thisCookie = document.cookie.split(";")
      for (i=0; i < thisCookie.length; i++) 
			{
                    cookieName= thisCookie[i].split("=")[0]
                    cookieValue = thisCookie[i].split("=")[1]
				

         if (cookieName.substring(0,1)==" ") 
					aWin = (cookieName.substring(1,4)==CONST_WIN_STR);
				else  
					aWin = (cookieName.substring(0,3)==CONST_WIN_STR);
					
					
          if (aWin) 
					{
					var currWin = cookieValue.substring(3);

					if ((currWin != self.name) && (currWin != CONST_SSC_STR_ALL_LOW_CASE)) 
					{
						newWin = window.open("", currWin);
						newWin.close()
					}
					}      
			}                               
    }
}

function ssc_changeOrganizationView () {
	if(confirm("Are you sure you want to change organization view?  If yes, all the popup windows for " 
		+ CONST_SSC_STR + " will be closed.")) 
	{
		    ssc_closeAllWindows();		
      return true;
	}
	
	return false;
}

function ssc_signoff (servleturl) {
	if(confirm("Are you sure you want to sign off?  If you sign off, all the windows for " 
		+ CONST_SSC_STR + " will be closed.")) 
		{
		    ssc_closeAllWindows();
		
        ssc_getSignOffPage();
		
		if (self.name != CONST_SSC_STR_ALL_LOW_CASE) {
        	self.close(1);
        }
	}
}



function isAlpha( chr ) {
	if ( ( ((chr >= "a") && (chr <= "z")) || ((chr >= "A") && (chr <= "Z")) ) ) 
		return true;
	else 
		return false;
}

function isAlphaNum( chr ) {
	if ( ( ((chr >= "a") && (chr <= "z")) || ((chr >= "A") && (chr <= "Z")) || ((chr >= "0") && (chr <= "9")) ) ) 
		return true;
	else 
		return false;
}


function SetCookie (name, value) {
         var argv = SetCookie.arguments;
         var argc = SetCookie.arguments.length;
         var expires = (argc > 2) ? argv[2] : null;
         var path = "/"; //(argc > 3) ? argv[3] : null;
         var domain = (argc > 4) ? argv[4] : null;
         var secure = (argc > 5) ? argv[5] : false;
		 
         document.cookie = name + "=" + escape (value) +
         ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
         ((path == null) ? "/" : ("; path=" + path)) +
         ((domain == null) ? "" : ("; domain=" + domain)) +
         ((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) endstr = document.cookie.length;
   return unescape (document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
   var arg = name+"=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
   }
   return null;
}

function DeleteCookie(name,path,domain) {
    if (GetCookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
