var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+" + "," + "(" + ")";
var minDigitsInIPhoneNumber = 10;
// JavaScript Document
function checkEmail(frmadvertise) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.frmadvertise.email.value))
	{
	return true;
	}
	alert("Invalid E-mail Address! Please re-enter.")
	return false;
}
function validate()
{
	if(document.frmadvertise.name.value == "")
	{
		alert ("Please enter the Name");
		document.frmadvertise.name.focus();
		return false;
	}
	if(document.frmadvertise.companyname.value == "")
	{
		alert ("Please enter the Company Name");
		document.frmadvertise.companyname.focus();
		return false;
	}
	if(document.frmadvertise.email.value == "")
	{
		alert ("Please enter email id");
		document.frmadvertise.email.focus();
		return false;
	}
	if(checkEmail(document.frmadvertise.email.value)==false)
	{
		return false;
	}
	if(document.frmadvertise.phone.value=="")
	{
		alert("Please Enter a Phone Number...");
		document.frmadvertise.phone.focus();
		return false;
	}
	if(document.frmadvertise.phone.value!="")
	{
	if (checkInternationalPhone(document.frmadvertise.phone.value)==false)
	{
		alert("Please Enter a Valid Phone Number...");
		//Phone.value=""
		document.frmadvertise.phone.focus();
		return false;
	}
	}
	if(document.frmadvertise.fax.value!="")
	{
	if (isInteger(document.frmadvertise.fax.value)==false)
	{
		alert("Please Enter a Valid Fax Number...");
		//Phone.value=""
		document.frmadvertise.fax.focus();
		return false;
	}
	}
	if(document.frmadvertise.code.value=='')
	{
			alert("Enter the code");
			document.frmadvertise.code.focus();
			return false;
	}
	if(document.frmadvertise.randomimage.value!=document.frmadvertise.code.value)
	{
			alert("Enter the true code");
			document.frmadvertise.code.focus();
			return false;
	}
	return true;
}
function randomString() 
{
	//alert('avadhesh');
	document.getElementById('rid').style.display='block';
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
	var string_length = 5;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	document.frmadvertise.randomimage.value = randomstring;	
}
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) 
		return false;
  	 }
    
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
