/**
 * This function allows you to add onload functions 
 * without overridding other functions that may have been
 * assigned to window.onLoad()
 */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
domain = ".sybase.com"
path = "/"
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
domain = ".sybase.com"
path = "/"
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
	
function login_out(param){
	if (getCookie("PSID") != null){
		try {
			document.write( sLogout);
		} catch (e){
			document.write( "Logout");
		}
	} else {
		try { 
			document.write( sLogin);
		} catch (e){
			document.write("Login");
		}
	}
}

function login_refer(param){
    if (whichEnvironment() == "dev"){
	    server = "https://login-dev.sybase.com"
    } else if (whichEnvironment() == "test"){
	    server = "https://login-test.sybase.com"
    } else {
	    server = "https://login.sybase.com"
    }
    
    var refer = param;
    
	if (getCookie("PSID") == null){
		document.location.href = server +"/login/userLogin.do?refer="+escape(refer);
	} else {
		document.location.href = refer;
	}
}

function login_out_link(param){
    if (whichEnvironment() == "dev"){
	    server = "https://login-dev.sybase.com"
    } else if (whichEnvironment() == "test"){
	    server = "https://login-test.sybase.com"
    } else {
	    server = "https://login.sybase.com"
    }
    var explicitRefer = "";
    try {
        referObj = document.getElementById("loginRefer");
        explicitRefer = referObj.value;
        param = (explicitRefer == "")?param : explicitRefer;
    } catch (e) {
        explicitRefer = "";
    }
    var refer = param;
    var currentURL = window.location.href;

    if (param.indexOf("/nav.do") != -1) { refer = "";}
    if (currentURL.indexOf("logout") != -1 || currentURL.indexOf("Password.do") != -1){
		refer = document.getElementById("returnRefer").value;
	}
	if (getCookie("PSID") != null){
        // logging out from eshop should always have a refer of /eshop
        if (currentURL.indexOf("/eshop/") != -1) refer = "/eshop";
		document.location.href = server +"/login/logout.do?refer="+escape(refer);
	} else {
		document.location.href =server +"/login/userLogin.do?refer="+escape(refer);
	}
}

/**
 * shows the My Account link and pipe separater in header if logged in
 * parameter includePipe is optional, if includePipe is false, the pipe will
 * not be appended, if its blank or true, pipe will be included
 **/
function showHideAccountLink(includePipe) {
	if (whichEnvironment() == "dev"){
	    server = "http://profile-dev.sybase.com"
   	} else if (whichEnvironment() == "test"){
	    server = "http://profile-test.sybase.com"
   	} else {
	    server = "http://profile.sybase.com"
   	}
   	var pipe = "&nbsp;&nbsp;<span class='wwText'>|</span>";
   	if(includePipe == 'false') {
   		pipe = "";
   	}
	if (getCookie("PSID") != null){
		document.write("<a href='"+server+"/login/myaccount.do' class='contextualLink'>My Account</a>" + pipe + "&nbsp;");	
	} 
}

// returns dev, test, or prod
function whichEnvironment(){
	var theLocation = document.location.href;
	if (theLocation.indexOf("-dev.sybase.com") > -1 ){
		return "dev";
	} else if (theLocation.indexOf("-test.sybase.com") > -1){
		return "test";
	} else {
		return "prod";
	}
}