/*
FORM VALIDATION SCRIPT
version 1.1
Copyright (C) 100jan Design Studio.
http://www.100jan.com/

You may NOT use this script without written 
permission from 100jan Design Studio.
*/

<!-- Hide the script from old browsers

//===========================================================
//CONFIGURATION
//===========================================================
var error_class=true; //true changes classes, false does not.
var error_msg=true; //true pops-up alert box, false does not.
var reject_yahoo=false; //true rejects free emails like yahoo, false does not.
var good_class="ff1"; //class name if all good.
var bad_class="ff1_error"; //class name if error.

//===========================================================
//LANGUAGES
//===========================================================
if (!checkform_lang) {var checkform_lang="english";}

// ENGLISH --------------------------------------------------
if (checkform_lang=="english") {
var msg_0="The following errors occurred:\n\n";
var msg_1="can not be empty.\n";
var msg_2="must contain a number";
var msg_3="that is equal or greater than";
var msg_4=" and equal or less than ";
var msg_5=" that is equal or less than ";
var msg_6="must contain";
var msg_7=" at least ";
var msg_8=" and not more than ";
var msg_9=" not more than ";
var msg_10=" characters.\n";
var msg_11=" must contain a valid email address.\n";
var msg_12=" can not be free email address,\n   e.g. yahoo, hotmail, etc.\n   We do not accept anonymous messages.\n   Please use your personal email address,\n   e.g. from your ISP.\n";
}
// ----------------------------------------------------------

// SRPSKI ---------------------------------------------------
if (checkform_lang=="srpski") {
var msg_0="Napravili ste sledeće greške:\n\n";
var msg_1="nemože biti prazno.\n";
var msg_2="mora sadržati broj";
var msg_3="koji je jednak ili veći od";
var msg_4=" i jednak ili manji od ";
var msg_5=" koji je jednak ili manji od ";
var msg_6="mora sadržati";
var msg_7=" najmanje ";
var msg_8=" i ne više od ";
var msg_9=" ne više od ";
var msg_10=" karaktera.\n";
var msg_11=" mora sadržati validnu email adresu.\n";
var msg_12=" nemože biti besplatna email adresa,\n   kao što je yahoo, hotmail, i sl.\n   Ne prihvatamo anonimne poruke.\n   Molimo Vas koristite Vašu ličnu email adresu,\n   kao što je ona kod Vašeg provajdera (ISP).\n";
}
// ----------------------------------------------------------

//===========================================================
// A utility function that returns true if a string 
// contains only whitespace characters.
//===========================================================

function isanything(s)
{
  for(var i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);
     if ((c != ' ') &&
         (c != '\n') &&
         (c != '\t'))
        return false;
  }
  return true;
}

//===========================================================
// This is the function that performs <form> validation.  
// It will be invoked from the onSubmit() event handler.
// The handler should return whatever value this function
// returns.
//===========================================================

function checkform(f)
{
  var msg;
  var errors = "";
  var whyreq = false;

  // Loop through the elements of the form, looking for all
  // text and textarea elements that don't have an
  // "optional" property defined.  Then, check for fields
  // that are empty and make a list of them.
  // Also, if any of these elements have a "min" or a "max"
  // property defined, then verify that they are numbers 
  // and that they are in the right range.
  // Put together error messages for fields that are wrong.


for(var i = 0; i < f.length; i++)
  {
  var e = f.elements[i];
	
     if ((e.type == "text") ||
         (e.type == "file") ||
         (e.type == "textarea") ||
         (e.type == "password") ||		 
         (e.type == "select-one") ||		 
         (e.state))
     {


//IF VALIDATING
if (e.attributes.getNamedItem("mustbe")) {


if (e.attributes.getNamedItem("mustbe").value=="anything") //if anything
{

        if ((e.value == null) || (e.value == "") || isanything(e.value))
        {
           errors += "- \"" + e.attributes.getNamedItem("emsg").value + "\" " + msg_1;
           //bad class
		   if (error_class==true) {e.className=bad_class;}
		   continue;
        }
		else 
		{
		//good class
		if (error_class==true) {e.className=good_class;}
		}
} //endif anything

//===========================================================
        // Now check for fields that are supposed 
        // to be numeric.
//===========================================================
/*
        if (e.attributes.getNamedItem("mustbe").value=="numeric")
        {
        //check if field is optional
		if (!(e.attributes.getNamedItem("optional").value=="true" && e.value.length==0)) {
           
		if (isNaN(e.value)==false)  {
		
		   var v = parseFloat(e.value);
//           var v = e.value;
           if (isNaN(v) ||
              ((e.min != null) && (v < e.min)) ||
              ((e.max != null) && (v > e.max)))
           {
              errors += "- \"" +
                        e.attributes.getNamedItem("emsg").value +
                        "\" " + msg_2;
              if (e.min != null)
                 errors += " " + msg_3 + " " +
                           e.min;

              if (e.max != null &&
                  e.min != null)
                 errors += msg_4 +
                           e.max;

              else if (e.max != null)
                 errors += msg_5 +
                           e.max;

				errors += ".\n";

		  	//bad class
			if (error_class==true) {e.className=bad_class;}
           }
		   else {
		  	//good class
			if (error_class==true) {e.className=good_class;}
		   		}
		}
        else {
		              errors += "- \"" +
                        e.attributes.getNamedItem("emsg").value +
                        "\" " + msg_2 + "\n";
		//bad class
		if (error_class==true) {e.className=bad_class;}
			}
	}//optional
	else {
			  	//good class
			if (error_class==true) {e.className=good_class;}
	}
} //end
*/
//===========================================================
// Check field length
//===========================================================
/*
        if (e.attributes.getNamedItem("mustbe").value=="length")
		   {
		   		   
        //check if field is optional
		if (!(e.optional=="true" && e.value.length==0)) {
		
           if (((e.min != null) && (e.value.length < e.min)) || ((e.max != null) && (e.value.length > e.max)))
           {
              errors += "- \"" +
                        e.attributes.getNamedItem("emsg").value +
                        "\" " + msg_6 ;
              if (e.min != null)
                 errors += msg_7 +
                           e.min;						   

              if (e.max != null &&
                  e.min != null)
                 errors += msg_8 +
                           e.max;

              else if (e.max != null)
                 errors += msg_9 +
                           e.max;

              errors += msg_10;

		  	//bad class
			if (error_class==true) {e.className=bad_class;}
           }
		   else 
		   {
		  	//good class
			if (error_class==true) {e.className=good_class;}
		   }		   
		   
		   } //optional
		   else {
		   		  	//good class
			if (error_class==true) {e.className=good_class;}
		   }

		}

*/
//===========================================================
        // Now check for fields that are supposed to be emails.
        // Not exactly as described in RFC 2822, as
        // the strict regexp for legal email addresses
        // runs about 2 printed pages.		
//===========================================================

        if (e.attributes.getNamedItem("mustbe").value=="email")
        {

        //check if field is optional
		if (!(e.attributes.getNamedItem("optional") && e.value.length==0)) {
		
			var filter1 =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
			var filter2 = /@(yahoo|hotmail|lycos|gmail)\./;
			var error_level = 0;		

				//check filter 1
				if (! e.value.match(filter1)) {
				    errors += "- \"" + e.attributes.getNamedItem("emsg").value + "\"" + msg_11;
					error_level = 1;
				}
				

		if (reject_yahoo==true) {				
				//check filter 2
				if (e.value.match(filter2)) {
				    errors += "- \"" + e.attributes.getNamedItem("emsg").value + "\"" + msg_12;
		  			//bad class
					error_level = 1;
				}
		}


				//set class
				if (error_class==true && error_level==1) {e.className=bad_class;} else {e.className=good_class;}
			

		   } //optional
		   else {
		   		  	//good class
					if (error_class==true) {e.className=good_class;}
		   }

        } //main


//===========================================================
	// Now check for fields that are supposed 
	// not to have spaces
//===========================================================	
/*
        if (e.nospaces)
        {
           var seenAt = false;
           var append = "";

           for(var j = 0; j < e.value.length; j++)
           {
              var c = e.value.charAt(j);

              if ((c == ' ') ||
                  (c == '\n') ||
                  (c == '\t'))
                 errors += "- The field " + e.description +
                           " must not contains white-space";
           }
        }
*/		
//===========================================================
			//if validating end
			} 
		//type check end	
		} 
	//elements collection loop end		
    } 
	
//--------------------------------------------
//===========================================================
  // Now, if there were any errors, then display the
  // messages, and return false to prevent the form from
  // being submitted.  Otherwise return true.
//===========================================================
  if (!errors) {
    return true;
	}

  else {
  msg = msg_0;
  msg += errors;
  if (error_msg==true) {alert(msg);}
  return false;
  }
//--------------------------------------------

//function end
}
  
// end hiding from old browsers -->