// Generic Form Validation
// Jacob Hage (jacob@hage.dk)
var checkObjects	= new Array();
var errors		= "";
var returnVal		= false;
var language		= new Array();
language["header"]	= "The following error(s) occured:"
language["start"]	= "->";
language["field"]	= " Field ";
language["require"]	= " is required";
language["min"]		= " and must consist of at least ";
language["max"]		= " and must not contain more than ";
language["minmax"]	= " and no more than ";
language["chars"]	= " characters";
language["num"]		= " and must contain a number";
language["email"]	= " must contain a valid e-mail address";

// -----------------------------------------------------------------------------
// define - Call this function in the beginning of the page. I.e. onLoad.
// n = name of the input field (Required)
// type= string, num, email (Required)
// min = the value must have at least [min] characters (Optional)
// max = the value must have maximum [max] characters (Optional)
// d = (Optional)
// -----------------------------------------------------------------------------
function define(n, type, HTMLname, min, max, d) {
var p;
var i;
var x;
if (!d) d = document;
if ((p=n.indexOf("?"))>0&&parent.frames.length) {
d = parent.frames[n.substring(p+1)].document;
n = n.substring(0,p);
}
if (!(x = d[n]) && d.all) x = d.all[n];
for (i = 0; !x && i < d.forms.length; i++) {
x = d.forms[i][n];
}
for (i = 0; !x && d.layers && i < d.layers.length; i++) {
x = define(n, type, HTMLname, min, max, d.layers[i].document);
return x;
}
eval("V_"+n+" = new formResult(x, type, HTMLname, min, max);");
checkObjects[eval(checkObjects.length)] = eval("V_"+n);
}
function formResult(form, type, HTMLname, min, max) {
this.form = form;
this.type = type;
this.HTMLname = HTMLname;
this.min  = min;
this.max  = max;
}

// -----------------------------------------------------------------------------
// define - the Validation function.
// -----------------------------------------------------------------------------
function validate1() {

	
if (checkObjects.length > 0) {
errorObject = "";
for (i = 0; i < checkObjects.length; i++) {
validateObject = new Object();
validateObject.form = checkObjects[i].form;
validateObject.HTMLname = checkObjects[i].HTMLname;
if( checkObjects[i].form){
validateObject.val = checkObjects[i].form.value;
validateObject.len = checkObjects[i].form.value.length;
}
validateObject.min = checkObjects[i].min;
validateObject.max = checkObjects[i].max;
validateObject.type = checkObjects[i].type;
if (validateObject.type == "num" || validateObject.type == "string") {
if ((validateObject.type == "num" && validateObject.len <= 0) || (validateObject.type == "num" && isNaN(validateObject.val))) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['num'] + "\n";
} else if (validateObject.min && validateObject.max && (validateObject.len < validateObject.min || validateObject.len > validateObject.max)) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['min'] + validateObject.min + language['minmax'] + validateObject.max+language['chars'] + "\n";
} else if (validateObject.min && !validateObject.max && (validateObject.len < validateObject.min)) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['min'] + validateObject.min + language['chars'] + "\n";
} else if (validateObject.max && !validateObject.min &&(validateObject.len > validateObject.max)) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + language['max'] + validateObject.max + language['chars'] + "\n";
} else if (!validateObject.min && !validateObject.max && validateObject.len <= 0) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['require'] + "\n";
   }
} else if(validateObject.type == "email") {
// Checking existense of "@" and ".".
// Length of must >= 5 and the "." must
// not directly precede or follow the "@"
if ((validateObject.val.indexOf("@") == -1) || (validateObject.val.charAt(0) == ".") || (validateObject.val.charAt(0) == "@") || (validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")) { errors += language['start'] + language['field'] + validateObject.HTMLname + language['email'] + "\n"; }
      }
   }
   if (errors) {
       alert(language["header"].concat("\n" + errors));
       errors = "";
       returnVal = false;
   } 
   else {
       returnVal = true;
   }
}
else {
       returnVal = false;
}

}

function findElementPos(HTMLName)
{
	for (i = 0; i < checkObjects.length; i++) {
	  if (HTMLName == checkObjects[i].HTMLname) {
		 return i;
	  }
	}
	return -1;
}

function undefine(HTMLName)
{
  pos = findElementPos(HTMLName);
  if (pos != -1) {
    checkObjects.splice(pos,1)
  }
}

function redefine()
{
 // First emtpty the array
 while (checkObjects.length != 0)
    checkObjects.pop();

 // Define all the fields that are mandatory for all the four products.
 define('first_name', 'string', 'First Name');
 define('last_name', 'string', 'Last Name');
 define('email', 'email', 'Email');
 define('phone', 'string', 'Phone');
 define('company', 'string', 'Company');
 define('employees', 'num', 'Employees');
 define('street', 'string', 'Address');
 define('city', 'string', 'City');
 
 if(leadform.country.value=="United States") {
 	define('state', 'string', 'State');
	define('zip', 'num', 'Zip \ Postal code', 5);
 }
 define('country', 'string', 'Country');

 define('00N30000000ey32', 'string', 'Relation to projcet');
 define('00N30000000ey38', 'string', 'End customer type ');
 define('00N30000000f0EG', 'string', 'Project Type');
 define('00N30000000ey2l', 'string', 'Product');


 // When no product is selected following fields are shown with * and
 // hence are considered mandatory.
	 // Total # of subscribers
/*	 
	 define('00N30000000ey2m', 'num', 'Total # of subscribers');
	 // Number of peak simultaneous users / callers

     define('00N30000000ey2n', 'num', 'Number of peak simultaneous users / callers');
	 // Total # of locations
	 define('00N30000000ey2v', 'num', 'Total # of locations');

*/
 // Now define the fields that are mandatory depending on the product
 if(leadform.fldProduct.value=="RBS ISP") {
   // Total # of subscribers*
   define('00N30000000ey2m', 'num', 'Total # of subscribers');
 }
 else if(leadform.fldProduct.value=="RBS Hotspot") {
   // Peak number of simultaneous users from all locations
   define('00N30000000ey2n', 'num', 'Peak number of simultaneous users from all locations');
   // Total # of locations
   define('00N30000000ey2v', 'num', 'Total # of locations');
 }
 else if(leadform.fldProduct.value=="RBS VoIP") {
   // Number of voice lines (E1=30 lines, T1=24 lines)
   define('00N30000000ey2n', 'num', 'Number of voice lines (E1=30 lines, T1=24 lines');
 }
 else if(leadform.fldProduct.value=="RBS Advanced"){	  // for RBS Advanced
   // Total # of subscribers*
	 define('00N30000000ey2m', 'num', 'Total # of subscribers');
   // Number of peak simultaneous userscallers*
     define('00N30000000ey2n', 'num', 'Number of peak simultaneous users / callers');
   // Total # of locations
     define('00N30000000ey2v', 'num', 'Total # of locations');
 }

}
function validateFields(id,reg)
{

   if(eval(document.getElementById(id)))
   {
       var elementValue=document.getElementById(id).value;
       if(reg.test(elementValue) == false) {
            return false;
       }
       else
       {
           return true;
       }
   }
   else
   {
     return true;
   }
}


