var randNum = 0;
function randNoDups(maxNum)
{
	tmpRand = randNum;
	while (tmpRand == randNum) {
		tmpRand = Math.round(Math.random()*maxNum);
	}
	randNum = tmpRand;
	return tmpRand;
}

function loadVCode() {

	var vCode, strVCode, vCodeLen;
	document.form1.txt1.value= randNoDups(99999);
	vCode = document.form1.txt1.value;
	
	vCodeLen = document.form1.txt1.value.length;
	//alert(vCodeLen);
	strVCode = "";
	for(i=1;i<=vCodeLen;i++) {
		strVCode = strVCode + "<img src='images/num/" + vCode.substring(i-1,i) + ".jpg'>"
	}
	document.getElementById("vCode").innerHTML = strVCode;
	document.form1.verifycode.value = ""; 
}

	function validateEmail(emailid)
		{
			if (emailid.charAt(0) == ' ')
			{
			alert("Email cannot have spaces");
			return false;
			}
			emailid = cutChars(emailid, " ");
			if(cutChars(emailid, ' ') == '')
			{
				return true;
			}
			at = emailid.indexOf("@");
			if(at <= -1) //one @ shd be there, not as the 1st char tho'
			{
				alert("Invlaid Emailid - no @");
				return false;
			}
			at1 = emailid.indexOf("@", at + 1);
			if(at1 != -1) //only one @ shd be there
			{
				alert("Invlaid Emailid - only one @");
				return false;
			}
			dot = emailid.indexOf(".", at + 1);
			if(dot - at < 2) //. shd be present, with atleast 1 char after @
			{
				alert("Invlaid Emailid - @.");
				return false;
			}
			if(emailid.indexOf(".@") != -1) //. shd not be just b4 @
			{
				alert("Invlaid Emailid - .@");
				return false;
			}
			if(emailid.indexOf("..") != -1)
			{
				alert("Invlaid Emailid - ..");
				return false;
			}
			if(emailid.charAt(emailid.length - 1) == "." || emailid.charAt(emailid.length - 2) == ".")
			{
				alert("Invlaid Emailid - last two chars");
				return false;
			}
			return true;
		}
	function cutChars(s, c)
		{
			var s1, x;
			x = s1 = "";
			l = s.length;
			for(i = 0; i < l; i++)
				if((x = s.charAt(i)) != c)
					s1+=x;
			return s1;
		}
		function isCharsInBag (s, bag)
		{
			var i;
			for (i = 0; i < s.length; i++)
			{

				var c = s.charAt(i);
				if (bag.indexOf(c) == -1) return false;
			}
			return true;
		}
function ValidateForm() {
	var vCode = document.form1.verifycode.value;
	var strVCode = document.form1.txt1.value;
	var str = document.form1;
	if(str.urname.value=='')
	{
	   alert("Your Name field should not be Empty");
	   str.urname.focus();
	   return false;
	}
	if(!isCharsInBag(str.urname.value ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 '"))
	{
	   alert("Please Enter Your Name. No special Characters allowed");
	   str.urname.focus();
       return false;    
	}
	
	if(str.uremail.value=='')
	{
	   alert("Your Email field should not be Empty");
	   str.uremail.focus();
	   return false;
	}

if(!isCharsInBag(str.uremail.value ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@._ '"))
	{
	   alert("Please Enter Valid Your Email. No special Characters allowed");
	   str.uremail.focus();
       return false;    
	}

	else if(!validateEmail(str.uremail.value))
			{
				str.uremail.focus();
				return false;
			}
			
			
				if(str.fname.value=='')
	{
	   alert("Friend Name field should not be Empty");
	   str.fname.focus();
	   return false;
	}
	if(!isCharsInBag(str.fname.value ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 '"))
	{
	   alert("Please Enter Valid Friend Name. No special Characters allowed");
	   str.fname.focus();
       return false;    
	}
	
	if(str.femail.value=='')
	{
	   alert("Friend Email field should not be Empty");
	   str.femail.focus();
	   return false;
	}
if(!isCharsInBag(str.femail.value ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@._ '"))
	{
	   alert("Please Enter Valid Friend Email. No special Characters allowed");
	   str.femail.focus();
       return false;    
	}
	else if(!validateEmail(str.femail.value))
			{
				str.femail.focus();
				return false;
			}
	if(vCode!=strVCode)
	{
		alert("Enter Verification code field should not be Empty");
		str.verifycode.focus();
		return false;
		
	}
	else
	{
		return true;
	}
}
