﻿///------------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////
//General Functions
///////////////////////////////////////////////////////////////////

//------------------------------------------------------------------------------------------
var singQuot = /\'/g;
var dubQuot = /\"/g;
var	Newline= "\n";
var	Tab= "\t";
var	Backslash= "\\";

function CheckAlphaNumeric(oFld, objFeedBack, msg) {
    var obj = document.getElementById(objFeedBack);
    var objExist = true;
    if (obj == null || typeof (obj) == "undefined")
        objExist = false;
        
    if(objExist)
        document.getElementById(objFeedBack).innerHTML = "";
        
	var data = oFld.value;
	var valid = "@()[]{}*@#$^?!'-.,אבגדהוזחטיכלמםנןסעפףצץקרשת abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + + singQuot + dubQuot + Newline + Tab + Backslash;
	var retVal = true;

	if (valid.indexOf(data.substr(data.length - 1)) == -1) {

	    if (objExist) {
	        //alert(document.getElementById(objFeedBack).id + " " + msg + "...");
	        document.getElementById(objFeedBack).innerHTML = msg; //"only alpha numeric chars allowed";
	    }
	    else
	        alert(msg); //"only alpha numeric chars allowed";
	    oFld.value = "";
        oFld.focus();
        oFld.select();
        retVal = false;
  }
  return retVal;
}
//------------------------------------------------------------------------------------------
function CheckAlpha(oFld, objFeedBack,msg) {
    var obj = document.getElementById(objFeedBack);
    var objExist = true;
    if (obj == null || typeof (obj) == "undefined")
        objExist = false;

    if (objExist)
        document.getElementById(objFeedBack).innerHTML = "";
    var data = oFld.value;
    var valid = "@()[]{}*@#$^?!'-.,אבגדהוזחטיכלמםנןסעפףצץקרשת abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + +singQuot + dubQuot + Newline + Tab + Backslash;
    var retVal = true;

    if (valid.indexOf(data.substr(data.length - 1)) == -1) {
        if (objExist) {
            //alert(document.getElementById(objFeedBack).id + " " + msg + "...");
            document.getElementById(objFeedBack).innerHTML = msg; //"only alpha chars allowed";
        }
        else
            alert(msg); //"only alpha chars allowed");
        oFld.focus();
        oFld.select();
        oFld.value = "";
        retVal = false;
    }
    return retVal;
}
//------------------------------------------------------------------------------------------
function CheckNum(oFld, objFeedBack, msg) {
    var obj = document.getElementById(objFeedBack);
    var objExist = true;

    if (obj == null || typeof (obj) == "undefined")
        objExist = false;

    if (objExist)
        document.getElementById(objFeedBack).innerHTML = "";
        
    var data; 
    data = oFld.value;//Object
    if(typeof(data)=='undefined')
		data=oFld;//Value
		
    var retVal = true;
    var validNum = "0123456789.";

    if (validNum.indexOf(data.substr(data.length - 1)) == -1) {
      if (objExist) {
            //alert(document.getElementById(objFeedBack).id + " " + msg + "...");
            document.getElementById(objFeedBack).innerHTML = msg;//"only Numeric alowed";
      }
      else
          alert(msg);//"only Numeric alowed");

      try {
        oFld.value = "";
		oFld.focus();
		oFld.select();
	  }
	  catch(e){}
      retVal = false;
  }

  return retVal;
}


function FormatTime() {

    var digital = new Date();
    var sFormtTime;


    var Year1 = digital.getFullYear();
    var Month1 = digital.getMonth();
    Month1 = Month1 + 1;
    //alert('Month1 ' + Month1);
    var Day1 = digital.getDate();
    //alert('Day1 ' + Day1);

    var hours = digital.getHours();
    var minutes = digital.getMinutes();
    var seconds = digital.getSeconds();



    if (Day1 <= 9) {
        Day1 = '0' + Day1;
    }

    if (Month1 <= 9) {
        Month1 = '0' + Month1;
    }

    if (hours <= 9) {
        hours = '0' + hours;
    }

    if (minutes <= 9) {
        minutes = '0' + minutes;
    }


    if (seconds <= 9) {
        seconds = '0' + seconds;
    }


    sFormtTime = Day1 + '/' + Month1 + '/' + Year1 + ' ' + hours + ':' + minutes + ':' + seconds;
    //alert(sFormtTime);
    return sFormtTime;


}

//////////////////////////////////////////////////////////////////////////////////

function IDVerify(sStr) {
    var mulVar
    var mulResult
    var sum = 0

    // if even - start with 2 , if parity - start with 1
    mulVar = (sStr.length + 1) % 2 + 1

    for (i = 0; i < sStr.length - 1; i++) {
        mulResult = eval(sStr.substring(i, i + 1)) * mulVar
        if (mulResult > 9)
            mulResult -= 9
        sum = sum + mulResult
        if (mulVar == 1)
            mulVar = 2
        else
            mulVar = 1
    }

    if ((sum + eval(sStr.substring(i, i + 1))) % 10 == 0)
        return true
    else
        return false
}

////////////////////////////////////////////////////////////
/////////////////////////
////////Email Validation
////////////////////////
function emailCheck(emailStr, objFeedBack) {
    
    //alert(emailStr + "," + objFeedBack);
    var obj = document.getElementById(objFeedBack);
    var objExist = true;

    if (obj == null || typeof (obj) == "undefined")
        objExist = false;

    if (objExist)
        document.getElementById(objFeedBack).innerHTML = "";

    /*  1 means check it, 0 means don't. */
    var checkTLD = 1;
    /* The following is the list of known TLDs that an e-mail address must end with. */
    var knownDomsPat = /^(com|net|org|edu|mil|gov|arpa|biz|aero|name|coop|info|pro|co|il|ac|museum)$/;
    /* The following pattern is used to check if the entered e-mail address
    fits the user@domain format.  It also is used to separate the username
    from the domain. */
    var emailPat = /^(.+)@(.+)$/;
    /* The following string represents the pattern for matching all special
    characters.  We don't want to allow special characters in the address. 
    These characters include ( ) < > @ , ; : \ " . [ ] */
    var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    /* The following string represents the range of characters allowed in a 
    username or domainname.  It really states which chars aren't allowed.*/
    var validChars = "\[^\\s" + specialChars + "\]";
    /* The following pattern applies if the "user" is a quoted string (in
    which case, there are no rules about which characters are allowed
    and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
    is a legal e-mail address. */
    var quotedUser = "(\"[^\"]*\")";
    /* The following pattern applies for domains that are IP addresses,
    rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
    e-mail address. NOTE: The square brackets are required. */
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    /* The following string represents an atom (basically a series of non-special characters.) */
    var atom = validChars + '+';
    /* The following string represents one word in the typical username.
    For example, in john.doe@somewhere.com, john and doe are words.
    Basically, a word is either an atom or quoted String. */
    var word = "(" + atom + "|" + quotedUser + ")";
    // The following pattern describes the structure of the user
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
    /* The following pattern describes the structure of a normal symbolic
    domain, as opposed to ipDomainPat, shown above. */
    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
    /* Finally, let's start trying to figure out if the supplied address is valid. */

    /* Begin with the coarse pattern to simply break up user@domain into
    different pieces that are easy to analyze. */
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
        /* Too many/few @'s or something; basically, this address doesn't
        even fit the general mould of a valid e-mail address. */

        //alert("Email address seems incorrect (check @ and .'s)");
        EmailNotValidMsg(objFeedBack);
        return false;
    }
    var user = matchArray[1];
    var domain = matchArray[2];

    // Start by checking that only basic ASCII characters are in the strings (0-127).

    for (i = 0; i < user.length; i++) {
        if (user.charCodeAt(i) > 127) {
            //alert("Ths username contains invalid characters.");
            EmailNotValidMsg(objFeedBack);
            return false;
        }
    }
    for (i = 0; i < domain.length; i++) {
        if (domain.charCodeAt(i) > 127) {
            //alert("Ths domain name contains invalid characters.");
            EmailNotValidMsg(objFeedBack);
            return false;
        }
    }
    // See if "user" is valid 
    if (user.match(userPat) == null) {
        // user is not valid
        //alert("The username doesn't seem to be valid.");
        EmailNotValidMsg(objFeedBack);
        return false;
    }

    /* if the e-mail address is at an IP address (as opposed to a symbolic
    host name) make sure the IP address is valid. */
    var IPArray = domain.match(ipDomainPat);
    if (IPArray != null) {
        // this is an IP address
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                //alert("Destination IP address is invalid!");
                EmailNotValidMsg(objFeedBack);
                return false;
            }
        }
        return true;
    }
    // Domain is symbolic name.  Check if it's valid.
    var atomPat = new RegExp("^" + atom + "$");
    var domArr = domain.split(".");
    var len = domArr.length;
    for (i = 0; i < len; i++) {
        if (domArr[i].search(atomPat) == -1) {
            //alert("The domain name does not seem to be valid.");
            EmailNotValidMsg(objFeedBack);
            return false;
        }
    }
    /* domain name seems valid, but now make sure that it ends in a
    known top-level domain (like com, edu, gov) or a two-letter word,
    representing country (uk, nl), and that there's a hostname preceding 
    the domain or country. */
    if (checkTLD && domArr[domArr.length - 1].length != 2 &&
		domArr[domArr.length - 1].search(knownDomsPat) == -1) {
        //alert("The address must end in a well-known domain or two letter " + "country.");
        EmailNotValidMsg(objFeedBack);
        return false;
    }
    // Make sure there's a host name preceding the domain.
    if (len < 2) {
        //alert("This address is missing a hostname!");
        EmailNotValidMsg(objFeedBack);
        return false;
    }
    // If we've gotten this far, everything's valid!
    //alert('email valid');
    return true;
}

function EmailNotValidMsg(objFeedBack) {
    //alert('EmailNotValidMsg');
    var obj = document.getElementById(objFeedBack);
    var objExist = true;
    

    if (obj == null || typeof (obj) == "undefined")
        objExist = false;

    if (objExist)
        document.getElementById(objFeedBack).innerHTML = "email is not valid, ";
    else
        alert("email is not valid");
}
