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.name.value=='')
	{
	   alert("Name field should not be Empty");
	   str.name.focus();
	   return false;
	}
	
	if(!isCharsInBag(str.name.value ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 '"))
	{
	   alert("Please Enter Valid Name");
	   str.name.focus();
       return false;    
	}
	
	if(str.subj.value=='')
	{
	   alert("Subject field should not be Empty");
	   str.subj.focus();
	   return false;
	}
	if(str.email.value=='')
	{
	   alert("Email field should not be Empty");
	   str.email.focus();
	   return false;
	}
	if(!isCharsInBag(str.email.value ,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@_.-"))
	{
	   alert("Please Enter Valid Email. No special Characters allowed");
	   str.email.focus();
       return false;    
	}
	else if(!validateEmail(str.email.value))
			{
				str.email.focus();
				return false;
			}
	if(vCode!=strVCode)
	{
		alert("Enter Verification code field should not be Empty");
		str.verifycode.focus();
		return false;
		
	}
	else
	{
		return true;
	}
}

function textonly(e){
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
    var AllowRegex  = /^[\ba-zA-Z\s-\.]$/;
    if (AllowRegex.test(character)) return true;
    return false;
}

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function textonly(e){
var code;
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
    var AllowRegex  = /^[\ba-zA-Z\s-\.]$/;
    if (AllowRegex.test(character)) return true;
    return false;
}

