/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



window.onload = initForm;
window.onunload = function() {};

function initForm() {
    document.getElementById("payMode"). selectedIndex = 0;
    document.getElementById("payMode"). onchange = showDiv;
    var telDiv = document.getElementById ("tellerDiv").style.visibility = "hidden";;
    var onlDiv = document.getElementById ("onlineDiv").style.visibility = "hidden";;	
}

function showDiv() {
    var newMode = document.getElementById ("payMode");
    var selMode = newMode.options [newMode.selectedIndex].value;

    var telDiv = document.getElementById ("tellerDiv");
    var onlDiv = document.getElementById ("onlineDiv");	
    if (selMode == "choose") {
        //alert("chose");
        onlDiv.style.visibility = "hidden";
        telDiv.style.visibility = "hidden";

    }else if (selMode == "teller") {
        onlDiv.style.visibility = "hidden";
        telDiv.style.visibility = "visible";
    }else {
        //alert("div2");
        telDiv.style.visibility = "hidden";
        onlDiv.style.visibility = "visible";
    }
}


String.prototype.trim = function() { 
    sInString = this.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
        
};
function processTeller( fm ){
        
    if ( ChkFields( fm ) ) {
        if(checkTellerFields(fm)){
            document.getElementById('errMess').innerHTML = "";
            showLoader(fm);
        }else return false;
    }else return false;


    return false;
}
function processOnline( fm ){
        
    if ( ChkFields( fm ) ) {
        //alert("Coming very soon!");
        document.getElementById('errMess').innerHTML = "";
    this.disabled=true;
    var el = document.getElementById('onlineDiv');
        
    el.innerHTML = "<div style='text-align: center;'><b>Processing...</b><br/><img src='resources/loading.gif' border='0' /></div>";
        
    validateNewOnlineMember(fm.empId.value,fm.lastName.value,fm.otherNames.value,
    fm.email.value,fm.phone.value,fm.location.value,fm.savings.value);
        
        
    }
}
function checkTellerFields(fm){
    var sMsg = '';
        
    if (fm.tellerBank.value.length == 0) {
        sMsg = "Please enter the name of the bank you made the payment.";
        fm.tellerBank.focus();
    }
    
    if (!IsNumeric(fm.tellerAmount.value)) {
        sMsg = "Invalid amount in teller.<br><em>Please remove all commas and alphabets<br>Just digits and decimal point</em>";
        fm.tellerAmount.focus();
    }
        
    if (fm.tellerAmount.value.length == 0) {
        sMsg = "Please enter amount you paid.";
        fm.tellerAmount.focus();
    }
    if (fm.tellerNum.value.length == 0) {
        sMsg = "Please enter the teller number.";
        fm.tellerNum.focus();
    }
        
        
    if (sMsg.length > 0) {
        //alert( sMsg );
        document.getElementById('errMess').innerHTML = "<font color='#FF0000'>"+sMsg+"</font>";
        return false;
    }
        
    return true;
}
function ChkFields( fm ) {
        
    var sMsg = '';
        
    if (!IsNumeric(fm.savings.value)) {
        sMsg = "Invalid monthly savings.<br><em>Please remove all commas and alphabets<br>Just digits and decimal point</em>";
        fm.savings.focus();
    }
    
    if (fm.savings.value.length == 0) {
        sMsg = "Please enter your monthly savings.";
        fm.savings.focus();
    }
    if (fm.location.value.length == 0) {
        sMsg = "Please enter your location.";
        fm.location.focus();
    }
        
    if (fm.phone.value.length == 0) {
        sMsg = "Please enter your phone number.";
        fm.phone.focus();
    }
        
    if (!isValidEmail(fm.email.value)) {
        sMsg = "Invalid email.";
        fm.email.focus();
    } 
    
    if (fm.email.value.length == 0) {
        sMsg = "Please enter a value for email.<br/> We will send your login details to this email address.";
        fm.email.focus();
    }
    if (fm.lastName.value.length == 0) {
        sMsg = "Please enter a your last name.";
        fm.lastName.focus();
    }
        
    if (fm.otherNames.value.length == 0) {
        sMsg = "Please enter your first name.";
        fm.otherNames.focus();
    }

    if (!IsNumeric(fm.empId.value)) {
        sMsg = "Invalid employee id!";
        fm.empId.focus();
    }

    if (fm.empId.value.length != 12) {
        sMsg = "Invalid employee id!";
        fm.empId.focus();
    }

    if (fm.empId.value.length == 0) {
        sMsg = "Please enter your employee id.";
        fm.empId.focus();
    }
        
    if (sMsg.length > 0) {
        //alert( sMsg );
        document.getElementById('errMess').innerHTML = "<font color='#FF0000'>"+sMsg+"</font>";
        return false;
    }
        
    return true;
}
    
function showLoader(fm){
    document.getElementById('errMess').innerHTML = "";
    this.disabled=true;
    var el = document.getElementById('tellerButtons');
        
    el.innerHTML = "<div style='text-align: center;'><b>Processing...</b><br/><img src='resources/loading.gif' border='0' /></div>";
        
    submitNewMember(fm.empId.value,fm.lastName.value,fm.otherNames.value,
    fm.email.value,fm.phone.value,fm.location.value,fm.savings.value,
    fm.tellerNum.value,fm.tellerAmount.value,fm.tellerBank.value);
        
    return false;
}
    
function sendreply(type, error){
    //		alert("I got here...");
    var fm = document.getElementById("myForm");
    var el = document.getElementById('tellerButtons');
       
    if(type == 1){
        document.getElementById('errMess').innerHTML = "<font color='#FF0000'>"+error+"</font>";
        el.innerHTML = "<label class=''></label>"+
            "<input value='Submit' name='submit' id='submit' class='inputsubmit'"+ 
            "type='button' onclick='javascript:processTeller(this.form);'>";
    }else if(type == 2){
        document.location = "message.jsp?type=newMember&empId="+fm.empId.value;
    }

        
}

function submitNewMember(empID, lName, fName, email, phone, location, savingsAmount, tellerNum, amount, bank)  {
    
    var xmlHttp;
    
    try    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    
    xmlHttp.onreadystatechange=function() {
        if(xmlHttp.readyState==4)
        {
            var result = xmlHttp.responseText;
            result = result.trim();
            if(result == "success"){
                setTimeout("sendreply('2', 'success')", 3000);
            }
                
            else 
                setTimeout("sendreply('1', '"+result+"')", 3000);
                
        }	
    }
    xmlHttp.open('POST','loginUtil.jsp?action=addNewMember&empId='+empID+'&lastName='+lName
        +'&otherNames='+fName+'&email='+email+'&phone='+phone+'&location='+location+'&savings='+savingsAmount
        +'&tellerNum='+tellerNum+'&tellerAmount='+amount+'&tellerBank='+bank,true);
    xmlHttp.send(null);
}

// online stuffs
function sendOnlineReply(type, error){
    //		alert("I got here...");
    var fm = document.getElementById("myForm");
    var el = document.getElementById('onlineDiv');
       
    if(type == 1){
        document.getElementById('errMess').innerHTML = "<font color='#FF0000'>"+error+"</font>";
        el.innerHTML = " <input class='inputsubmit' value='Start online transaction' name='Button' type='button' onclick='processOnline(this.form)'/>";
    }else if(type == 2){
        newWindow = window.open("eTranzact/payRegistration.jsp","","status=yes, width=500,height=450");
        el.innerHTML = " <input class='inputsubmit' value='Start online transaction' name='Button' type='button' onclick='processOnline(this.form)'/>";
    }
    
   // newWindow = window.open("eTranzact/payRegistration.jsp","","status=yes, width=500,height=450");

        
}

function validateNewOnlineMember(empID, lName, fName, email, phone, location, savingsAmount)  {
    
    var xmlHttp;
    
    try    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    
    xmlHttp.onreadystatechange=function() {
        if(xmlHttp.readyState==4)
        {
            var result = xmlHttp.responseText;
            result = result.trim();
            if(result == "success"){
                setTimeout("sendOnlineReply('2', 'success')", 3000);
            }
                
            else 
                setTimeout("sendOnlineReply('1', '"+result+"')", 3000);
                
        }	
    }
    xmlHttp.open('POST','loginUtil.jsp?action=validateNewOnlineMember&empId='+empID+'&lastName='+lName
        +'&otherNames='+fName+'&email='+email+'&phone='+phone+'&location='+location+'&savings='+savingsAmount
        ,true);
    xmlHttp.send(null);
}


function isValidEmail(str) {

   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }