/*
 * JavaScript Functions for Web Application Client ( app.js)
 *
 * (c) Copyright Zaidsoft. All Rights Reserved.
 * 
 * This software is the proprietary information of Zaidsoft. 
 * 
 * No part of this software can be reused without explicit written 
 * permission of  Zaidsoft. (www.zaidsoft.com)
 *
 * All Use(s) is(are) subject to license terms.
 *  
 */

	///////////////////////////////////////////////////
	// The function validates the date given to it 
	//	returns False if The date is Not Valid    

        //
        // 
        

	// VALIDATION - REQUIRED
	// the filed can not be left blank/null/empty
	function nf_checkValidation_Required(frm, fld, caption){
		  var f = document.forms[frm];
		  var val = trimString(f.elements[fld].value);   
                  var tagName = f.elements[fld].tagName;
                  var ifBlank = (val == null || val == '') ;
                  if (tagName.toLowerCase() == "select") {
                        ifBlank = (val == null || val == '' || val == "0");
                  }
		  if (ifBlank){
		  		alert(caption + " is Required.");
				f.elements[fld].focus();
				return false;
		  }
                  return true;
	}

	// VALIDATION - HUMAN NAME
	// should only contains letters and may be a .
	// should not contain digits or special characters
	function nf_checkValidation_hname(frm, fld, caption, required){
		  var f = document.forms[frm];
		  var val = trimString(f.elements[fld].value);      
		  if (required){
                            if (!nf_checkValidation_Required(frm, fld, caption)) return false;
		  }
		  if (val != null){    
	          var s = val;
       	          for (i=0; i < s.length; i++){
        	        if ( !isAlphabet(s.charAt(i))) {
						alert(caption + " value is not valid");
                                                f.elements[fld].focus();
						return false;
					}
           		}
	           return true;
		  }
	}
	
	// VALIDATION - STRING OF DIGITS
        // 0 to 9 only. No dot or dash or other char allowed
	// should only contains letters and may be a .
	// should not contain digits or special characters
	function nf_checkValidation_digit(frm, fld, caption, required){
		  var f = document.forms[frm];
		  var val = trimString(f.elements[fld].value);     
		  if (required){
                            if (!nf_checkValidation_Required(frm, fld, caption)) return false;
		  }
		  if (val != null){    
	           var s = val;
                   for (i=0; i < s.length; i++){
        	        if ( !isDigit(s.charAt(i))) {
						alert(caption + " value is not valid");
                                                f.elements[fld].focus();
						return false;
					}
           		}
	           return true;
		  }
	}

	// VALIDATION - E-MAIL
        // check to make sure it is valid email address
	function nf_checkValidation_email(frm, fld, caption, required){
		  var f = document.forms[frm];
		  var val = trimString(f.elements[fld].value);     
		  if (required){
                            if (!nf_checkValidation_Required(frm, fld, caption)) return false;
		  }
                  // write code for email validation
	}
	
	// VALIDATION - CREDIT CARD NUMBER
        // check to make sure it is valid email address
	function nf_checkValidation_creditcard(frm, fld, caption, required){
		  var f = document.forms[frm];
		  var val = trimString(f.elements[fld].value);     
		  if (required){
                            if (!nf_checkValidation_Required(frm, fld, caption)) return false;
		  }
                  // write code for credit card validation
	}

	// VALIDATION - DATE
        // check to make sure it is valid email address
	function nf_checkValidation_date(frm, fld, caption, required){
		  var f = document.forms[frm];
		  var val = trimString(f.elements[fld].value);     
		  if (required){
                            if (!nf_checkValidation_Required(frm, fld, caption)) return false;
		  }
                  // TODO: write code for date verification
                  return true; 
	}
	
	function isValidDate(dt, mn, yr){
	
			// If user has not entered date, it's invalid
			if ( dt == 0 || mn == 0 || yr == "0000" ){
				return false;
			}
			// else ..
			var yr_1 = yr/4;
			var d_max = 28;
			m31= new Array (1, 3, 5, 7, 8, 10, 12);
			var flag_m=0;
			var i=0;
			
			// If the person is born in February check 
			if ( mn == 2)
			{
				if ( yr_1 == Math.round(yr_1))    d_max=29;
								
			}
			else
			{
				// Check to see if the month is of 31 days 
				for ( i =0; i<7; i++) {
				if (mn == m31[i]) flag_m = 1;
				}
				if ( flag_m ) {d_max=31 } else {d_max=30}
			}
			if ( dt > d_max ) return false;	
			return true;
			
	}



        // verify that the given input contains valid number only
        // i.e., It can't have chars other than '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-'
        function isValidNumber(num){
           var s = num + " ";
           for (i=0; i < num.length; i++){
                if ( !isValidNumChar(s.charAt(i))) return false;
           }
           return true;
        }

 
        //
        function isValidNumChar(ch){
            return ( ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch== '9' || ch == '.');
        }
        //
        function isDigit(ch){
            return ( ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch== '9');
        }
        function isAlphabet(ch){
            return ( ch > 'A' || ch < 'z');
        }

        //////////////////////////////////////
        // Verify that the given string makes up a http URL
        // if 'http://' is not given it will add this to make it a valid url

        function httpURLify(s){
            //check the string to see if it srarts with http:// if not ad it
            // if user wants to edit arb URL but has not given the url, Warn.
            if ( s == "" ){
                alert("Please enter a valid web address(url)");
	        return false;
            }
            else {
                // it should start with http:// else ad this at the begining.
                if ( s.indexOf("http://") != 0 ) s = "http://" + s;

	        // now check if url is of the form http://a.b.somthing.tld if y add a '/'
                if( s.substring(7).indexOf("/") == -1 ) s = s + "/"; 
                // return this string now
                return s;
            }	
        }

