// #####################
// All Functions In OPS
// #####################

var CONST_OPS_STR = "OperationSmart"
var CONST_OPS_STR_ALL_LOW_CASE = "operationsmart"
var CONST_WIN_STR = "win"
var CONST_OPS_WIN_MAX = 15
var CONST_MAIN_WIN_STR = "opscenter"
var screen_ht = screen.availHeight;
var screen_wd = screen.availWidth;
var screen_left = 0;
var screen_top = 10;

var copyright_period = "2003 - 2004"
var last_mod_date_tou = " (last updated April 2003)"
var last_mod_date_pss = " (last updated April 2003)"
var last_mod_date_tou_format = "17 April 2003"
var last_mod_date_pss_format = "17 April 2003"

//Date difference value in seconds
var seconds_in_day=86400000
var Days_8=7                   //7 as both start and end dates are inclusive.
var Days_Week_1=7
var Days_Week_2=14
var Days_Week_4=30

var max_date_range_diff_fop=Days_8
var max_date_range_diff_cop=Days_8
var max_sec_range_diff_fop=Days_8*seconds_in_day
var max_sec_range_diff_cop=Days_8*seconds_in_day

FULL= "toolbar,directories,resizable,status,scrollbars,menubar,location,"
NONE= "scrollbars,resizable,status,"
MENUONLY= "menubar,scrollbars,resizable,status,"
ALL = ""

TOUURL  = serverurl+"footer/ops_terms_of_use.htm";
COPYURL = serverurl+"footer/ops_terms_of_use.htm#Section5";
PSURL  = serverurl+"footer/ops_privacy_security.htm";
EXITURL = serverurl+"exit.htm";

appdomain = "; domain=.operationsmart.com"
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_proceed = true

function getForgotPwd()
{
        document.ops_sso_login.elements['hdr.ref_page'].value = document.title;
        document.ops_sso_login.elements[var_work_id].value = "OpsForgotPasswordWork";
        document.ops_sso_login.elements[var_action].value = "forward";
        document.ops_sso_login.elements[var_task_id].value = "OpsForgotPasswordTask";
        document.ops_sso_login.elements[var_cache_id].value = "ops_common_forgot_password";
        document.ops_sso_login.elements[var_new_task].value = true;
        return true;
}
function getContactUs()
{
        document.frm_header_footer.elements['hdr.ref_page'].value = document.title;
        document.frm_header_footer.elements[var_work_id].value = "OpsContactUsWork";
        document.frm_header_footer.elements[var_action].value = "forward";
        document.frm_header_footer.elements[var_task_id].value = "OpsContactUsTask";
        document.frm_header_footer.elements[var_cache_id].value = "ops_common_contact_us";
        document.frm_header_footer.elements[var_new_task].value = true;
        return true;
}
function getVendorMainPage() {
        document.frm_header_footer.elements['hdr.ref_page'].value = document.title;
        document.frm_header_footer.elements[var_work_id].value = "VendorOperationWork";
        document.frm_header_footer.elements[var_action].value = "forward";
        document.frm_header_footer.elements[var_task_id].value = "VendorOperationTask";
        document.frm_header_footer.elements[var_cache_id].value = "fop_m_main_page";
        document.frm_header_footer.elements[var_new_task].value = true;
        return true;
}
function getCarrierMainPage() {
        document.frm_header_footer.elements['hdr.ref_page'].value = document.title;
        document.frm_header_footer.elements[var_work_id].value = "CarrierOperationWork";
        document.frm_header_footer.elements[var_action].value = "forward";
        document.frm_header_footer.elements[var_task_id].value = "CarrierOperationTask";
        document.frm_header_footer.elements[var_cache_id].value = "cop_m_main_page";
        document.frm_header_footer.elements[var_new_task].value = true;
        return true;
}
function isSSL(url)
{
	if (url.indexOf("http") == -1 )
	{ return isSSL(location.protocol) }
	if (url.indexOf("https") == -1 )
	{ return false }
	else
	{ return true }
}
function popupTOU(){
	popupPageCenter(TOUURL,"opsfooter","l",NONE);
}
function popupCopyRight(){
	popupPageCenter(COPYURL,"opsfooter","l",NONE);
}
function popupPS(){
	popupPageCenter(PSURL,"opsfooter","l",NONE);
}
function popupPageCenter(url,windowname,winsize,windowparms) {
  if (var_proceed)
  {
        var_proceed = false;
     if(!windowExists(windowname)){
     	var_proceed = true;
	if (checkWindowNum()){
		winsize = winsize.toUpperCase();
		var ht = screen.availHeight;
		var wd = screen.availWidth;
		var left = 0;
		var top = 0;
		if (winsize == "S") scalar = 0.33;
		 else if (winsize == "M") scalar = 0.50;
		  else scalar = 0.75;
		w_ht = Math.round(scalar * ht);
		w_wd = Math.round(scalar * wd);
		left = (wd - w_wd) /2;
		top = (ht - w_ht) /2;
		windowparms += "left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + ",";
		windowparms += "height=" + w_ht + ",width=" + w_wd;
 		NewWindow = window.open(url,windowname,windowparms);
 		NewWindow.focus();
	}else{
              	return;
        }
     }else{	var_proceed = true;
               	newWindow = window.open('', windowname);
               	newWindow.focus();
     }
  } else
        { alert('Please wait while processing...')}
}
function submitClickSmallWinCenter(newWin, windowName, winsize, url, form, method, para)
{if (var_proceed) {
        var_proceed = false;
        windownames = getWindowName(windowName);
	winsize = winsize.toUpperCase();
	if (winsize == "S") scalar = 0.33;
	 else if (winsize == "M") scalar = 0.50;
	  else scalar = 0.75;
	w_ht = Math.round(scalar * screen_ht);
	w_wd = Math.round(scalar * screen_wd);
	w_left = Math.round((screen_wd-w_wd)*0.5);
	w_top = Math.round((screen_ht-w_ht)*0.5);
	var windowparms = "resizable=yes,status=yes,scrollbars=yes,";
	windowparms += "left=" + w_left + ",top=" + w_top + ",screenX=" + w_left + ",screenY=" + w_top + ",";
	windowparms += "height=" + w_ht + ",width=" + w_wd;
        if (method == "get")
        {
            if(!windowExists(windowName)){
		para = var_submit_win + '=' + escape(windownames) + '&' + para + '&' + var_ssl + '=' + isSSL(url)
		var_proceed = true;
                if (newWin == true)
                {	
                        if (checkWindowNum())
                        {
                                //document.cookie= windownames + "=" + escape(windownames)
                                newWindow = window.open(url, windowName, windowparms)
                                newWindow.window.focus()
                        }else{
                        	return;
                        }
                }
                else
                {
	            window.location.href = url + "?" + para;
                }
            }else{  var_proceed = true;
               	    newWindow = window.open('', windowName, windowparms);
                    newWindow.focus();
            }
        }
        else if (method == "post")
        {
                form.action = url
                form.method = method
            if(!windowExists(windowName)){
            	var_proceed = true;
                if (newWin == true)
                {	
                        if (checkWindowNum())
                        {
                                //document.cookie= windownames + "=" + escape(windownames)
                                newWindow = window.open('', windowName, windowparms)
                                newWindow.window.focus()
                                form.target = windowName
                        }else{
                        	return;
                        }
                }
                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 = isSSL(form.action);
                form.submit()
            }else{	var_proceed = true;
                	newWindow = window.open('', windowName, windowparms);
                	newWindow.focus();
            }
        }
        else
        { return }
} else
        { alert('Please wait while processing...')}
}

function submitClick(newWin, windowName, windowType, url, form, method, para)
{
  if (var_proceed)
  {
        var_proceed = false;
	windownames = getWindowName(windowName);
        if (method == "get")
        {
            para = '&' + var_submit_win + '=' + escape(windownames) + '&' + var_ssl + '=' + isSSL(url)
            if(!windowExists(windowName)){
            	var_proceed = true;
                if (newWin == true)
                {	
                        if (checkWindowNum())
                        {
                                //document.cookie= windownames + "=" + escape(windownames)
                                var w_ht = screen.availHeight - 60;
                                var w_wd = screen.availWidth - 20;
                                windowType += "left=10,top=10,screenX=10,screenY=10,";
                                windowType += "height=" + w_ht + ",width=" + w_wd;
                                newWindow = window.open(url + para, windowName, windowType)
                                newWindow.window.focus()
                        }else{
                        	return;
                        }
                }
                else
                {
                        window.location.href = url + para;
                }
            }else{	var_proceed = true;
                	newWindow = window.open('', windowName, windowType);
                	newWindow.focus();
            }
        }
        else if (method == "post")
        {
                form.action = url
                form.method = method
            if(!windowExists(windowName)){
            	var_proceed = true;
                if (newWin == true)
                {	
                        if (checkWindowNum())
                        {
                                //document.cookie= windownames + "=" + escape(windownames)
                                var w_ht = screen.availHeight - 60;
                                var w_wd = screen.availWidth - 20;
                                windowType += "left=10,top=10,screenX=10,screenY=10,";
                                windowType += "height=" + w_ht + ",width=" + w_wd;
                                newWindow = window.open('', windowName, windowType)
                                newWindow.window.focus()
                                form.target = windowName
                        }else{
                        	return;
                        }
                }
                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 = isSSL(form.action);
                form.submit();
            }else{	var_proceed = true;
                	newWindow = window.open('', windowName, windowType);
                	newWindow.focus();
            }
        }
        else
        { return }
  } else
        { alert('Please wait while processing...')}
}
function getWindowName(aWinName)
{
  if (aWinName == "")
  	return escape(CONST_WIN_STR + self.name);
  else
	return escape(CONST_WIN_STR + aWinName);
}
function setMyCookie() {
  var winnames = CONST_WIN_STR + self.name;
  everyMinute();
  self.focus();
}
function setSpecialCookie(aWinName) {
  var winnames = CONST_WIN_STR + aWinName
  everyMinute();
  self.focus();
}
function 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) + "; path=/; expires=" + expires_date.toGMTString() ;
}
function deleteCookie(id)
{
  expireDate = new Date
  expireDate.setYear(expireDate.getYear() - 1)
  document.cookie = id + "=; path=/; expires=" + expireDate.toGMTString() ;
}
function everyMinute() {
   addCookie(CONST_WIN_STR + self.name)
   setTimeout('everyMinute()',15000);
}
function closeSelf()
{ deleteCookie("win" + self.name) }
function checkWindowNum(){
	var windowCount = 0;
	thisCookie = document.cookie.split(";")
	for (i=0; i < thisCookie.length; i++) {
		cookieName= doTrim(thisCookie[i].split("=")[0]);
		if(cookieName.substring(0,3)==CONST_WIN_STR) {
			windowCount=windowCount+1;
		}
	}
	if (windowCount < CONST_OPS_WIN_MAX) {
	  return true
	} else {
	  alert("Maximum allowable opened windows has been reached. Please close unused " + CONST_OPS_STR + " windows before proceeding further.")
	  return false
	}
}
function resetCookies()
{
        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_OPS_STR_ALL_LOW_CASE))
                        {
                                document.cookie = cookieName + "=;expires=" + expireDate.toGMTString()
                        }}
                }
        }
}
function w(astring){
        document.writeln(astring);
}
function signoff(servleturl){
   if(confirm("Are you sure you want to sign off?  If you sign off, all the windows for " + CONST_OPS_STR + " will be closed.")) {
        // modified for 10g upgrade
        //if (document.cookie!=""){
        //    newWindow = window.open("", "opscenter");
        //    newWindow.focus();
        //    newWindow.close();
        //}
        //if (self.name != CONST_OPS_STR_ALL_LOW_CASE) {self.close()}
        closeAllChild();
        //newWindow = window.open("", "opscenter");
        //newWindow.focus();
   }
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function autosignoff(){
var cookieValueSubStr;
        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) {
                           cookieValueSubStr=cookieValue.substring(3);
                        if ((cookieValueSubStr != self.name) && (cookieValueSubStr != CONST_OPS_STR_ALL_LOW_CASE)) {
                        if(!isDigit(cookieValueSubStr.substring(0,1))) {
                            if(cookieValueSubStr != CONST_MAIN_WIN_STR) { // added for 10g upgrade
                        	  NewWindow = window.open("", cookieValueSubStr)
                        	  NewWindow.window.close()
                    	    }
                        }
                        }}
                }
        }
        deleteCookie("win" + "opscenter");
	    //NewWindow = window.open(appSSLUrl+"sys_sign_off.jsp?sw=", "signoff", 'scrollbars=yes,width=450,height=350,top=0,left=500'); // commented for 10g upgrade
        if (self.name != CONST_OPS_STR_ALL_LOW_CASE) {self.close();}
}

function windowExists(windowName)
{
	if(windowName!=""){
	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)=="win")}
			else  {aWin = (cookieName.substring(0,3)=="win")}
			if (aWin) {
			if ((cookieValue.substring(3) != self.name) && (cookieValue.substring(3) == windowName)) {
				return true;
			}}
		}
	} }
	return false;
}

var exp_alphanumeric = /[\s0-9a-z]+/i ;
var exp_noSpace = /[^\s]/ ;
var exp_Space_head = /^(\s+)(\S?.*)$/ ;
var exp_Space_tail = /^(.*\S+)(\s+)$/ ;
var exp_Space_mid  = /^(\S+)(\s+)(\S?.*)$/ ;

function doTrim(trimComponent)  {
	trimComponent = trimComponent.replace(exp_Space_head, "$2");
	trimComponent = trimComponent.replace(exp_Space_tail, "$1");
	return trimComponent;
}

function strReplace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += strReplace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function formGetElementNamed(elementName, theForm) {
	var formElement = theForm.elements[elementName];
	if (formElement != null) {
		return formElement;
	}
	return null;
}

function formSetNamedInput(value, inputName, theForm) {
	theForm.elements[inputName].value = value;
}

function formSetFocus(inputName, theForm) {
	theForm.elements[inputName].focus();
}

function formSetCheckedInput(value, inputName, theForm) {
	var inputField = formGetElementNamed(inputName, theForm);
	if((inputField != null) && (inputField.disabled != true)) {
	  if(value=="true") {
		inputField.checked = true;
	  }else {
		inputField.checked = false;
	  }
	}
}

function formDisableNamedInput(inputName, theForm) {
	var inputField = formGetElementNamed(inputName, theForm);
	if (inputField != null) {
		inputField.disabled = true;
	}
}

function formDisableReadOnlyNamedInput(inputName, theForm) {
	var inputField = formGetElementNamed(inputName, theForm);
	if (inputField != null) {
		inputField.readOnly = false;
	}
}

function formEnableReadOnlyNamedInput(inputName, theForm) {
	var inputField = formGetElementNamed(inputName, theForm);
	if (inputField != null) {
		inputField.readOnly = true;
	}
}


function formSetFieldFocus(inputName, theForm) {
	theForm.elements[inputName].focus();
	theForm.elements[inputName].select();
}

//
//@desc This function returns the (absolute/rounded) diff between two dates in days
//@param dtTime1 time in seconds for date 1
//@param dtTime2 time in seconds for date 2
//
function daysDiff( dtTime1, dtTime2 ){
var timediff;
var days;

timediff = dtTime1 - dtTime2;
days = Math.floor(timediff / (1000 * 60 * 60 * 24));
return days;

}
function toggleRow(button, elementName) {
  elements = document.getElementsByName(elementName);
  for(j=0; j < elements.length;j++) {
    ele = elements[j];
    if(ele.style.display == 'none')
    {
      ele.style.display = "block";
      button.src = "/images/compress.jpg";
    }else{
      ele.style.display = "none";
      button.src = "/images/expand.jpg";
    }
  }
 }

if (window.Event)
  document.captureEvents(Event.MOUSEUP);
function nocontextmenu() {
  event.cancelBubble = true, event.returnValue = false;
  return false;
}

function norightclick(e) {
  if (window.Event) {
    if (e.which == 2 || e.which == 3) return false;
  }
  else if (event.button == 2 || event.button == 3) {
    event.cancelBubble = true, event.returnValue = false;
    return false;
  }
}

if (document.layers)
  document.captureEvents(Event.MOUSEDOWN);
document.oncontextmenu = nocontextmenu;
document.onmousedown = norightclick;
document.onmouseup = norightclick;

document.onkeydown = mykeyhandler;
function mykeyhandler() {
    if (window.event && window.event.keyCode == 8) {
	var e=window.event; 
        var tag=e.srcElement.tagName;
        var type='';
	if(e.srcElement.type) {	type = e.srcElement.type; }
       	if(tag=='BODY'|| (tag != 'TEXTAREA' && tag != 'INPUT')||(type == 'checkbox' || type == 'radio')) {
		e.returnValue = false;
		e.cancelBubble = true;
		return false;
	}
    }else { return true; }
}


function closeAllChild(){
var cookieValueSubStr;
        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) {
                           cookieValueSubStr=cookieValue.substring(3);
                        if ((cookieValueSubStr != self.name) && (cookieValueSubStr != CONST_OPS_STR_ALL_LOW_CASE)) {
                        if(!isDigit(cookieValueSubStr.substring(0,1))) {
                            if(cookieValueSubStr != CONST_MAIN_WIN_STR) {
                        	NewWindow = window.open("", cookieValueSubStr)
                        	NewWindow.window.close()
                    	    }
                        }
                        }}
                }
        }
        deleteCookie("win" + "opscenter");
	//NewWindow = window.open(appSSLUrl+"sys_sign_off.jsp?sw=", "signoff", 'scrollbars=yes,width=450,height=350,top=0,left=500');
        NewWindow = window.open("", CONST_MAIN_WIN_STR)
        NewWindow.window.location = appSSLUrl+"sys_sign_off.jsp?sw=";

        if (self.name != CONST_OPS_STR_ALL_LOW_CASE) {self.close();}
}
