//
// This script validates fields input from a form by checking
//
// 1) if the field is empty or not
// 2) if the field matches a given regular expression
// 3) if the field passes any other validation you wish to perform
//    in an arbitary javascript function
//
// To get a field to be validated, you need to call the add_rule
// function (see below) with appropriate arguments. You can also
// use the utility functions below for common validation functions
// e.g. add_date("field", "Your Field", 1);
//
// You also need to add the following to any submit buttons:
//
//    onClick="return validate(this.form)"

rules = new Array();
rulecount = 0;
extras = new Array();
extracount = 0;
errors = new Array();
errorcount = 1;

// Function add_rule(name, heading, mandatory, pattern, snippet)
//
// name      - name of the input field to validate
// heading   - what to call the field for the user's benefit
// mandatory - 0,"0",false or null object means the field can be blank
// pattern   - a regular expression which any value entered into the
//             HTML form must match against for validation to succeed.
//             We need to escape special characters with '\', including '\'
//             itself (e.g. use \\s in your string if you want to match a
//             whitespace char - we want the expression to contain the 2
//             characters '\s', not s. If you put brackets into your
//             expression then JavaScript will allow you to access them
//             through the global (to each window) variable RegExp as
//             RegExp.$1 ... RegExp.$9 in any javascript (for instance,
//             the next argument :-)
// snippet   - the body of JavaScript function. You can use this to perform
//             more general validation tests than just matching a regular
//             expression. It has access to one formal paramater, 'value' -
//             the value of the input field being validated. It also has
//             access to global variables such as RegExp.$1. It should
//             return nothing (or an empty string) if validation succeeds,
//             otherwise it should return a string to be shown to the user
//             explaining the problem.

function add_rule(name, heading, mandatory, pattern, snippet) {
  rules[rulecount]           = new Object();
  rules[rulecount].name      = name;
  rules[rulecount].heading   = heading;
  rules[rulecount].pattern   = pattern;
  rules[rulecount].mandatory = mandatory;
  rules[rulecount].snippet   = snippet;
  rulecount++;
}

function remove_rule(name) {
  
  for(i=0;i<rulecount;i++) {
		if(rules[i].name == name)
		{
		  	rules[i].name      = name;
			rules[i].heading   = "";
			rules[i].pattern   = "";
			rules[i].mandatory = 0;
			rules[i].snippet   = "";
		}
    
	}
}


function add_extra(expression) {
  extras[extracount++] = expression;
}



// validate
//
// This takes a form object as input and checks each rule added using
// add_rule. If there are any problems with validation, an alert box
// is popped up and the form is not submitted. If there are problems
// and allowSubmit is "yes" then the user will be presented with a
// confirm dialog box (with "OK" and "Cancel" options) which will
// submit the form anyway if OK is clicked.

function validate (form, allowSubmit) {
  if (window.RegExp) {

    var display = "";
    var name,heading,snippet,mandatory,value,re,func_result;

    // Loop through our rules, doing the appropriate checks
    for(i=0;i<rulecount;i++) {
      name       = rules[i].name;
      heading    = rules[i].heading;
      snippet    = rules[i].snippet;
      pattern    = rules[i].pattern;
      mandatory  = Boolean(rules[i].mandatory);
      valid			 = true;
	  
  
      if (!form[name]) {
            alert(name + " is not defined for this form");
      }
      else {
        // If this is a select box (but not a multiple select)...
        if (form[name].type == "select-one") {
          value = form[name].options[form[name].selectedIndex].value;
        } else {
            value = form[name].value;
        }

        // Check that a mandatory field isn't empty
        if (value.search(".") == -1) {
            if (mandatory == true)
            {
                display += "* "+heading+" must be filled in\n";
                valid		= false;
            }
        }
        // Check if we match the pattern
        else if (value.search(pattern) != -1) {
          if (snippet) {
              // If we have a code snippet, turn it into a function and call it on value
              var func = new Function("value", "form", snippet);
              func_result = func(value, form)
              if (func_result){
                display += "* "+heading+": "+func_result+"\n";
                valid		= false;
              }
          }
        }
        else {        // failed to match pattern
          if (pattern){
            display += "* "+heading+": The value '"+value+"' is not allowed\n";
						valid		= false;
          }
        }
		    if (!valid) {
		    	changeColor('lbl_'+name,'red');
    		}else{
		    	changeColor('lbl_'+name,'black');
    		}
      }
    }       // end of loop through rules
    // Loop through our extras, evaluating each one
    for(i=0;i<extracount;i++) {
      res = eval(extras[i]);
      if (res)
        display += "* "+res+"\n";
    }
    if (display.length != 0) {
      if (allowSubmit == "yes")
          return confirm("This form has not been submitted because there were problems with the following fields:\n\n"+display+"\n\nWould you like to continue anyway?");
      else {
          alert(display+"\n\nPlease try again.");
          return false;
      }
    }
    return true;
  } else {    // no RegExp object - exit
    return true;
  }
}

//
// Some utility functions for most common validations
//

// Integer          - digits with an optional minus at the front
function add_integer(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "^-?\\d*\\s*$");
}

// Number           - an integer with a dot and some digits
function add_number(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "^-?\\d*(\\.\\d*)?\\s*$");
}

// Image           - a image file name
function add_image(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "", "if(value.search('(jpg|gif|png|jpeg|ico|JPG|GIF|PNG|JPEG|ICO)$')==-1) return 'Please choose a valid image (jpg, gif, png, jpeg, ico)';");
}

// Email           - "@" symbol and no spaces - one or more good 
// characters before and after "@" - .xx country code or .xxx (.xxxx) tld to end
function add_email(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "^[a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*(\.[a-zA-Z]{2,4})$");
}


// URL           - Matches complete URL
function add_url(name, heading, mandatory) {
  add_rule(name, heading, mandatory,"^(((ht|f)tp(s?))\:\/\/)(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+(\.[a-zA-Z]{2,6})(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$");
}


// Date             - dd/mm/yyyy
// We need to put '/' chars in a char class (i.e. [/]) to prevent
// IE from complaining that our re is wrong :-(
function add_date(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "(^[0123]?\\d)[/]([01]?\\d)[/]\\d{4}\\s*$",
           "return (RegExp.$1>31 || RegExp.$1==0 || RegExp.$2>12 || RegExp.$2==0)?'The value \"'+value+'\" is not allowed':'';"
          );
}

// DateTime         - dd/mm/yyyy hh:mm
function add_datetime(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "(^\\d{1,2})[/](\\d{1,2})[/]\\d{2,4}\\s*(\\d{1,2}):(\\d{1,2})\\s*$",
           "return (RegExp.$1>31 || RegExp.$1==0 || RegExp.$2>12 || RegExp.$2==0 || RegExp.$3>23 || RegExp.$4>59 )?'The value \"'+value+'\" is not allowed':'';"
          );
}

// Timestamp         - dd/mm/yyyy hh:mm:ss.mmm
function add_timestamp(name, heading, mandatory) {
  add_rule(name, heading, mandatory, "(^\\d{1,2})[/](\\d{1,2})[/]\\d{2,4}\\s*(\\d{1,2}):(\\d{1,2}):(\\d{1,2})\\s*$",
           "return (RegExp.$1>31 || RegExp.$1==0 || RegExp.$2>12 || RegExp.$2==0 || RegExp.$3>23 || RegExp.$4>59 || RegExp.$5>59 )?'The value \"'+value+'\" is not allowed':'';"
          );
}

function changeColor(id,color) {
 
       if (document.getElementById)	document.getElementById(id).style.color = color;
       else if (document.layers)    document[id].color = color;
       else if (document.all)       document.all[id].style.color = color;
}

function disable_fields(thisform, state){
   	if(state=='1')
   	{
     	for(i=0;i<thisform.elements.length;i++)
	    {
	        thisform.elements[i].disabled = true;
	    }
	}
	else
	{
      	for(i=0;i<thisform.elements.length;i++)
	    {
			if(thisform.elements[i].id=='disabled')
			{
				thisform.elements[i].disabled = true;
			}
			else
			{
	        	thisform.elements[i].disabled = false;
			}
	    }
	}
}


function MatchClick() {
  var re = new RegExp(document.demoMatch.regex.value);
  var m = re.exec(document.demoMatch.subject.value);
  if (m == null) {
    alert("No match");
  } else {
    var s = "Match at position " + m.index + ":\n";
    for (i = 0; i < m.length; i++) {
      s = s + m[i] + "\n";
    }
    alert(s);
  }
}

function countdown_clock(year, month, day, hour, minute, format, message, style)
         {
         //I chose a div as the container for the timer, but
         //it can be an input tag inside a form, or anything
         //who's displayed content can be changed through
         //client-side scripting.

	if(style=='' || style==null)
        	 html_code = message+' <span id="countdown" style="text-align:inline;"></span>';
	else
        	 html_code = '<DIV '+style+'>'+message+' <span id="countdown" style="text-align:inline;"></span></DIV>';
         
         document.write(html_code);
         
         countdown(year, month, day, hour, minute, format);                
         }
         
function countdown(year, month, day, hour, minute, format)
         {
         Today = new Date();
         Todays_Year = Today.getFullYear() - 2000;
         Todays_Month = Today.getMonth() + 1;                  
         
         //Convert both today's date and the target date into miliseconds.                           
         Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), 
                                 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();                                 
         Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime();                  
         
         //Find their difference, and convert that into seconds.                  
         Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
         
         if(Time_Left < 0)
            Time_Left = 0;
         
         switch(format)
               {
               case 0:
                    //The simplest way to display the time left.
                    document.getElementById('countdown').innerHTML = Time_Left + ' seconds';
                    break;
               case 1:
                    //More datailed.
                    days = Math.floor(Time_Left / (60 * 60 * 24));
                    Time_Left %= (60 * 60 * 24);
                    hours = Math.floor(Time_Left / (60 * 60));
                    Time_Left %= (60 * 60);
                    minutes = Math.floor(Time_Left / 60);
                    Time_Left %= 60;
                    seconds = Time_Left;
                    
                    dps = 's'; hps = 's'; mps = 's'; sps = 's';
                    //ps is short for plural suffix.
                    if(days == 1) dps ='';
                    if(hours == 1) hps ='';
                    if(minutes == 1) mps ='';
                    if(seconds == 1) sps ='';
                    
                    document.getElementById('countdown').innerHTML = days + ' day' + dps + ' ';
                    document.getElementById('countdown').innerHTML += hours + ' hour' + hps + ' ';
                    document.getElementById('countdown').innerHTML += minutes + ' minute' + mps + ' and ';
                    document.getElementById('countdown').innerHTML += seconds + ' second' + sps;
                    break;
               default: 
                    document.getElementById('countdown').innerHTML = Time_Left + ' seconds';
               }
               
         //Recursive call, keeps the clock ticking.
         setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + format + ');', 1000);
         }
        
function OpenModalWindow(url, name, width, height) {
	if (window.showModalDialog) {
		window.showModalDialog(url,name,'dialogWidth:'+width+'px;dialogHeight:'+height+'px');
	} else {
		window.open(url,name,'height='+height+',width='+width+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');
	}
} 

function htmlentities (string, quote_style) {
    // Convert all applicable characters to HTML entities  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/htmlentities    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: nobbler
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // -    depends on: get_html_translation_table
    // *     example 1: htmlentities('Kevin & van Zonneveld');    // *     returns 1: 'Kevin &amp; van Zonneveld'
    // *     example 2: htmlentities("foo'bar","ENT_QUOTES");
    // *     returns 2: 'foo&#039;bar'
    var hash_map = {}, symbol = '', tmp_str = '', entity = '';
    tmp_str = string.toString();    
    if (false === (hash_map = this.get_html_translation_table('HTML_ENTITIES', quote_style))) {
        return false;
    }
    hash_map["'"] = '&#039;';    for (symbol in hash_map) {
        entity = hash_map[symbol];
        tmp_str = tmp_str.split(symbol).join(entity);
    }
        return tmp_str;
}

function get_html_translation_table (table, quote_style) {
    // Returns the internal translation table used by htmlspecialchars and htmlentities  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/get_html_translation_table    // +   original by: Philip Peterson
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: noname
    // +   bugfixed by: Alex
    // +   bugfixed by: Marco    // +   bugfixed by: madipta
    // +   improved by: KELAN
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Frank Forte    // +   bugfixed by: T.Wild
    // +      input by: Ratheous
    // %          note: It has been decided that we're not going to add global
    // %          note: dependencies to php.js, meaning the constants are not
    // %          note: real constants, but strings instead. Integers are also supported if someone    // %          note: chooses to create the constants themselves.
    // *     example 1: get_html_translation_table('HTML_SPECIALCHARS');
    // *     returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
    
    var entities = {}, hash_map = {}, decimal = 0, symbol = '';    var constMappingTable = {}, constMappingQuoteStyle = {};
    var useTable = {}, useQuoteStyle = {};
    
    // Translate arguments
    constMappingTable[0]      = 'HTML_SPECIALCHARS';    constMappingTable[1]      = 'HTML_ENTITIES';
    constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
    constMappingQuoteStyle[2] = 'ENT_COMPAT';
    constMappingQuoteStyle[3] = 'ENT_QUOTES';
     useTable       = !isNaN(table) ? constMappingTable[table] : table ? table.toUpperCase() : 'HTML_SPECIALCHARS';
    useQuoteStyle = !isNaN(quote_style) ? constMappingQuoteStyle[quote_style] : quote_style ? quote_style.toUpperCase() : 'ENT_COMPAT';
 
    if (useTable !== 'HTML_SPECIALCHARS' && useTable !== 'HTML_ENTITIES') {
        throw new Error("Table: "+useTable+' not supported');        // return false;
    }
 
    entities['38'] = '&amp;';
    if (useTable === 'HTML_ENTITIES') {        entities['160'] = '&nbsp;';
        entities['161'] = '&iexcl;';
        entities['162'] = '&cent;';
        entities['163'] = '&pound;';
        entities['164'] = '&curren;';        entities['165'] = '&yen;';
        entities['166'] = '&brvbar;';
        entities['167'] = '&sect;';
        entities['168'] = '&uml;';
        entities['169'] = '&copy;';        entities['170'] = '&ordf;';
        entities['171'] = '&laquo;';
        entities['172'] = '&not;';
        entities['173'] = '&shy;';
        entities['174'] = '&reg;';        entities['175'] = '&macr;';
        entities['176'] = '&deg;';
        entities['177'] = '&plusmn;';
        entities['178'] = '&sup2;';
        entities['179'] = '&sup3;';        entities['180'] = '&acute;';
        entities['181'] = '&micro;';
        entities['182'] = '&para;';
        entities['183'] = '&middot;';
        entities['184'] = '&cedil;';        entities['185'] = '&sup1;';
        entities['186'] = '&ordm;';
        entities['187'] = '&raquo;';
        entities['188'] = '&frac14;';
        entities['189'] = '&frac12;';        entities['190'] = '&frac34;';
        entities['191'] = '&iquest;';
        entities['192'] = '&Agrave;';
        entities['193'] = '&Aacute;';
        entities['194'] = '&Acirc;';        entities['195'] = '&Atilde;';
        entities['196'] = '&Auml;';
        entities['197'] = '&Aring;';
        entities['198'] = '&AElig;';
        entities['199'] = '&Ccedil;';        entities['200'] = '&Egrave;';
        entities['201'] = '&Eacute;';
        entities['202'] = '&Ecirc;';
        entities['203'] = '&Euml;';
        entities['204'] = '&Igrave;';        entities['205'] = '&Iacute;';
        entities['206'] = '&Icirc;';
        entities['207'] = '&Iuml;';
        entities['208'] = '&ETH;';
        entities['209'] = '&Ntilde;';        entities['210'] = '&Ograve;';
        entities['211'] = '&Oacute;';
        entities['212'] = '&Ocirc;';
        entities['213'] = '&Otilde;';
        entities['214'] = '&Ouml;';        entities['215'] = '&times;';
        entities['216'] = '&Oslash;';
        entities['217'] = '&Ugrave;';
        entities['218'] = '&Uacute;';
        entities['219'] = '&Ucirc;';        entities['220'] = '&Uuml;';
        entities['221'] = '&Yacute;';
        entities['222'] = '&THORN;';
        entities['223'] = '&szlig;';
        entities['224'] = '&agrave;';        entities['225'] = '&aacute;';
        entities['226'] = '&acirc;';
        entities['227'] = '&atilde;';
        entities['228'] = '&auml;';
        entities['229'] = '&aring;';        entities['230'] = '&aelig;';
        entities['231'] = '&ccedil;';
        entities['232'] = '&egrave;';
        entities['233'] = '&eacute;';
        entities['234'] = '&ecirc;';        entities['235'] = '&euml;';
        entities['236'] = '&igrave;';
        entities['237'] = '&iacute;';
        entities['238'] = '&icirc;';
        entities['239'] = '&iuml;';        entities['240'] = '&eth;';
        entities['241'] = '&ntilde;';
        entities['242'] = '&ograve;';
        entities['243'] = '&oacute;';
        entities['244'] = '&ocirc;';        entities['245'] = '&otilde;';
        entities['246'] = '&ouml;';
        entities['247'] = '&divide;';
        entities['248'] = '&oslash;';
        entities['249'] = '&ugrave;';        entities['250'] = '&uacute;';
        entities['251'] = '&ucirc;';
        entities['252'] = '&uuml;';
        entities['253'] = '&yacute;';
        entities['254'] = '&thorn;';        entities['255'] = '&yuml;';
    }
 
    if (useQuoteStyle !== 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';    }
    if (useQuoteStyle === 'ENT_QUOTES') {
        entities['39'] = '&#39;';
    }
    entities['60'] = '&lt;';    entities['62'] = '&gt;';
 
 
    // ascii decimals to real symbols
    for (decimal in entities) {        symbol = String.fromCharCode(decimal);
        hash_map[symbol] = entities[decimal];
    }
    
    return hash_map;
}
