<!-- Begin

/*
Form validation script modified
Original script : http://onlinetools.org/articles/unobtrusivejavascript/chapter5.html
Created: 26.02.2008 HP
*/

function checkform(of,path)
	//of is required field list
	//path is path to images

		{//alert(path);
		// Test if DOM is available and there is an element called required
			if(!document.getElementById || !document.createTextNode){return;}
			if(!document.getElementById('required')){return;}

		// Define error messages and split the required fields
			var errorID='errormsg';
			var errorClass='error'
			var errorMsg='Please enter or change the field(s) marked with a ';
			var errorImg= path + '/images/alertbox.gif';
			var errorAlt='Error';
			var errorTitle='This field has an error!';
			var reqfields=document.getElementById('required').value.split(',');

		// Cleanup old mess
			// if there is an old errormessage field, delete it
			if(document.getElementById(errorID))
			{
				var em=document.getElementById(errorID);
				em.parentNode.removeChild(em);
			}
			// remove old images and classes from the required fields
			for(var i=0;i<reqfields.length;i++)
			{
				var f=document.getElementById(reqfields[i]);
				if(!f){continue;}
				if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
				{
					f.parentNode.removeChild(f.previousSibling);
				}
				f.className='';
			}
		// loop over required fields
			for(var i=0;i<reqfields.length;i++)
			{
		// check if required field is there
				var f=document.getElementById(reqfields[i]);
				if(!f){continue;}
		// test if the required field has an error, 
		// according to its type
				switch(f.type.toLowerCase())
				{
					case 'text':
						if(f.value=='' && f.id!='email'){cf_adderr(f)}							
		// email is a special field and needs checking
						if(f.id=='email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}							
					break;
					case 'textarea':
						if(f.value==''){cf_adderr(f)}							
					break;
					case 'password':
						if(f.value==''){cf_adderr(f)}
					break;					
					case 'checkbox':
						if(!f.checked){cf_adderr(f)}							
					break;
					case 'select-one':
						if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}							
					break;
					case 'select-multiple':
						if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}							
					break;
					
				}
			}
			return !document.getElementById(errorID);

			/* Tool methods */
			function cf_adderr(o)
			{
				// create image, add to and colourise the error fields
				var errorIndicator=document.createElement('img');
				errorIndicator.alt=errorAlt;
				errorIndicator.src=errorImg;
				errorIndicator.title=errorTitle;
				o.className=errorClass;
				o.parentNode.insertBefore(errorIndicator,o);

			// Check if there is no error message
				if(!document.getElementById(errorID))
				{
				// create errormessage and insert before submit button
					var em=document.createElement('div');
					em.id=errorID;
					var newp=document.createElement('p');
					newp.appendChild(document.createTextNode(errorMsg))
					// clone and insert the error image
					newp.appendChild(errorIndicator.cloneNode(true));
					em.appendChild(newp);
					// find the submit button 
					for(var i=0;i<of.getElementsByTagName('input').length;i++)
					{
						if(/submit/i.test(of.getElementsByTagName('input')[i].type))
						{
							var sb=of.getElementsByTagName('input')[i];
							break;
						}
					}
					if(sb)
					{
						sb.parentNode.insertBefore(em,sb);
					}	
				} 
			}
			function cf_isEmailAddr(str) 
			{
			    //return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
				//HP 26.02.2008: A modified script which is more robust, i think
				//return str.match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
				//HP 7 Sept 09: Replaced to allow apostrophes in email address
			    return str.match(/^([\w-\']+(?:\.[\w-\']+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/);				
			}
	//return false;			
		}
		
		
	/* Numbers only allowed in the text field */
		function checkIt(evt) {
		    evt = (evt) ? evt : window.event
		    var charCode = (evt.which) ? evt.which : evt.keyCode
		    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		        status = "This field accepts numbers only."
		        return false
		    }
		    status = ""
		    return true
		}		
		
		
/*
	INTEGER VALIDATION 
	The following scripts are from : http://www.acmesoffware.com/acme/default.asp  		
	Javascript Tips
*/
		// validate number is integer
		function validateInt(num)
		   {
		   	  var o = document.getElementById(''+num+'');
			  switch (isInteger(o.value))
			  {
				 case true:
					// alert(o.value + " is an integer") Do not need to show any message if it's okay
					break;
				 case false:
					alert(o.value + " is not an integer")
			  }
		   }
		   
		   // validate number (num) is in the range between (value1) and (value2)
		   function validateRange(num,value1,value2)
		   { 
			  var s = document.getElementById(''+num+'').value;
			  var A = value1;
			  var B = value2;
		
			  switch (isIntegerInRange(s, A, B))
			  {
				 case true:
					// alert(s + " is in range from " + A + " to " + B) Do not need to show any message if it's okay
					break;
				 case false:
					alert(s + " is not in range from " + A + " to " + B)
			  }
		   }

		   
			// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
		   function isIntegerInRange (s, a, b)
		   {   if (isEmpty(s))
				 if (isIntegerInRange.arguments.length == 1) return false;
				 
				 else return (isIntegerInRange.arguments[1] == true);
		
			  // Catch non-integer strings to avoid creating a NaN below,
			  // which isn't available on JavaScript 1.0 for Windows.
			  if (!isInteger(s, false)) return false;
		
			  // Now, explicitly change the type to integer via parseInt
			  // so that the comparison code below will work both on
			  // JavaScript 1.2 (which typechecks in equality comparisons)
			  // and JavaScript 1.1 and before (which doesn't).
			  var num = parseInt (s);
			  return ((num >= a) && (num <= b));
		   }
		   

		   function isInteger (s)
		   {
			  var i;
		
			  if (isEmpty(s))
			  if (isInteger.arguments.length == 1) return 0;
			  else return (isInteger.arguments[1] == true);
		
			  for (i = 0; i < s.length; i++)
			  {
				 var c = s.charAt(i);
		
				 if (!isDigit(c)) return false;
			  }
		
			  return true;
		   }
		   
		   
		   function isEmpty(s)
		   {
			  return ((s == null) || (s.length == 0))
		   }
		   
		
		   function isDigit (c)
		   {
			  return ((c >= "0") && (c <= "9"))
		   }


   		   

// End -->		
