var oXmlHttp         = null;
var trys             = 3;
var checkUID         = 0;
var checkPassword    = 1;
var emailPassword    = 2;
var badUID_PWD       = "1";
var badUSER_ID       = "1";
var badEMAIL_ADDRESS = "2";
var pwdNotSent       = "2";
var emailSendERROR   = "3";
var passwordSENT     = "0";
var instructionsOpen = true;
var loginOpen        = false;
var ClockMode        = "shownoon";
var onLoadCheck      = false;
window.defaultStatus  = "IPRO | Loading Data, Please Wait...";

setInterval('buildCLOCK()',1000);

function checkUID_PWD_SNDPWD(whichFctn) {
   if ( whichFctn != emailPassword )
      toggleDiv('sendPasswordDIV','hide');
   if ( whichFctn == checkUID )
      writeIntoDiv("userID","");
   if ( whichFctn == checkPassword )
      writeIntoDiv("userPWD","");

   if ( whichFctn == checkUID && document.logon.logonID.value.length < 5 ) {
      writeIntoDiv('userID','<b><font color="red"><-- Bad</font></b>');
      alert("A valid UID is 5 - 12 characters.\nSpaces not allowed.");
      return false;
   }
   if ( whichFctn == checkPassword ) {
      pwdError = isValidNewPASSWORD( document.logon.pswrd.value,0 );
      if ( pwdError != "" ) {
         if ( document.logon.pswrd.value != "" ) {
            writeIntoDiv("userPWD","<b><font color=red><-- Bad</font></b>");
            alert("A valid password is 5 - 12 characters\nwith at minimum 2 Capital Letters and 2 Numbers");
            toggleDiv('sendPasswordDIV','show');
         }
         return false;
      }
   }
   if ( whichFctn == emailPassword ) {
      email = document.logon.emailAddress.value;
      if ( email != "" && email != "Enter Email Address" ) {
         if ( email.indexOf("\@") == -1 || email.indexOf(".") == -1 ) {
            alert("An Email Addess contains an at sign (@) and at minimum 2 periods.\nPlease Re-enter");
            document.logon.emailAddress.focus()
           return false;
         }
      }
      else {
         if ( email == "Enter Email Address" )
            email = "";
      }
   }
   var jsonReturnData = "";
   if (!oXmlHttp )
      oXmlHttp = zXmlHttp.createRequest();
   else if (oXmlHttp.readyState != 0 )
      oXmlHttp.abort();
   if ( whichFctn == checkUID ) {
      document.logon.validUID.value = "0";
      var sUrl                           = "ajaxDBFunctions.html"+
                                           "?the_function=checkUID"+
                                           "&logonID="+document.logon.logonID.value;
   }
   if ( whichFctn == checkPassword ) {
      document.logon.validPWD.value = "0";
      var sUrl                           = "ajaxDBFunctions.html"+
                                           "?the_function=validLogin"+
                                           "&logonID="+document.logon.logonID.value+
                                           "&pswrd="+document.logon.pswrd.value;
   }
   if ( whichFctn == emailPassword ) {
      var sUrl = "ajaxDBFunctions.html"+
                 "?the_function=sendPassword"+
                 "&uidEmail="+email+
                 "&logonID="+document.logon.logonID.value+
                 "&pswrd="+document.logon.pswrd.value;
   }
   oXmlHttp.open("get", sUrl, true);
   oXmlHttp.onreadystatechange =
   function () {
      if ( oXmlHttp.readyState == 4 ) {
	       dummy1 = 1;
         if ( oXmlHttp.status == 200 ) {
	          dummy1 = 2;
            jsonReturnData = oXmlHttp.responseText;
            if ( whichFctn == checkUID ) {
               document.logon.validUID.value = jsonReturnData;
               if ( jsonReturnData == "1" )
                  writeIntoDiv('userID','<b><font color="green"><-- OK</font></b>');
               if ( jsonReturnData == "0" )
                  writeIntoDiv('userID','<b><font color="red"><-- Bad</font></b>');
               if ( !onLoadCheck )
                  loginTestOnLoad(true);
            }
            else {
               if ( whichFctn == checkPassword ) {
                  document.logon.validPWD.value = jsonReturnData;
                  if ( jsonReturnData == "1" )
                     writeIntoDiv('userPWD','<b><font color="green"><-- OK</font></b>');
                  if ( jsonReturnData == "0" ) {
                     writeIntoDiv('userPWD','<b><font color="red"><-- Bad</font></b>');
                     toggleDiv('sendPasswordDIV','show');
                  }
               }
               else {
                  if ( whichFctn == emailPassword ) {
                     if ( jsonReturnData == "0" ) {
                        toggleDiv('sendPasswordDIV','hide');
                        toggleDiv('passwordSent','show');
                     }
                     else {
                        writeIntoDiv('pwdSentError','<b><font color="red">ERROR SENDING PASSWORD</font></b>');
                        toggleDiv('passwordSent','show');
                     }
                  }
               }
            }
         }
      }
   }
   oXmlHttp.send(null);
}

function sendPassword() {
}

function checkUID_SendPwd(whichFctn) {
   writeIntoDiv("userMessage","");
   var jsonReturnData = "";
   if (!oXmlHttp )
      oXmlHttp = zXmlHttp.createRequest();
   else if (oXmlHttp.readyState != 0 )
      oXmlHttp.abort();

   if ( whichFctn == checkPassword ) {
      document.webSolForm.validUidPwd.value = "1";
      var sUrl                              = "ajaxDBFunctions.html"+
                                                 "?the_function=validLogin"+
                                                 "&logonID="+document.webSolForm.logonID.value+
                                                 "&pswrd="+document.webSolForm.pswrd.value;
   }
   if ( whichFctn == emailPassword ) {
      document.webSolForm.pswrd.value = "";
      var sUrl = "ajaxDBFunctions.html"+
                     "?the_function=sendPassword"+
                     "&uidEmail="+document.webSolForm.logonID.value;
   }
   oXmlHttp.open("get", sUrl, true);
   oXmlHttp.onreadystatechange =
   function () {
      if ( oXmlHttp.readyState == 4 ) {
	       dummy1 = 1;
         if ( oXmlHttp.status == 200 ) {
	          dummy1 = 2;
            jsonReturnData = oXmlHttp.responseText;
            if ( whichFctn == checkPassword ) {
               if ( jsonReturnData == badUID_PWD )
                  writeIntoDiv('userMessage','<b><font size="1"><font color="red">UID / Password Error... Please Try again...</font> or if you have forgotten your password, Enter your User ID or Email Address in the User ID field above and click <a href="javascript:void(0);" onClick="doLogin(\'sendPWD\');">here</a></b></font>');
               else {
	          document.webSolForm.action = "https://basec.sicomm.net/vendorlogin/pe_system_login_handler.html";
	          document.webSolForm.submit();
	       }
            }
            if ( whichFctn == emailPassword ) {
               if ( jsonReturnData == passwordSENT )
                  writeIntoDiv('userMessage','<b><font size="1"><font color="red">AT YOUR REQUEST, YOUR PASSWORD HAS BEEN SENT...</font><br>Please check your email and Login again...</font></b>');
               else {
                  switch ( jsonReturnData ) {
                     case badUSER_ID:
                          writeIntoDiv('userMessage','<b><font size="1"><font color="red">The user-id (</font> '+document.webSolForm.logonID.value+' <font color="red">) as entered is not found! Please try again.<br>Enter your User ID or Email Address in the User ID field above and click <a href="javascript:void(0);" onClick="doLogin(\'sendPWD\');">here</a></font></b>');
                          break;
                     case badEMAIL_ADDRESS:
                          writeIntoDiv('userMessage','<b><font size="1"><font color="red">The email address (</font> '+document.webSolForm.logonID.value+' <font color="red">) as entered is not found! Please try again.<br>Enter your User ID or Email Address in the User ID field above and click <a href="javascript:void(0);" onClick="doLogin(\'sendPWD\');">here</a></font></b>');
                          break;
                     case emailSendERROR:
                          writeIntoDiv('userMessage','<b><font size="1"><font color="red">SENDING OF EMAIL FAILED! Please try again.</font></b>');
                          break;
                     default: break;
                  }
               }
            }
            handleSubmission();
         }
      }
   }
   oXmlHttp.send(null);
}

function setStatus(MSG) {
	window.status = MSG;
}

function buildCLOCK() {
	var dateVAR=new Date();
	secondsVAR=dateVAR.getSeconds();
	hoursVAR=dateVAR.getHours();
	minutesVAR=dateVAR.getMinutes();
	var noonVAR;
	if(ClockMode=="shownoon"){
		if((hoursVAR>=12)&&(hoursVAR<24)){
			noonVAR="p.m.";
			hoursVAR-=12;
		} else {
			noonVAR="a.m.";
		}
	} else {
		noonVAR="";
	}
	if (minutesVAR<=9) {
		minutesVAR = "0"+minutesVAR;
	}
	if (secondsVAR<=9) {
		secondsVAR = "0"+secondsVAR;
	}
	if (hoursVAR<=9) {
		hoursVAR = "0"+hoursVAR;
	}
	window.status="IPRO | " +hoursVAR+":"+minutesVAR+":"+secondsVAR+" "+noonVAR;
}

function loginTestOnLoad(doneFirstPass) {
   if ( !doneFirstPass ) {
      if ( document.logon.logonID.value != "" )
         checkUID_PWD_SNDPWD(0);
   }
   else {
      if ( document.logon.pswrd.value != "" )
         checkUID_PWD_SNDPWD(1);
      onLoadCheck = true;
   }
}

function loginTest() {
   validUID = document.logon.validUID.value;
   validPWD = document.logon.validPWD.value;
   if ( validUID == "0" ) {
      writeIntoDiv('userID','<b><font color="red"><-- Bad</font></b>');
      alert("The User Id Entered is in error.\nPlease re-enter.");
      document.logon.logonID.focus();
      return false;
   }
   if ( validPWD == "0" ) {
      writeIntoDiv('userPWD','<b><font color="red"><-- Bad</font></b>');
      alert("The Password Entered is in error.\nPlease re-enter.");
      document.logon.pswrd.focus();
      return false;
   }

   pswrd    = document.logon.pswrd.value;
   logonID  = document.logon.logonID.value;
   if ( ( (logonID != "") && (logonID != null))  && ((pswrd != "") && (pswrd != null)) ) {
      passwordStatus = isValidNewPASSWORD( pswrd,1 );
      if ( passwordStatus == "" ) {
				 return true;
			}
			else {
         trys--;
         alert("Enter Your Login ID and Password and click the login button again.\nYou have "+trys+" try(s) remaining...\n"+passwordStatus);
				 return false;
      }
   }
   else {
      if ( logonID == "" || logonID == null ) {
         var andPassword = "";
         if ( pswrd == "" || pswrd == null )
            andPassword = " and Password\n";
         alert("Enter Your Login ID"+andPassword+" then click the login button again...");
         return false;
      }
      if ( pswrd == "" || pswrd == null ) {
         alert("Enter Your Password and click the login button again...");
         return false;
      }
      trys--;
   }
   if ( trys < 1 ) {
      window.top.location.href="http://www.sicomm.net";
      alert("Maximum 3 trys surpassed. Aborting IPRO login...");
      return false;
   }
}

function isValidNewPASSWORD( str,pswrdType ) {
   var errorText     = "";
   var pswrdTypeText = "";
   if (pswrdType == 2)
      pswrdTypeText = "NEW ";
   str += "";	// convert to a string for performing string comparisons.
   if (str.length < 7)
      errorText = errorText+pswrdTypeText+"PASSWORDS MUST be at least 7 characters in length.\n";
   var numCapitals = 0;
   var numNumerics = 0;
   for (i = 0; i < str.length; i++) {
       if ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))
       	numCapitals++;
       if ((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))
       	numNumerics++;
   }
   if (numCapitals < 2)
      errorText = errorText+pswrdTypeText+"PASSWORDS MUST contain at least 2 CAPITAL LETTERS.\n";
   if (numNumerics < 2)
      errorText = errorText+pswrdTypeText+"PASSWORDS MUST contain at least 2 NUMBERS.\n";
   return(errorText);
}

function initDivs() {
  toggleDiv('load','show');
  for(kk=0;kk<=divs.length-1;kk++) {
    toggleDiv(divs[kk],'hide');
  }
}

function nav_wb(target) {
  var tD = document.getElementById('targetDiv');
	var tA = document.getElementById('targetAjax');
	var nB = document.getElementById('nB');
  if(target != tD.value) {
    switch(target) {
      case 'home' : {
        initDivs();
				toggleDiv('staticDiv','show');
        tD.value = "home";
				tA.value = "Welcome to IPRO, the State of Idaho's Electronic Procurement System.";
				nB.innerHTML = "<span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('solicitations');\">Show Bid Opportunities</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('registration');\">IPRO Vendor Registration</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('purchasing');\">Purchasing Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('idaho');\">Idaho.gov</a></span>\n";
        break;
      }
      case 'solicitations' : {
        initDivs();
				toggleDiv('staticDiv','show');
        tD.value = "solicitations";
				tA.value = "IPRO Bid Oportunities...";
				nB.innerHTML = "<active><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('home');\">Hide Bid Opportunities</a></active><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('registration');\">IPRO Vendor Registration</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('purchasing');\">Purchasing Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('idaho');\">Idaho.gov</a></span>\n";
        break;
      }
      case 'registration' : {
         initDivs();
				toggleDiv('staticDiv','hide');
        tD.value = "registration";
				tA.value = "IPRO Vendor Registration";
				nB.innerHTML = "<span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('home');\">Show Bid Opportunities</a></span><active><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('registration');\">IPRO Vendor Registration</a></active><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('purchasing');\">Purchasing Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav_wb('idaho');\">Idaho.gov</a></span>\n";
				break;
      }
      case 'idaho' : {
        window.open('http://www.accessidaho.org/');
        break;
      }
			case 'purchasing' : {
        window.open('http://adm.idaho.gov/purchasing/');
        break;
      }
      default : {
      }
    }
    TO = setInterval(clearLoad,500,target);
  }
}

function nav(target) {
  var tD = document.getElementById('targetDiv');
	var tA = document.getElementById('targetAjax');
	var nB = document.getElementById('nB');
  if(target != tD.value) {
    switch(target) {
      case 'home' : {
        initDivs();
				toggleDiv('staticDiv','show');
        tD.value = "home";
				tA.value = "Welcome to IPRO, the State of Idaho's Electronic Procurement System.";
				nB.innerHTML = "<active><a href=\"javascript:void(0);\" onclick=\"javascript:nav('home');\">IPRO Home</a></active><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('solicitations');\">Bid Opportunities</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('registration');\">IPRO Vendor Registration</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('purchasing');\">Purchasing Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('idaho');\">Idaho.gov</a></span>\n";
        break;
      }
      case 'solicitations' : {
        initDivs();
				toggleDiv('staticDiv','show');
        tD.value = "solicitations";
				tA.value = "IPRO Bid Oportunities...";
				nB.innerHTML = "<span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('home');\">IPRO Home</a></span><active><a href=\"javascript:void(0);\" onclick=\"javascript:nav('solicitations');\">Bid Opportunities</a></active><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('registration');\">IPRO Vendor Registration</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('purchasing');\">Purchasing Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('idaho');\">Idaho.gov</a></span>\n";
        break;
      }

      case 'registration' : {
        initDivs();
				toggleDiv('staticDiv','hide');
        tD.value = "registration";
				tA.value = "IPRO Vendor Registration";
				nB.innerHTML = "<span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('home');\">IPRO Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('solicitations');\">Bid Opportunities</a></span><active><a href=\"javascript:void(0);\" onclick=\"javascript:nav('registration');\">IPRO Vendor Registration</a></active><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('purchasing');\">Purchasing Home</a></span><span><a href=\"javascript:void(0);\" onclick=\"javascript:nav('idaho');\">Idaho.gov</a></span>\n";
				break;
      }
      case 'idaho' : {
        window.open('http://www.accessidaho.org/');
        break;
      }
			case 'purchasing' : {
        window.open('http://adm.idaho.gov/purchasing/');
        break;
      }
      default : {
      }
    }
    TO = setInterval(clearLoad,500,target);
  }
}

function clearLoad(target) {
  clearInterval(TO);
  toggleDiv('load','hide');
  toggleDiv(document.getElementById('targetDiv').value+'Div','show');
  toggleDiv('footer','show');
	document.getElementById('page_headline').innerHTML = document.getElementById('targetAjax').value;
}

function writeIntoDiv(divID, textIN) {
   if (document.layers) {
      document.layers[divID].document.open();
      document.layers[divID].document.write(textIN);
      document.layers[divID].document.close();
   }
   else if (document.getElementById)
      document.getElementById(divID).innerHTML = textIN;
   else if (document.all)
      document.all[divID].innerHTML = textIN;
}

