var whitespace = " \t\n\r"

function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}
function isWhitespace (s)
{  var i;
	// Is s empty?
	if (isEmpty(s)) return true;

	 // Search through string's characters one by one
	 // until we find a non-whitespace character.
	 // When we do, return false; if we don't, return true.
	 for (i = 0; i < s.length; i++)
	 {   
		 // Check that current character isn't whitespace.
		 var c = s.charAt(i);
 
		 if (whitespace.indexOf(c) == -1) return false;
	 }
 
	 // All characters are whitespace.
	 return true;
}
function BuildStr(s, s1)
{
	  if (s.length> 0) s = s + "\n";
	  s = s + s1;
	  return s;
}

function isCharsInBag (s, bag)
{  
  var i;
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {   
	  // Check that current character isn't whitespace.
	  var c = s.charAt(i);
	  if (bag.indexOf(c) == -1) return false;
  }
  return true;
}


function Validate(theForm)
{
	var errMesg = "";
	var diplayMesg = "";

	// Check all the Required Information fields.
	{
	var Q = ""; // this block determines lifespan of Q
	if (isWhitespace(theForm.company.value))
		{
		Q += "  Company Name\n";
		}
	if (isWhitespace(theForm.contactperson.value))
		{
		Q += "  Contact Person\n";
		}
	if (isWhitespace(theForm.address.value))
		{
		Q += "  Address\n";
		}
	if (theForm.address.value!="" && theForm.address.value.length>500)
		{
		 errMesg += "Address should not be greater than 500 characters\n";
		}
	if (isWhitespace(theForm.city.value))
		{
		Q += "  City\n";
		}
	if (isWhitespace(theForm.zipcode.value))
		{
		Q += "  Zip Code\n";
		}
	/*if (theForm.state.value=="")
		{
		Q += "  State\n";
		}*/
	if (theForm.country.value==0)
		{
		Q += "  Country\n";
		}
	if (theForm.country.value==1 && theForm.state.value=="")
		{
		Q += "  State\n";
		}
	if (isWhitespace(theForm.email.value))
		{
		Q += "  Email Address\n";
		}
	else if(echeck(theForm.email.value))
	{
	   errMesg += "Invalid Email Address\n";
	}	
	else if(!isCharsInBag( theForm.email.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-"))
	{
	  errMesg += "Email Address contains Invalid Characters\n";
	}
	if (isWhitespace(theForm.telephone.value))
		{
		Q += "  Telephone\n";
		}	
	//if (isWhitespace(theForm.fax.value))
	//	{
	//	Q += "  Fax\n";
	//	}				
	if (isWhitespace(theForm.banner_size.value))
		{
		Q += "  Banner Size\n";
		}	
	if (isWhitespace(theForm.selectpage.value))
		{
		Q += "  Select Page\n";
		}	
	if (isWhitespace(theForm.bannerfile.value))
		{
		Q += "  Banner\n";
		}	
	if (isWhitespace(theForm.link.value))
		{
		Q += "  Link\n";
		}

	if(theForm.termschk)
		{
		if (theForm.termschk.checked==false)
		{
			Q += "  Terms and Conditions \n";
		}
		}

	if ( Q.length > 0 )
		{
		diplayMesg = "Please provide Valid values for\n" + Q ;
		//errMesg += "Please provide Valid values for\n" + Q ;
		}
	}

 
	if (errMesg == "" && diplayMesg == "")
	{
		
			theForm.submit();
		
	}
	
	else
	{
	
			if(diplayMesg!="")
			{
				alert(diplayMesg);
				return false;			
			}
			else
			{
				alert(errMesg);
				return false;
			}	
	}
}

function echeck(str) 
	{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return true;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			return true;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return true;
   		}

		 if (str.indexOf(at,(lat+1))!=-1){
			return true;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return true;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			return true;
		 }
		
		 if (str.indexOf(" ")!=-1){
			return true;
		 }

 		 return false;					
	}

var sizeofbanner=new Array();
sizeofbanner[0]="Banner AD 468 x 60";
sizeofbanner[1]="Banner AD 180 x 150";
sizeofbanner[2]="Banner AD 120 x 90";

var pagebanner=new Array();

pagebanner[0]=new Array();
pagebanner[0][0]="Bottom banner on all pages";

pagebanner[1]=new Array();
pagebanner[1][0]="Browse Books";
pagebanner[1][1]="Tell a Friend";
pagebanner[1][2]="Registration";
pagebanner[1][3]="Login";
pagebanner[1][4]="My book shelf";

pagebanner[2]=new Array();
pagebanner[2][0]="Show calendar details";


function fill_selectpage_combo(theForm,firstcombo,secondcombo)
{
	
	secondcombo.length=0;	
		
	for(i=0;i<=3;i++)
		{
			if(sizeofbanner[i]==firstcombo.value)
			{
				chknum=i;
			}
		}	
	

	for(j=0;j<pagebanner[chknum].length;j++)
	{
		var combo2=new Array(pagebanner[chknum].length);
		secondcombo.options[j] =new Option(pagebanner[chknum][j],pagebanner[chknum][j]);		
		secondcombo.options[0].selected=true
	}	
}
