var formSubmitted = false;
function Validate(theForm)
{
	
	var errMesg = "";
	var diplayMesg = "";
	// Check all the Required Information fields.
	//{
	var Q = ""; // this block determines lifespan of Q
	
	
	if (isWhitespace(theForm.name.value))
	{
		Q += "  Name\n";
	}
	if (isWhitespace(theForm.company.value))
	{
		Q += "  Company\n";
	}

	if (isWhitespace(theForm.address.value))
	{
		Q += "  Address\n";
	}	
	else if(theForm.address.value.length>500)
	{
		errMesg += "Address should not be greater than 500 characters\n";
	}
	if(isWhitespace(theForm.city.value))
	{
		Q += "  City or Town\n";
	}
	if (theForm.country.value==0)
	{
		Q += "  Country\n";
	}
	if (isWhitespace(theForm.zip.value))
	{
		Q += "  Post Code\n";
	}
	
	if (isWhitespace(theForm.phone.value))
	{
		Q += "  Telephone\n";
	}
	else if(!isCharsInBag(theForm.phone.value,"0123456789- "))
	{
	  errMesg += "Telephone contains Invalid Characters\n";
	}
	if (isWhitespace(theForm.email.value))
	{
		Q += "  Email \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(theForm.email1.value!="")
	{
		if(echeck(theForm.email1.value))
		{
		    alert("Invalid Email Address");
			theForm.email1.focus();
			 return false;
		}	
		else if(!isCharsInBag( theForm.email1.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-"))
		{
		  alert("Email Address contains Invalid Characters");
		  theForm.email1.focus();
		   return false;
		}
	}

	if(theForm.email2.value!="")
	{
		if(echeck(theForm.email2.value))
		{
		 alert("Invalid Email Address");
		 theForm.email2.focus();
		 return false;
		}	
		else if(!isCharsInBag( theForm.email2.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-"))
		{
		   alert("Email Address contains Invalid Characters");
		   theForm.email2.focus();
		    return false;
		}
	}
	
	if(theForm.email3.value!="")
	{
		if(echeck(theForm.email3.value))
		{
		    alert("Invalid Email Address");
			theForm.email3.focus();
			return false;
		}	
		else if(!isCharsInBag( theForm.email3.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-"))
		{
		   alert("Email Address contains Invalid Characters");
		   theForm.email3.focus();
		    return false;
		}
	}
	
	if (!isWhitespace(theForm.distributors.value))
	{
		var char_count = theForm.distributors.value.length;
		var fullStr = theForm.distributors.value + " ";
		var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
		var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
		//var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
		var non_alphanumerics_rExp = rExp = /\s+/gi;
		var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
		var splitString = cleanedStr.split(" ");
		var word_count = splitString.length -1;
		if (fullStr.length <2) {
		word_count = 0;
		}
		//alert(word_count);
		if (word_count > 35) {
		errMesg += "List Distributors should be 35 Words\n";
		}
	}

	if (isWhitespace(theForm.number_titles.value) && isWhitespace(theForm.number_shelves.value) && isWhitespace(theForm.number_panels.value))
	{
		Q += "  Number of Titles to be exhibited \n";
	}
	else if ((theForm.number_titles.value + theForm.number_shelves.value + theForm.number_panels.value) <= 0) {
		Q += "  Number of Titles to be exhibited \n";
	}
	
	if(document.form1.number_titles.value!="")
	{
		
		if(isNaN(document.form1.number_titles.value))
		{
			errMesg += " Number of Titles to be exhibited should be a number\n";
		}
	}
	if ( Q.length > 0 )
	{
		diplayMesg = "Please provide Valid values for\n" + Q ;
		//errMesg += "Please provide Valid values for\n" + Q ;
	
	}
	
	if (errMesg == "" && diplayMesg == "")
	{
		if (theForm.terms.checked==false)
		{
			alert("Please read and understand Terms and Conditions.");
			return false;
		}
		else {
			if(formSubmitted == false) {
				formSubmitted = true;
				return true;
			}
			else {
				alert('Your transaction is in progress. Please wait !');
				return false;
			}
		}
	}
	else
	{
		if(diplayMesg!="")
		{
			alert(diplayMesg);
			return false;			
		}
		else
		{
			alert(errMesg);
			return false;
		}	
	}
}


function openWin(url)
{
	var j=window.open(url,"jj","width=800,height=600,scrollbars=1")
}

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 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;					
}

function numeralsOnly1(evt)
{
    
	evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        alert("Enter numerals only in Quantity field.");
        return false;
	}
    return true;
}

function booth1()
{
	
	if(document.form1.booth.value==0 || document.form1.booth.value==2)
	{
		document.form1.booth_number.readOnly=true;
	}
	else
	{
		document.form1.booth_number.readOnly=false;
	}
}

function check()
{
	if(document.form1.ititles.value!="")
	{
		
		if(isNaN(document.form1.ititles.value))
		{
			alert("Enter numerals only");
		}
		else
		{
			if(document.form1.ititles.value>50)
			{
				alert("Number of Titles to be exhibited should not be more than 50");
			}
		}
	}
	
}

function check1()
{
	if(document.form1.ptitles.value!="")
	{
		document.form1.ititles.readOnly=true;
		if(isNaN(document.form1.ptitles.value))
		{
			alert("Enter numerals only");
		}
		else
		{
			if(document.form1.ptitles.value>10)
			{
					alert("Number of Publisher Shelves should not be more than 10");
			}
		}
	}
	else
	{
		document.form1.ititles.readOnly=false;
	}
}
function checkbooth()
{
	if(document.form1.booth2[0].checked || document.form1.booth2[2].checked)
	{
		document.form1.booth_number.value="";
		document.form1.booth_number.readOnly=true;
	}
}

function checkinclude()
{
	
	if(document.form1.includeKeyContact[0].checked)
	{
		document.form1.name1.value=document.form1.name.value;
		document.form1.title1.value=document.form1.jobtitle.value;
		document.form1.email1.value=document.form1.email.value;
		document.form1.name1.readOnly=true;
		document.form1.title1.readOnly=true;
		document.form1.email1.readOnly=true;
	}
	if(document.form1.includeKeyContact[1].checked)
	{
		document.form1.name1.value="";
		document.form1.title1.value="";
		document.form1.email1.value="";
		document.form1.name1.readOnly=false;
		document.form1.title1.readOnly=false;
		document.form1.email1.readOnly=false;
	}
}

function change1()
{
	if(document.form1.personname.value!="")
	{
		if(document.form1.include[0].checked)
		{
			document.form1.name1.value=document.form1.personname.value;
			document.form1.name1.readOnly=true;
		}
	}
	else
	{
			document.form1.name1.value="";
			document.form1.name1.readOnly=false;
	}
	
	
}

function change2()
{
	if(document.form1.jobtitle.value!="")
	{
		if(document.form1.include[0].checked)
		{
			document.form1.ctitle1.value=document.form1.jobtitle.value;
			document.form1.ctitle1.readOnly=true;
		}
	}
	else
	{
			document.form1.ctitle1.value="";
			document.form1.ctitle1.readOnly=false;
	}
	
	
}

function change3()
{
	
	if(document.form1.email.value!="")
	{
		if(document.form1.include[0].checked)
		{
			document.form1.email1.value=document.form1.email.value;
			document.form1.email1.readOnly=true;
		}
	}
	else
	{
			document.form1.email1.value="";
			document.form1.email1.readOnly=false;
	}

}

function disableOtherInputs(id, field1, field2) {
	if(!isWhitespace(document.getElementById(id).value)) {
		document.getElementById(field1).value = '';
		document.getElementById(field2).value = '';
		
		document.getElementById(field1).disabled = true;
		document.getElementById(field2).disabled = true;
	}
	else {
		document.getElementById(field1).disabled = false;
		document.getElementById(field2).disabled = false;
	}
}
