//Constants - characters to be validated

var SPL_CHAR_BAG = '<>/?,.;:"~`!@#$%^&*()-_=+\\|[]{}' + "'";   //all special chars
var NAME_CHAR_BAG = '<>?"~`!@$%^*=+\\|{}';  //First Name, Last name
var ADDRESS_CHAR_BAG = '<>?~`!@$%^*=+\\|{}';  //address specific special chars for City, State, Country, Title, Address1 and Address2
var PHONE_CHAR_BAG = '<>/?,.;:"~`!@#$%^&*=+\\|{}' + "'"; // for Biz ph, Home ph, Cell and Other ph.
var PWD_CHAR_BAG = '<>/?,.;:~`!@#$%^&*()=+\\|[]{}' + "'" // for User login and passwords
var A_Z_CHAR_BAG = 'abcdefghijklmnopqrstuvwxyz';

function isblank(s)
{
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c!= ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function spaceBetChars(str)
{
	for (var i=0;i<str.length;i++) 
	{
		if (str.charAt(i) == " ") return false
	}
	
	return true;
}

function validateSplChars(str,dispName,charBag)
{
	var j;
	var charBag = '<>/?,;:"~`!#$%^&*()-=+\|[]{}' + "'";
	if (str.length>0 || charBag.length>0)
	{
		//loop for not allowed special chars
		for (j=0;j<charBag.length; j++)
		{		
			if (str.indexOf(charBag.charAt(j))>=0) 
			{
				if (dispName != '');
				return false;	
			}
		}
	}	
	return true;
}

function validateEmail(email,dispName)
{
	//check for space	
	if (!spaceBetChars(email))
	{
		if (dispName != '');
		return false;
	}
	
	//check for special chars
	var charBag = '<>/?,;:"~`!#$%^&*()-=+\|[]{}' + "'";
	if (!validateSplChars(email,dispName,charBag)) return false;

     // there must be >= 1 character before @
     var sLength = email.length;
     var lPos = email.indexOf("@");	
     
     //check additional cond for sencond occurance of @	and atleast one occurance of dot 
     if ((lPos<=0) || (lPos >= sLength) || (email.indexOf("@",lPos+1)>=0) || (email.indexOf(".",lPos+2)==-1)) 
     {
		if (dispName != '');
		return false;
     }

     return true;
}

function verify(f) 
{

var msg;
var empty_fields = '';
var errors = '';
var password = '';
var confirm = '';

	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];
		var display = e.display;
		var errfield;
		
		if (!display) {
			display = e.name;
		}
		
		if (((e.type == 'text') || 
			 (e.type == 'textarea') || 
			 (e.type == 'password') || 
			 (e.type == 'select-one')) && !e.optional) 
		{
			
			if ((e.value == null) || 
				(e.value == '') || 
				isblank(e.value) ) 
			{
				empty_fields += '\n     ' + display;
				if (! errfield){
					errfield = e;
				}
				continue;
			}

			if (e.numeric || (e.min != null) || (e.max != null)) {
				
				var v = parseFloat(e.value);
				
				if (isNaN(v) || ((e.min != null) && (v < e.min)) ||
								((e.max != null) && (v > e.max))) 
				{
					errors += '- The field ' + display + ' must be a number';
					
					if (e.min != null)
						errors += ' that is greater than ' + e.min;
					if (e.max != null && e.min != null)
						errors += ' and less than ' + e.max;
					else if (e.max != null)
						errors += ' that is less than ' + e.max;
						
					errors += '.\n';
					if (! errfield){
						errfield = e;
					}	

				}
			}
			if ((e.nosplchar != null) && ( validateSplChars(e.value) == false)){
					errors += '- The field ' + display + ' has got special characters. \n'
					if (! errfield){
						errfield = e;
					}
			}
			if(e.password != null) {
				password = e.value;
			}		
			
			if(e.confirm != null) {
				confirm = e.value;
			}		

			if (e.validateEmail != null) {
				if ( validateEmail(e.value) == false){
					errors += '- The field ' + display + ' must be a valid email id. \n'
					if (! errfield){
						errfield = e
					}
				}
			}
		
			
				
			
		}
	}
	
	if (password != confirm){
		errors += '- The password fields do not match \n'
	}


	if (!empty_fields && !errors) return true;

	msg = '______________________________________________________\n\n'
	msg += 'The form was not submitted because of the following error(s).\n';
	msg += 'Please correct these error(s) and re submit.\n';
	msg += '______________________________________________________\n\n'

	if  (empty_fields) {
		msg += '- Please fill in all required fields:\n'
		msg += '- The following field(s) are empty:\n'
				+ empty_fields + '\n';
		if (errors) msg += '\n';
	}
	
	msg += errors;
	alert(msg);
	errfield.focus()
	errfield.select()
	return false;
}

