// JavaScript Document

var field_main;
function do_blink(field)
{
	var errorcolor="#B9DB76";
	field.focus();
	field.select();
	field.style.background=errorcolor;
    field_main=field;
	setInterval("temp()",500);    
}

function temp(field)
{
	var okcolor="#B9DB76";
	field_main.style.background=okcolor;
}

function validate(field)
{
	try
	{
		valiclass=field.getAttribute("valiclass");
		valimessage=field.getAttribute("valimessage");
		if(valiclass=="required")
		{	
			req=field.getAttribute("req");
			pattern="\\w{"+req+",}";
			if(!field.value.match(pattern))
			{
				alert(valimessage);
				do_blink(field);
				return false;
			}
		}
		
		else if(valiclass=="number")
		{		
			if((field.value.length<1)||isNaN(field.value))
			{
				alert(valimessage);
				do_blink(field);
				return false;
			}		
		}
		
		else if(valiclass=="checkpassword")
		{
			password = document.getElementById('password');
			verify_password = document.getElementById('verify_password');
			if((password.value.length || verify_password.value.length) < 3)	
			{
				alert("Password should be of at least 3 characters long");	
				do_blink(field);
				return false;
			}
			else
			{
				if(password.value!=verify_password.value)	
				{
					alert("Password do not match");
					do_blink(field);
					return false;
				}
			}
		}
		
		else if(valiclass=="creditcard")
		{
			pattern="^[0-9]{16}$";
			if(!field.value.match(pattern))
			{
				alert(valimessage);
				do_blink(field);
				return false;
			}
		}
		
		else if(valiclass=="zip")
		{
			pattern="^[0-9]{5}$";
			if(!field.value.match(pattern))
			{
				alert(valimessage);
				do_blink(field);				
				return false;
			}
		}
	
		else if(valiclass=="code")
		{
			pattern="^[0-9]{3}$";
			if(!field.value.match(pattern))
			{
				alert(valimessage);
				do_blink(field);
				return false;
			}
		}
		
		else if(valiclass=="integer")
		{
			$bool=field.value.match("^[0-9]{1,4}$");	
			if($bool==0)
			{
				alert("Please enter a non-zero number");
				do_blink(field);
				return false;
			}
			if((!$bool)||isNaN(field.value)||(field.value.indexOf(".")!=-1))
			{
				alert(valimessage);
				do_blink(field);
				return false;	
			}
		}
		
		else if(valiclass=="email")
		{
			pattern="^\\w{1,}@\\w{1,}(\\.\\w{1,}){1,}$";
			if(!field.value.match(pattern))
			{
				alert(valimessage);
				do_blink(field);
				return false;
			}
		}
		
		else if(valiclass=="select")
		{
			if(field.value=="NA")
			{
				alert(valimessage);
				field.focus();
				return false;
			}
		}
		
		else if(valiclass=="picture")
		{
			if(field.value.length>0)
			{
				values=field.value.toLowerCase();
				if(!ends_with(values))
			  	{
					alert("Only jpg,gif and png images are supported");
					do_blink(field);
					return false;
				}
			}
		}
		return true;
	}
catch(ex)
{
alert(ex.message);
return true;
}
}



function call_validate(form,from,to)
{
for(counter=from;counter<to;counter++)
{
bool=validate(form[counter]);
if(!bool)
{
return false;
break;
}
}
form.submit();
}






/////////////////////


function ends_with(hay)
{
	hay=hay.replace(/^\s*|\s*$/g,"");
	hay_length=hay.length;
	hay_part=hay.substring((hay_length-4),hay_length);
	if(hay_part==".jpg" || hay_part==".png" || hay_part==".gif")
		return true;
}
function validate_range()
{
//alert(validate_range.arguments.length);	
for(counter=0;counter<validate_range.arguments.length;counter++)
{
bool=validate(validate_range.arguments[counter]);
if(!bool)
{
return false;
break;
}
}
validate_range.arguments[0].form.submit();
return true;
}

