// JavaScript Document
var phoneNumberDelimiters = "()- ";

var validWorldPhoneChars = phoneNumberDelimiters + "+";

function validateForm() 
{

	var status = true;
	var txtEmail = "";
	var txtFirstName = "";
	var txtLastName = "";
	var txtOrganisation = "";
	var isEmailValid = false;
	var isFirstNameEmpty = false;
	var isLastNameEmpty = false;
	var isOrganisationEmpty = false;
	var message = "";
	
    txtEmail = document.getElementById("Email").value;
    txtFirstName = document.getElementById("firstname").value;
	txtLastName = document.getElementById("lastname").value;
	txtIndustrySector = document.getElementById("industrySector").value;
	
	
	isEmailValid = checkEmail(txtEmail);
	
	isFirstNameEmpty = isEmpty(txtFirstName);
	
	isLastNameEmpty = isEmpty(txtLastName);
	
	isIndustrySectorEmpty = isEmpty(txtIndustrySector);
	
	
   if(isFirstNameEmpty)
   {
	   message = "<li style='color:black'>First name cannot be left blank</li>";
		
	   document.getElementById("firstname").style.border="1px solid red";
		status = false;
	   
   }
   
   	
   if(isLastNameEmpty)
   {
	   message = message+"<li style='color:black'>Last name cannot be left blank</li>";
		
	   document.getElementById("lastname").style.border="1px solid red";
		status = false;
	   
   }
   
  
  
   if(!isEmailValid)
   {
   		message = message+ "<li style='color:black'>Email is not valid</li>";
		
		document.getElementById("Email").style.border="1px solid red";
		status = false;
	}

	 if(isIndustrySectorEmpty)
   {
   		message = message+ "<li style='color:black'>Please select at least 1 industry sector</li>";
		
		
		document.getElementById("industrySector").style.border="1px solid red";
		status = false;
	}
	
	
	
	
   if(status==false)
   {
	  
			 Ext.onReady(function() {
					Ext.MessageBox.show({
									title:"Warning",
									msg:'<ul style="color:black;padding-left:50px">'+message+'</ul>',
									buttons:Ext.MessageBox.OK,
									icon:Ext.MessageBox.WARNING,
									width:350,
									height:250				
									});
					
					});
		return false;
	}
	else
	{
	
		document.getElementById("frmNewsletter").submit();
	
	}	
	
	
	
}


function validateFormConf()
{

	var status = true;
	var txtEmail = "";
	var txtFirstName = "";
	var txtLastName = "";
	var txtOrganisation = "";
	var isEmailValid = false;
	var isFirstNameEmpty = false;
	var isLastNameEmpty = false;
	var isOrganisationEmpty = false;
        var isIndustrySectorEmpty = false;
        var txtIndustrySector = "";

	var message = "";

    txtEmail = document.getElementById("Email").value;
    txtFirstName = document.getElementById("firstname").value;
	txtLastName = document.getElementById("lastname").value;
	txtIndustrySector = document.getElementById("industrySectorConf").value;


	isEmailValid = checkEmail(txtEmail);

	isFirstNameEmpty = isEmpty(txtFirstName);

	isLastNameEmpty = isEmpty(txtLastName);

	isIndustrySectorEmpty = isEmpty(txtIndustrySector);


   if(isFirstNameEmpty)
   {
	   message = "<li style='color:black'>First name cannot be left blank</li>";

	   document.getElementById("firstname").style.border="1px solid red";
		status = false;

   }


   if(isLastNameEmpty)
   {
	   message = message+"<li style='color:black'>Last name cannot be left blank</li>";

	   document.getElementById("lastname").style.border="1px solid red";
		status = false;

   }



   if(!isEmailValid)
   {
   		message = message+ "<li style='color:black'>Email is not valid</li>";

		document.getElementById("Email").style.border="1px solid red";
		status = false;
	}

	 if(isIndustrySectorEmpty)
   {
   		message = message+ "<li style='color:black'>Please select at least 1 industry sector</li>";


		document.getElementById("industrySectorConf").style.border="1px solid red";
		status = false;
	}




   if(status==false)
   {

			 Ext.onReady(function() {
					Ext.MessageBox.show({
									title:"Warning",
									msg:'<ul style="color:black;padding-left:50px">'+message+'</ul>',
									buttons:Ext.MessageBox.OK,
									icon:Ext.MessageBox.WARNING,
									width:350,
									height:250
									});

					});
		return false;
	}
	else
	{

		document.getElementById("frmExpo").submit();
                return true;
	}



}





function isEmpty(elementValue)
{
	
	elementValue = trim(elementValue);
	
	
    if((elementValue == '') || (elementValue == null) || (elementValue == ' '))
	{
		
	   return true;	
		
	}
	else
	{
		
	   return false;	
		
	}
	
	
}


function validateUpdate()
{
	 email = document.getElementById("Email").value;
	 isValidEmail = checkEmail(email);
	 message ="";
	  if(!isValidEmail)
  	  {
			message = message+ "<li style='color:black'>Email is not valid</li>";
			
			document.getElementById("Email").style.border="1px solid red";
			status = false;
			
				 Ext.onReady(function() {
						Ext.MessageBox.show({
										title:"Warning",
										msg:'<ul style="color:black;padding-left:50px">'+message+'</ul>',
										buttons:Ext.MessageBox.OK,
										icon:Ext.MessageBox.WARNING,
										width:350,
										height:250				
										});
						
						});
			return false;
		
		
		}
		else
		{
		
			document.getElementById("frmNewsletter").submit();
		}
	 
}




function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}



function checkEmail(email)
{
	
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    isMailAdd = emailPattern.test(email); 
	
	return isMailAdd;


}




function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}




function validateMembershipForm()
{
	
	var status = true;
	var txtEmail = "";
	var txtFirstName = "";
	var txtCompany = "";
	var txtLastname = "";
	var txtPhone = "";
	
	var isEmailValid = false;
	var isFirstNameEmpty = false;
	var isCompanyEmpty = false;
	var isLastNameEmpty = false;
	var isPhoneValid = false;
	var message = "";
	
    txtEmail = document.getElementById("Email").value;
    txtFirstName = document.getElementById("firstname").value;
	txtLastname = document.getElementById("lastname").value;
	txtCompany = document.getElementById("company").value;
	txtPhone = document.getElementById("phonenumber").value;
	
	isEmailValid = checkEmail(txtEmail);
    
	txtFirstName = trim(txtFirstName);
    isFirstNameEmpty = isEmpty(txtFirstName);
	
	txtLastname = trim(txtLastname);
    isLastNameEmpty = isEmpty(txtLastname);
	
    isPhoneValid = checkPhone(txtPhone);
	
	txtCompany = trim(txtCompany);
	isCompanyEmpty = isEmpty(txtCompany);	
	
	
   if(isFirstNameEmpty)
   {
	   message = message+"<li style='color:black'>First name cannot be left blank</li>";
		
	   document.getElementById("firstname").style.border="1px solid red";
		status = false;
	   
   }
   
   if(isLastNameEmpty)
   {
	   message = message+"<li style='color:black'>Last name cannot be left blank</li>";
		
	   document.getElementById("lastname").style.border="1px solid red";
		status = false;
	   
   }
   
     if(isCompanyEmpty)
   {
	   message = message+"<li style='color:black'>Company cannot be left blank</li>";
		
	   document.getElementById("company").style.border="1px solid red";
		status = false;
	   
   }
   
  
   if(!isEmailValid)
   {
   		message = message+ "<li style='color:black'>Email is not valid</li>";
		
		document.getElementById("Email").style.border="1px solid red";
		status = false;
	}

	if(!isPhoneValid)
   {
   		message = message+ "<li style='color:black'>Phone numbers must contain only numbers or in the following format +(##)-(##)-(###)-(#####)</li>";
		
		document.getElementById("phonenumber").style.border="1px solid red";
		status = false;
	}
	
	
   if(status==false)
   {
	  
			 Ext.onReady(function() {
					Ext.MessageBox.show({
											title:"Warning",
											msg:'<ul style="color:black;padding-left:50px">'+message+'</ul>',
											buttons:Ext.MessageBox.OK,
											icon:Ext.MessageBox.WARNING,
											width:350,
											height:250				
									});
					
					});
		return false;
	}
	else
	{
	
		document.getElementById("frmMembership").submit();
	
	}	
	
	
	
}

function checkPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	
   return (isInteger(s) && s.length >1);

}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
