//



function bIsBlank(str) 

{

// returns true if str string contains only non printable characters

var ch;



for(var i=0; i < str.length; ++i)

   {

   ch = str.charAt(i);

   if((ch >= '!') && (ch <= '~')) return false;

   }

return true;

}



function bIsLetter(ch)

{

return (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z'))); 

}



function bIsDigit(ch)

{

return ((ch >= '0') && (ch <= '9'));

}



function bIsAlphaNum(ch)

{

return(bIsLetter(ch) || bIsDigit(ch) || (ch == '_') || (ch=='-') || (ch == '.') );

}



function bIsValidStr(str)

{

var ch;



if(str.length == 0) return false;

for(var i = 0; i < str.length; i++) {

   ch = str.charAt(i);

   if(!bIsAlphaNum(ch)) return false;

   }

return true;

}



function bParseMail(email)

{ // parse an E-mail address 

var i, a, b, s = new String(email);



a = s.split('@');

if(a.length != 2) return false;

if(!bIsValidStr(a[0])) return false;

b = a[1].split('.');

i = b[b.length-1].length; // com or uk type tail

if((i < 2)||(i > 3)) return false;

for(i = 0; i < b.length; i++) if(!bIsValidStr(b[i])) return false;

return true;

}



function bParseHome(address)

{ // parse a home page address excluding protocol

var i, tail = null, a, top = s = new String(address);



if(s.indexOf('/') != -1) {

   top = s.substring(0, s.indexOf('/'));

   if(top.length != s.length-1) tail = s.substring(s.indexOf('/')+1, s.length);

   }

a = top.split('.');

if(a.length < 2) return false;

i = a[a.length-1].length; // com or uk type tail

if((i < 2)||(i > 3)) return false;

for(i = 0; i < a.length; i++) if(!bIsValidStr(a[i])) return false;

if(tail) {

   var x, y, str;



   a = tail.split('/');

   if(a.length > 1) for(i = 0; i < a.length-1; i++) if(!bIsValidStr(a[i])) return false;

   if((x = a[a.length-1].indexOf('.')) != -1) {

     str = a[a.length-1].substring(0, x);

     if(!bIsValidStr(str)) return false;

     if(a[a.length-1].indexOf('#') != -1) {

       var aa;



       str = a[a.length-1].substring(x+1, a[a.length-1].length);

       aa = str.split('#');

       if(aa.length != 2) return false;

       str = aa[0]+aa[1];

       if(!bIsValidStr(str)) return false;

       }

     }

   }

return true;

}



function bParseNumber(str)

{ // parse a contact number

var a = str.split(' ');



if((a[0].length > 2) && ((a[0].charAt(0) == '(') && (a[0].charAt(a[0].length-1) == ')'))) {

   a[0] = a[0].substring(1, a[0].length-1);

   if(a[0].charAt(0) == '+') a[0] = a[0].substring(1, a[0].length);

   }

for(var k = 0; k < a.length; ++k)

   for(i = 0; i < a[k].length; ++i) if(!bIsDigit(a[k].charAt(i))) return false;

return true;

}



function dParseDate(str)

{ // parses and checks date in the form DD-MM-YYYY

var a, d = new Date();



a = str.split('/');

if((a.length != 3) || (parseInt(a[0]) > 31) || (parseInt(a[1]) > 12)) return null;



a[1] -= 1;

d.setDate(a[0]);

d.setMonth(a[1]);

d.setYear(a[2]);

if(parseInt(d.getDate()) == parseInt(a[0])) return d;

return null;

}



function bValidate(form, data, state)

{ // validate a form's fields for the properties:-

// optional (ignored by this function)

// integer imin imax 

// decimal dmin dmax 

// date before after

// mail

// home

// number (telephone etc)

// data is reference to cookie whose .st property is set to state 

// if bValidate returns true 

var msg = empty = errors = "";

var e, v;



for(var i = 0; i < form.elements.length; ++i) {

   e = form.elements[i];

   if((e.type == 'text') || (e.type == 'password') || (e.type == 'textarea') && !e.optional) {

     if((e.value == null) || (e.value == "") || bIsBlank(e.value)) {

       empty += "\n\t" + e.name;

       continue;

       }

     if(e.integer || e.imin || e.imax) {

     v = parseInt(e.value);

     if(isNaN(v) || ((e.imin != null)&& (v < e.imin)) || ((e.imax != null) && (v > e.imax))) {

       errors += "- The field \"" + e.name + "\" must be an integer";

       if(e.imin != null) errors += " that is greater than " + e.imin;

       if((e.imin != null)&& (e.imax != null)) errors += " and less than " + e.imax;

       else if(e.imax != null) errors += " that is less than " + e.imax; errors += ".\n";

       }

     }

     if(e.decimal || e.dmin || e.dmax) {

     v = parseFloat(e.value);

     if(isNaN(v) || ((e.dmin != null) && (v < e.dmin))|| ((e.dmax!= null) && (v > e.dmax))) {

       errors += "- The field \"" + e.name + "\" must be a decimal number";

       if(e.dmin != null) errors += " that is greater than " + e.dmin;

       if((e.dmin != null) && (e.dmax != null)) errors += " and less than " + e.dmax;

       else if(e.dmax != null) errors += " that is less than " + e.dmax;

       errors += ".\n";

       }

     }

     if(e.date || e.after || e.before) {

     var d;



     if((!(d = dParseDate(e.value))) || (e.after && (d.getTime() <= e.after.getTime())) || (e.before && (d.getTime() >= e.before.getTime()))) {

       errors += "- The field \"" + e.name + "\" must be a date";

       if(e.before) errors += " that is before " + e.before.getDate() + "/" + (parseInt(e.before.getMonth() + 1)) + "/" + e.before.getYear();

       if(e.before && e.after) errors += " and after " + e.after.getDate() + "/" + (parseInt(e.after.getMonth() + 1)) + "/" + e.after.getYear();

       else if(e.after) errors += " that is after " + e.after.getDate() + "/" + (parseInt(e.after.getMonth() + 1)) + "/" + e.after.getYear();

       errors += ".\n";

       }

     }

  if(e.mail) {

     if(!bParseMail(e.value)) errors += "- The field \"" + e.name + "\" is not a valid E-mail address.\n";

     }

  if(e.home) {

     if(!bParseHome(e.value)) errors += "- The field \"" + e.name + "\" is not a valid page URL.\n";

     }

  if(e.number) {

     if(!bParseNumber(e.value)) errors += "- The field \"" + e.name + "\" is not a valid contact number.\n";

     }

   } // text type

} // loop 

if(!empty && !errors) {

  if(data && state) {

     data.st = state;

     data.bPut();

     }

   return true;

  }

msg = "The form was not submitted because of the following error(s):-\n\n";

if(empty) {

   msg += "- The following required fields are empty:-\n" + empty + "\n";

   if(errors) msg += '\n';

   }

msg += errors;

alert(msg);

return false;



}





function ValidateForm(thisForm)

{

	// This function checks the entire form before it is submitted

	// Note: This function needs to be customized to fit your form
	with (thisForm)
	{
			if (bIsBlank(name.value))
				{ name.focus(); alert("Please enter your name."); return false;}
			if (bIsBlank(phone.value))
				{ phone.focus(); alert("Please enter your phone number."); return false;}
			if (bIsBlank(email.value))
				{ email.focus(); alert("Please enter your e-mail address."); return false;}
			if (!bIsBlank(email.value) && !bParseMail(email.value))
				{ email.focus(); alert("Please check your e-mail address. It does not seem valid."); return false;}
			if (bIsBlank(message.value))
				{ message.focus(); alert("You did not type a message."); return false;}
	}
	return true;



}


