﻿var SESSIONSTALEMESSAGE = "Session timed out";

function demoinit() {
    var nope = document.getElementById("nope");
    var div = document.getElementById("logfaildiv");
    var guy = document.getElementById("email");

    if (nope != null && nope.value == "1") {
            if (guy.value == "")
                return;
                
            div.innerText = "Please use the supplied demo id and password.";
            div.style.display = "block";
            nope.value = "0";
        }
    else 
        init();    
}

function init() {
    var nope = document.getElementById("nope");
    var div = document.getElementById("logfaildiv");
    var guy = document.getElementById("email");
    if (div == null)
        return;
        
    if (nope != null) {
        if (nope.value == "1") {
            if (guy.value == "")
                return;
                
            div.innerText = "Login failed for " + guy.value;
            div.style.display = "block";
            nope.value = "0";
        }
        else if (nope.value == "9") {    
            div.style.color = "Green";
            div.innerText = "Loading your personal settings...";
            div.style.display = "block";
            nope.value = "0";

//            window.location = "./lbec/analytics.aspx";
        }
    }
    else {
        div.style.display = "none";
    }        
}

function admininit() {
    var nope = document.getElementById("nope");
    var div = document.getElementById("logfaildiv");
    var guy = document.getElementById("email");
    
    if (nope != null) {
        if (nope.value == "1") {
            if (guy.value == "")
                return;
                
            div.innerText = "Login failed for " + guy.value;
            div.style.display = "block";
            nope.value = "0";
        }
        else if (nope.value == "9") {    
            div.style.color = "Green";
            div.innerText = "Loading your personal settings...";
            div.style.display = "block";
            nope.value = "0";

            window.location = "./lbec/analytics.aspx";
        }
        else if (nope.value == "10") { 
            div.style.color = "Green";
            div.innerText = "Please wait for one-time data conversion to the latest format...";
            div.style.display = "block";
            nope.value = "0";

            window.location = "./admin/adminmain.aspx";
        }

    }
    else {
        div.style.display = "none";
    }        
}

function pbarVisible(on) {
    if (document.getElementById("pbar") != null) {
        if (on)
            document.getElementById("pbar").style.display = "block";
        else
            document.getElementById("pbar").style.display = "none";
    }
}

function processDisplayCallBackResult(arg) {
    if (arg.indexOf("<DISPLAYBLOCK>") > -1) {
        var srcStartIdx = arg.indexOf("<DISPLAYBLOCK>");
       if (srcStartIdx != -1) {
            srcStartIdx += "<DISPLAYBLOCK>".length;

        var srcEndIdx = arg.indexOf("</DISPLAYBLOCK>");
        src = arg.substring(srcStartIdx, srcEndIdx);
        if (src != null && src != "" )
            var display = new Function(src);
            display();
        }        
    }    
}

function ProcessCallBackError(arg, context) {
    if (pbarVisible)
        pbarVisible(false);
   
    var ctl = document.getElementById("failalert");
    if (ctl != null && ctl.value != null && ctl.value.toLowerCase().indexOf("timed out") > -1) {
        ctl.value = "";
        window.location = "../help_center/SessionTimeout.htm";
    }
        
}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2);
//	var d = (a.length > 1) a[1] : 00;
//	if (d.length > 2)
//	    d = d.substring(0,2);
	    
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
//	if(d.length < 1) { amount = n; }
//	else { amount = n + '.' + d; }
   
	amount = minus + n;
	return amount;
}
// end of function CommaFormatted()

function receiveLogoutCallbackResult(arg, context) {
    var src = getRequestContent("<LOGOUTPAGE>", "</LOGOUTPAGE>", arg);
    if (src != null && src != "" )
        window.location = src;
}

function globalVersionInclude() {
if (document.getElementById("version") != null)
    document.getElementById("version").text = "patents pending v 0.55";
}

function locateUser() {
  serviceCall.webService.callService(locaterResult,
    "Accept", email.value,
    password.value);
}

function locaterResult(result) {
  if(result.value == "notfound")
    alert(email.value + " is not yet registered.");
  else if(result.value == null)
    alert("The 'locate' service is not available");
  else {
      if (result.value != "") {
          setCookie(result.value);  
          alert("welcome " + email.value +".");
      }
  }    
}  
  
function getWindowDimensions() {
    var arr = new Array(4);
    
     if (navigator.appName=="Netscape") {
      top = window.screenY;
      left = window.screenX;
      width = window.innerWidth;
      hgt = window.innerHeight;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      width = document.body.offsetWidth;      
      hgt  = document.body.offsetHeight;      
     }

    arr[0] = 0;
    arr[1] = 0;
    arr[2] = width;
    arr[3] = hgt;
    return arr;
}
  
function setCookie(cVal) {
    var strings = new Array();
    strings = cVal.split(";");

    var start = user = emailname = "";
    if (strings[0] != "") {
        start = strings[0].indexOf("=");
        user = strings[0].substring(start+1);
        document.cookie = "client" + "=" + user; 
        emailname = user;
    }

    if (strings[1] != "") {
        start = strings[1].indexOf("=");
        user = strings[1].substring(start+1);
        document.cookie = "prospectruser" + "="+escape(user)+ ";";                
    }
        
    if (strings[2] != "") {
        start = strings[2].indexOf("=");
        user = strings[2].substring(start+1);
        document.cookie = "prospectrid" + "=" + user;                
    }
  
    if (strings[3] != "") {
        start = strings[3].indexOf("=");
        user = strings[3].substring(start+1);
        document.cookie = "companyName" + "=" + user;                
    }
  
  return emailname;  
}

function doSearch() {
    var arg = document.getElementById("zoom_query").value;    
    window.open("http://localhost/search/search.asp?zoom_query=" + arg);
}

function evaluateResult(result) {
    var msg = document.getElementById("message");
    
  if(result == "notfound") {
    msg.value = document.getElementById('username').value + " is not yet registered.";
    return false;
  }  
  else if(result == "retry") {
    msg.value = new String('Longbow is not available at this time. \nPlease retry in a few minutes');
    return false;
  }
  else {
      if (result != "") {
          var user = setCookie(result);  
          msg.value = "welcome " + user;
      }
  }
  
  return true;    
}  
 
var resPopup; 
function resolverLoad() {    
    document.getElementById('pbardiv').style.display = "none";

    var resp = document.getElementById('response').value; 
    if (!evaluateResult(resp) ) {
        if (resp.toLowerCase().indexOf("retry") > -1) {        
            document.getElementById("closediv").style.display = "block";
        }
        else {
            document.getElementById("regdiv").style.display = "block";
        }
        
        window.focus();
    }    
    else        
        doContinue();
}

function resolver() {
    var top = window.screenY;
    var left = window.screenX;
    var uid = document.getElementById('email').value; 
    var pwd = document.getElementById('password').value; 

	resPopup = window.open('resolver.aspx?f1=' + uid + '&f2=' + pwd,	
		'Validating','resizable=0,dependent=0,titlebar=0,toolbar=0,location=0,menubar=0,width=400,height=100,left=' + left + ',top=' + top);
}
 
function doContinue() {
    window.opener.location = "lbec/analytics.aspx"
    window.close();
}
 
function doRegister() {
    window.opener.location = "./my_account/myaccount_accountinfo.aspx"
    window.close();
}

function getRequestContent(openkey, closeKey, arg) {
    var content = "";
    var srcStartIdx = arg.indexOf(openkey);
    var srcEndIdx = -1;

    if (srcStartIdx > -1) {
        srcStartIdx += openkey.length;
        srcEndIdx = arg.indexOf(closeKey);
        srcEndIdx = srcEndIdx - srcStartIdx;
        content = arg.substring(srcStartIdx, srcStartIdx+srcEndIdx);
    }

    return content;
}
     
function getRequestContentArray(openkey, closeKey, arg) {
    var endIdx = arg.indexOf(closeKey);
    var str = arg;
    var retList = "";
    while (endIdx > -1 && str.length > 0) {
        endIdx += closeKey.length;
        var frag = getRequestContent(openkey, closeKey, str);
        str += (retList == "") ? frag : "," + frag;
        str = str.substring(endIdx);         
        }
        
    return retList;    
}
     
function clearInfo() {
    var info = document.getElementById("infofld");
    if (info != null) 
        info.value = "";
}
     
function highlightAllRows(table) {
    var rows = table.getElementsByTagName("tr");  
    for(var i = 0; i < rows.length; i++){   
        var row = rows[i];        
        unhighlightRow(row);
    }
}

var oldrowbgcolor = null;
function highlightRow(row) {       
    if (oldrowbgcolor == null)
        oldrowbgcolor = row.style.backgroundColor;
      row.style.backgroundColor = "#FFE7BA"; 
}

function unhighlightRow(row) {
     row.style.backgroundColor = oldrowbgcolor; 
}

function unhighlightAllRows(table) {
    var rows = table.getElementsByTagName("tr");  
    for(var i = 0; i < rows.length; i++){   
        var row = rows[i];        
        unhighlightRow(row);
    }
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;
 
function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
	}

function validateForm(){
	var emailID=document.getElementById("email");
    var div = document.getElementById("logfaildiv");

    var isOK = true;
    	
	if ((emailID.value==null)||(emailID.value=="")){		
		emailID.focus();
		isOK = false;
	}
	if (echeck(emailID.value)==false){
		emailID.focus()		
		isOK = false;
	}
		
	if (!isOK) {
        div.innerText = "Please enter a valid email address";
        div.style.display = "block";
	}

	return isOK;
 }

function receiveReturnPointCallbackResult(arg, context) {
    var src = getRequestContent("<RETURNPOINT>", "</RETURNPOINT>", arg);
    if (src != null && src != "" )
        window.location = src;
}
