// ********************************************************************************
// Copyright 2004-05. JETNET,LLC. All rights reserved.
//
// $$Archive: /Evolution/JetnetWeb/Test/common/default_asp.js $
// $$Author: Mike $
// $$Date: 11/30/09 2:40p $
// $$Modtime: 11/30/09 2:40p $
// $$Revision: 1 $
// $$Workfile: default_asp.js $
//
// ********************************************************************************


/// COOKIE FUNCTIONS

var arrSubId = new Array();
var arrUserId = new Array();
var arrPassWord = new Array();
var autoLogonFlag;

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie= name + "=" + escape(value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);

  //alert(dc);

  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));
}
  
function fillArrays() {

  var fieldValues;
  var fieldArray;
                          
  fieldValues = getCookie("UserName");
 	if (fieldValues != null)
	{
    //alert("CK:[USERNAME]" + fieldValues);
    fieldArray = fieldValues.split("\&"); 
    //alert(fieldArray);
    //alert(fieldArray[0]);
    
		var n = fieldArray.length;
		for (i = 0; i < n; i++) {
		
 	    if (fieldArray[i] != "") {
 	      var tmpname = fieldArray[i].split("\=");
 	      
 	      arrUserId[i] = tmpname[1];
 	      arrSubId[i] = tmpname[0];
        
		  }  
		}
  }
    
  fieldValues = getCookie("Password");               
 	if (fieldValues != null)
	{
    //alert("CK:[PASSWORD]" + fieldValues);
    fieldArray = fieldValues.split("\&"); 
    //alert(fieldArray);
    //alert(fieldArray[0]);
		
		var n = fieldArray.length;
		for (i = 0; i < n; i++) {
		
 	    if (fieldArray[i] != "") {
 	      var tmpname = fieldArray[i].split("\=");
 	      
 	      arrPassWord[i] = tmpname[1];
		  }  
		}
  }             
 
}

function FillInputBoxes() {

 	var n = arrSubId.length;
 	 
  if (n == 0) {  // try to fill cookie arrays
    fillArrays();
    n = arrSubId.length;
    if (n == 0) return; // can't match to empty array return  
  }
    
  // Loop and Look for Match on Sub_ID
  for (i = 0; i < n; i++) {

 	  if (typeof(document.logonForm.SubID) != "undefined") {
 	    if (document.logonForm.SubID.value != null && document.logonForm.SubID.value != "") {
        
        if (arrSubId[i] == document.logonForm.SubID.value) {
          document.logonForm.UserID.value = arrUserId[i];
          document.logonForm.UserPassword.value = arrPassWord[i];
         
        }
      }
    }               
  }  
  
}

function checkForAutoLogon(whichSite) {
  var fieldValues;

  //alert(whichSite);
  
  if (window != top) {
    top.location.href = location.href;
    //alert("NOT TOP FRAME");
  }

  fieldValues = getCookie(whichSite);
	if (fieldValues != null && fieldValues != "") {
 	  var tmpname = fieldValues.split("\=");	      
 	  autoLogonFlag = tmpname[1]; // second item is value
  }
	
  fillArrays();

}

function tryAutoLogon() {

  if (autoLogonFlag == "Y") {
    // grab the first item out of each array this should be the autologon SID, UID and PWD
    
    document.logonForm.SubID.value = arrSubId[0]
    document.logonForm.UserID.value = arrUserId[0];
    document.logonForm.UserPassword.value = arrPassWord[0];

    if ((document.logonForm.SubID.value != "") && (document.logonForm.UserID.value != "") && (document.logonForm.UserPassword.value != "")) {
      document.logonForm.submit();
    }    
  }
}

function Validator(theForm) {

  //alert("theForm.UserID.value == '" + theForm.UserID.value + "'");
  //alert("theForm.UserPassword.value == '" + theForm.UserPassword.value + "'");
  
  //alert("theForm.UserEmail.value == '" + theForm.UserEmail.value + "'");
  //alert("theForm.NewPassword.value == '" + theForm.NewPassword.value + "'");

  if (theForm.UserID.value == "") {

    if (theForm.UserEmail.value == "") {
      alert("Please enter your \"User Email Address\".");
      theForm.UserEmail.focus();
      return (false);
    }
    else {

      if (theForm.UserID.value == "" && theForm.UserEmail.value == "") {
        alert("Please enter your \"Username\".");
        theForm.UserID.focus();
        return (false);
      }
    }
  }
    
  if (theForm.UserPassword.value == "") {
	
	  if (theForm.NewPassword.value == "") {
	    alert("Please enter your \"Password\".");
	    theForm.NewPassword.focus();
	    return (false);
	  }
	  else {

	    if (theForm.UserPassword.value == "" && theForm.NewPassword.value == "") {
	      alert("Please enter your \"Password\".");
	      theForm.UserPassword.focus();
	      return (false);
	    }
	  }
	} 

	return (true);
}
