/****************************************************************************************************
File : ref_friend.js
Author : P.Padmanabhan
Date : 19/03/2008 
*****************************************************************************************************
Description : refer a friend validations - bharateyebank.org
********************************************************************************************************/


function IsEmpty(obj, obj_type)
{
	if (obj_type == "text" || obj_type == "password" || obj_type == "textarea")	
		{
			var objValue;
			objValue = obj.value.replace(/\s+$/,"");	
			if (objValue.length == 0) 
			{
			//obj.focus();
			return true;
			}
		}
}

function Validateany( NumStr, String )
	{
 		for( var Idx = 0; Idx < NumStr.length; Idx ++ )
 		{
			 var Char = NumStr.charAt( Idx );
			 var Match = false;

				for( var Idx1 = 0; Idx1 < String.length; Idx1 ++)
				{
					 if( Char == String.charAt( Idx1 ) )
					 Match = true;
				}

				if ( !Match )
					return false;
 		}

        	return true;
	}

function IsEmail(email,Label)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  var x = email;
  	if(x!="")	
	{			
		var testresults;
		var varEmail=email;
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;														
		if (filter.test(varEmail))
		{		
			testresults=true;
					
		}
		else
		{
			alert("Invalid email ID");
			return false;
		}
		
		
			var testresults;
			var varEmail = ""
			varEmail=email;
			var tmp = varEmail;
			var i,j,t1,vale;
			//alert (varEmail.length);
			i = varEmail.indexOf('@');
			j = tmp.indexOf('.',i);
			tn = j - i;
			
	}
	else
	{
		alert("Please enter your email");
		return false;
	}
  	return true;
}

function trim(Val)
{
	while(''+Val.charAt(0)==' ')
	Val=Val.substring(1,Val.length);
	return Val
}

function validateForm ()
{
	
	if(IsEmpty(document.referFrm.name,"text"))
	{
		alert("Please enter your name");
		referFrm.name.focus();
		return false;
	}

	if (!IsEmpty(document.referFrm.name,"text"))
	{
		if (!Validateany(document.referFrm.name.value,'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '))
		{
			alert ("Special character/s are not allowed in the name");
			document.referFrm.name.focus();
			return false;
		}
	}


	if(IsEmpty(document.referFrm.your_mail,"text"))
	{
		alert("Please enter your email");
		referFrm.your_mail.focus();
		return false;
	}

	if(!IsEmail(referFrm.your_mail.value,"EMAIL "))
	{			
		referFrm.your_mail.focus();
		return false;
	}
	
	if (IsEmpty(document.referFrm.frnd_name1,"text") && IsEmpty(document.referFrm.frnd_name2,"text") && IsEmpty(document.referFrm.frnd_name3,"text") && IsEmpty(document.referFrm.frnd_name4,"text") && IsEmpty(document.referFrm.frnd_name5,"text") && IsEmpty(document.referFrm.frnd_mail1,"text") && IsEmpty(document.referFrm.frnd_mail2,"text") && IsEmpty(document.referFrm.frnd_mail3,"text") && IsEmpty(document.referFrm.frnd_mail4,"text") && IsEmpty(document.referFrm.frnd_mail5,"text"))
	{
	alert ("Please enter atleast one name and email")
	document.referFrm.frnd_name1.focus();
	return false;
	}

	//First

	if (!IsEmpty(document.referFrm.frnd_name1,"text"))
	{
		if (!Validateany(document.referFrm.frnd_name1.value,'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '))
		{
			alert ("Special character/s are not allowed in the friend name");
			document.referFrm.frnd_name1.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_mail1,'text'))
		{alert("Please enter your friend email");document.referFrm.frnd_mail1.focus();return false;}
	}

	if(!IsEmpty(document.referFrm.frnd_mail1,'text'))
	{
		if(!IsEmail(referFrm.frnd_mail1.value,"First Friend E-MAIL "))
		{			
			referFrm.frnd_mail1.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_name1,"text"))
		{
			alert ("Please enter your friend name");
			document.referFrm.frnd_name1.focus();
			return false;
		}
	}
	
	// Second Friend
	if (!IsEmpty(document.referFrm.frnd_name2,"text"))
	{
		if (!Validateany(document.referFrm.frnd_name2.value,'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '))
		{
			alert ("Special character/s are not allowed in the friend name");
			document.referFrm.frnd_name2.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_mail2,'text'))
		{alert("Please enter your friend email");document.referFrm.frnd_mail2.focus();return false;}
	}

	if(!IsEmpty(document.referFrm.frnd_mail2,'text'))
	{
		if(!IsEmail(referFrm.frnd_mail2.value,"First Friend E-MAIL "))
		{			
			referFrm.frnd_mail2.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_name2,"text"))
		{
			alert ("Please enter your friend name");
			document.referFrm.frnd_name2.focus();
			return false;
		}
	}
	//Third
	if (!IsEmpty(document.referFrm.frnd_name3,"text"))
	{
		if (!Validateany(document.referFrm.frnd_name3.value,'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '))
		{
			alert ("Special character/s are not allowed in the friend name");
			document.referFrm.frnd_name3.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_mail3,'text'))
		{alert("Please enter your friend email");document.referFrm.frnd_mail3.focus();return false;}
	}

	if(!IsEmpty(document.referFrm.frnd_mail3,'text'))
	{
		if(!IsEmail(referFrm.frnd_mail3.value,"First Friend E-MAIL "))
		{			
			referFrm.frnd_mail3.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_name3,"text"))
		{
			alert ("Please enter your friend name");
			document.referFrm.frnd_name3.focus();
			return false;
		}
	}
	//fourth
	if (!IsEmpty(document.referFrm.frnd_name4,"text"))
	{
		if (!Validateany(document.referFrm.frnd_name4.value,'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '))
		{
			alert ("Special character/s are not allowed in the friend name");
			document.referFrm.frnd_name4.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_mail4,'text'))
		{alert("Please enter your friend email");document.referFrm.frnd_mail4.focus();return false;}
	}

	if(!IsEmpty(document.referFrm.frnd_mail4,'text'))
	{
		if(!IsEmail(referFrm.frnd_mail4.value,"First Friend E-MAIL "))
		{			
			referFrm.frnd_mail4.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_name4,"text"))
		{
			alert ("Please enter your friend name");
			document.referFrm.frnd_name4.focus();
			return false;
		}
	}
	//fifth
	if (!IsEmpty(document.referFrm.frnd_name5,"text"))
	{
		if (!Validateany(document.referFrm.frnd_name5.value,'1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. '))
		{
			alert ("Special character/s are not allowed in the friend name");
			document.referFrm.frnd_name5.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_mail5,'text'))
		{alert("Please enter your friend email");document.referFrm.frnd_mail5.focus();return false;}
	}

	if(!IsEmpty(document.referFrm.frnd_mail5,'text'))
	{
		if(!IsEmail(referFrm.frnd_mail5.value,"First Friend E-MAIL "))
		{			
			referFrm.frnd_mail5.focus();
			return false;
		}

		if (IsEmpty(document.referFrm.frnd_name5,"text"))
		{
			alert ("Please enter your friend name");
			document.referFrm.frnd_name5.focus();
			return false;
		}
	}
}
