
//////////////////////////////////////////////////////////////////
//	- COOL VALIDATE	v1.00 -										//
//////////////////////////////////////////////////////////////////
//	Developed By ;												//
// 	SARFRAZ AHMED CHANDIO										//
//																//
//	sarfraznawaz2005@gmail.com									//
//	http://www.brainstechnology.com								//
//																//
//	01 Feb 2009													//
//////////////////////////////////////////////////////////////////
//	Please keep this notice intact if you are using this file.	//
//////////////////////////////////////////////////////////////////

/*
	?? Future Additions ?? (--) & Fixes (-):
	========================================
		-- Custom RegEX
		-- God Knows !!
*/


// cross-browser document.getElementById, should be on top of code.
if(!document.getElementById)
{
  if(document.all)
  document.getElementById=function()
  {
	if(typeof document.all[arguments[0]]!="undefined")
	return document.all[arguments[0]]
	else
	return null
  }
  else if(document.layers)
  document.getElementById=function()
  {
	if(typeof document[arguments[0]]!="undefined")
	return document[arguments[0]]
	else
	return null
  }
}
////////////////////////////////////////////////

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
return sString;
}

function validateForm(formid)
{
	// regex patterns, more can be added
	var pattern_email = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var pattern_date = /^\d\d\/\d\d\/\d\d\d\d$/;
	var pattern_number = /^\-?\d+$/;
	var pattern_text = /^[a-zA-Z]+$/;
	var pattern_alpha = /^\w+$/;
	var pattern_decimal = /^\-?\d+(\.\d+)?$/;
	var pattern_web = /^https?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;
	////////////////////////////////////////////////
	
	var formobj = document.forms[formid];
	var arr_vtype = new Array();
	var fieldname = "";
	var vtype = "";
	var counter = 0;
	var fieldtype = "";
	var fieldvalue = "";
	var fieldtitle = "";
	var curfield = "";
	var emsg = "";
	for (var i=0; i<formobj.elements.length; i++)
	{
		arr_vtype = formobj.elements[i].className.split(" ");

		// If this is the field to be validated
		if (arr_vtype[0] == "required")
		{
			counter++; // total fields to be validated
			
			// get current field
			curfield = formobj.elements[i];
			// get field type
			fieldtype = formobj.elements[i].type;

			// get field name
			if (formobj.elements[i].getAttribute("name"))
			{
				fieldname = formobj.elements[i].getAttribute("name");
			}

			// get field title
			if (formobj.elements[i].getAttribute("title"))
			{
				fieldtitle = formobj.elements[i].getAttribute("title");
			}

			if (!fieldtitle)
			{
				fieldtitle = fieldname;
			}

			// get current filed value
			fieldvalue = formobj.elements[i].value;
			// get validation type
			vtype = arr_vtype[1];
			
			// get validation stuff from class tag irrespective of their order
			for (var z = 0; z < 10; z++)
			{
				if (arr_vtype[z] == "min")
				{
					var cmin = arr_vtype[parseInt(z, 10) + 1];
				}
				else if (arr_vtype[z] == "max")
				{
					var cmax = arr_vtype[parseInt(z, 10) + 1];
				}
				else if (arr_vtype[z] == "match")
				{
					var comparefield = arr_vtype[parseInt(z, 10) + 1];
					var pvalue = fieldvalue;
					var ptitle = fieldtitle;
					var pfield = curfield;
				}
			}
			
			// for comparing password and confirm passwords
			if (comparefield != "" && fieldname == comparefield)
			{
				var cpvalue = fieldvalue;
				var cptitle = fieldtitle;
				
				if (trimAll(pvalue) != "" && trimAll(cpvalue) != "")
				{
					if (pvalue.length < cmin && cmin > 0 && pfield.className.indexOf("min") > -1)
					{
						pfield.style.borderColor = "#FF0000";
						emsg += "-->  " + ptitle + " - should be at least " + cmin + " characters long\n";
					}
					else if (pvalue.length > cmax && cmax > 0 && pfield.className.indexOf("max") > -1)
					{
						pfield.style.borderColor = "#FF0000";
						emsg += "-->  " + ptitle + " - should be at most " + cmax + " characters long\n";
					}
					else if (pvalue != cpvalue)
					{
						pfield.style.borderColor = "#FF0000";
						emsg += "-->  " + ptitle + " - and " + cptitle + "  must MATCH\n";
					}
					else
					{
						pfield.style.borderColor = "#00FF00";
					}
				}
			}
			////////////////////////////////////////////
			
			// Do the validation stuff now
			switch (vtype)
			{
				case "alpha":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_alpha.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be ALPHA characters\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				case "text":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_text.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should only contain A-Z characters\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				case "email":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_email.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be in valid EMAIL format\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				case "date":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_date.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be in DD/MM/YYYY format\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				case "number":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_number.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should contain only NUMBERS\n";
					}
					// since this is number, we compare them directly rather than their length
					else if (parseInt(fieldvalue, 10) < parseInt(cmin, 10) && parseInt(cmin, 10) > 0 && curfield.className.indexOf("min") > -1)
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be at least " + cmin + "\n";
					}
					else if (parseInt(fieldvalue, 10) > parseInt(cmax, 10) && parseInt(cmax, 10) > 0 && curfield.className.indexOf("max") > -1)
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be at most " + cmax + "\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				case "decimal":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_decimal.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should contain only DECIMAL NUMBER\n";
					}
					// since this is number, we compare them directly rather than their length
					else if (parseFloat(fieldvalue) < parseFloat(cmin) && parseFloat(cmin) > 0 && curfield.className.indexOf("min") > -1)
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be at least " + cmin + "\n";
					}
					else if (parseFloat(fieldvalue) > parseFloat(cmax) && parseFloat(cmax) > 0 && curfield.className.indexOf("max") > -1)
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be at most " + cmax + "\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				case "web":
					if (trimAll(fieldvalue) == "")
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - REQUIRED\n";
					}
					else if (!pattern_web.test(fieldvalue))
					{
						curfield.style.borderColor = "#FF0000";
						emsg += "-->  " + fieldtitle + " - should be in URL format\n";
					}
					else
					{
						curfield.style.borderColor = "#00FF00";
					}
				break;
				// for fields containing required keyword only
				default:
					if (fieldtype == "checkbox")
					{
						if (!curfield.checked)
						{
							emsg += "-->  " + fieldtitle + " - REQUIRED\n";
						}
					}
					else
					{
						if (trimAll(fieldvalue) == "")
						{
							curfield.style.borderColor = "#FF0000";
							emsg += "-->  " + fieldtitle + " - REQUIRED\n";
						}
						else
						{
							if (fieldtype != "radio")
							{
								curfield.style.borderColor = "#00FF00";
							}
						}
					}
				break;
			}
			
		}
	}

	// separetely for radio buttons since many radios can have the same name
	var rnames = "";
	var rtitles = "";
	var radiogroup = new Array();
	var arr_titles = new Array();
	var msg = "";
	
	// Get the radio fields to be validated
	for (var j = 0; j < formobj.elements.length; j++)
	{
		arr_vtype2 = formobj.elements[j].className.split(" ");
		if (arr_vtype2[0] == "required")
		{
			if (formobj.elements[j].type == "radio")
			{
				if (!formobj.elements[j].checked)
				{
				  if (msg.indexOf(formobj.elements[j].getAttribute("title")) == -1)
				  {
						formobj.elements[j].style.borderColor = "#FF0000";
						msg += "-->  " + formobj.elements[j].getAttribute("title") + " - REQUIRED\n";
				  }
				}
				else if (formobj.elements[j].checked)
				{
					//msg = msg.replace("-->  " + formobj.elements[j].getAttribute("title") + " - REQUIRED\n", "");
					rtitles += "|" + formobj.elements[j].getAttribute("title");
					rnames += "|" + formobj.elements[j].getAttribute("name");
				}
			}
		}
	}

	radiogroup = rnames.split("|");
	arr_titles = rtitles.split("|");
	
	// remove checked radio from the msg
	for (var p in radiogroup)
	{
		msg = msg.replace("-->  " + arr_titles[p] + " - REQUIRED\n", "");
	}
	
	if (msg) emsg += msg;
	/////////////////////////////////////////	

	if (emsg)
	{
		var dashes = "----------------------------------------------";
		alert ("You failed to correctly fill in your form:\n" + dashes
		+ "\n" + emsg + dashes + "\nPlease re-enter and submit again!");
		return false;
	}
	else
	{
		return true;
	}
}

