//
// 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');
	}
} 