// JavaScript Document
// JavaScript Document
/*******************************************************************/
/***                                                             ***/
/***   commercial-estimate.js - JavaScript Form Validations	     ***/
/***   Author	 : Sundarrajan S.                                ***/
/***                                                             ***/
/***   Version   : 1.2                                           ***/
/***   Date      : 05.03.2009                                    ***/
/***                                                             ***/
/*******************************************************************/
function checkForm()
{
		var company_name=document.form1.company_name;
		var phone=document.form1.phone;
		var fax=document.form1.fax;
		var website=document.form1.website;
		var email=document.form1.email;
		var street=document.form1.street;
		var city=document.form1.city;
		var state=document.form1.state;
		var zip=document.form1.zip;
		var business_category=document.form1.business_category;
		var employees=document.form1.employees;
		var contact=document.form1.contact;
		var title=document.form1.title;
		var about=document.form1.about;
		var others=document.form1.others;
		
		if(empty_check(company_name.value,"Please enter Company Name")==0){company_name.focus();return false;}
		if(empty_check(phone.value,"Please enter Phone Number")==0){phone.focus();return false;}
		if(empty_check(fax.value,"Please enter Fax Number")==0){fax.focus();return false;}
		if(empty_check(website.value,"Please enter Website Address")==0){website.focus();return false;}
		if(!CheckValidUrl(website.value)){alert("Please enter a valid Website Address");website.focus();return false;}
		
		if(empty_check(email.value,"Please enter Email Id")==0){email.focus();return false;}
		if(email_check(email.value,"Please enter a valid Email Id")==0){email.focus();return false;}
		
		if(empty_check(street.value,"Please enter Street")==0){street.focus();return false;}
		if(empty_check(city.value,"Please enter City")==0){city.focus();return false;}
		if(empty_check(state.value,"Please enter State")==0){state.focus();return false;}
		if(empty_check(zip.value,"Please enter Zip Code")==0){zip.focus();return false;}
		if(empty_check(business_category.value,"Please enter a Business Category")==0){business_category.focus();return false;}
		if(empty_check(employees.value,"Please enter No. of Employees")==0){employees.focus();return false;}
		if(empty_check(contact.value,"Please enter Principal Contact Name")==0){contact.focus();return false;}
		if(empty_check(title.value,"Please enter Title")==0){title.focus();return false;}
		if(empty_check(about.value,"Please select how you learnt about ABA?")==0){about.focus();return false;}
		if(about.value=="Others")
		{
			if(empty_check(others.value,"If Others, Please Specify")==0){others.focus();return false;}
		}
		
  	if((!document.form1.promotion.checked) && (!document.form1.roster.checked) && (!document.form1.newmember.checked) && (!document.form1.webcoordination.checked) && (!document.form1.coop.checked) && (!document.form1.alliance.checked) && (!document.form1.referral.checked) && (!document.form1.monthly.checked) && (!document.form1.manage.checked) && (!document.form1.media.checked) && (!document.form1.identify.checked) && (!document.form1.plan.checked) && (!document.form1.record.checked) && (!document.form1.internal.checked) && (!document.form1.communication.checked))
	   {
		   	alert("Please select at least one ABA committee you wish to participate");
			return false;
	   }
	
	var chosen=0;
	for (i = 0; i <document.form1.boundary.length; i++) 
	{
	if (document.form1.boundary[i].checked)
	{
	chosen = 1;
	}
	}
	if (chosen==0)
	{
	alert("Please select whether your company is located within ABA boundaries");
	return false;
	}
	
	var chosen1=0;
	for (j = 0; j <document.form1.member.length; j++) 
	{
	if (document.form1.member[j].checked)
	{
	chosen1 = 1;
	}
	}
	if (chosen1==0)
	{
	alert("Please select whether you are a current member of SJSV Chamber of Commerce");
	return false;
	}
	
	var chosen2=0;
	for (k = 0; k <document.form1.approval.length; k++) 
	{
	if (document.form1.approval[k].checked)
	{
	chosen2 = 1;
	}
	}
	if (chosen2==0)
	{
	alert("Please select whether you wish to have your information posted in the members area");
	return false;
	}
	
}
	
	//Text only
function text_check(str)
{
		var s;
		var c=str;
		var fl = 0;
		/*if(c.charAt(0)==' ')
		{
			return 0;
		} */
		for(var i=0;i<c.length;i++)
		{
			s=c.charAt(i);
			if((s>='a' &&s<='z') || (s>='A' &&s<='Z')) 		
			{}
			else
			{
				fl = 1;
			}
		}
		
		if(fl == 1)
		return 0;
		else
		return 1;	
}
	
	//Email Check
function email_check(email,str)
{
		var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
		if(emailfilter.test(email)==false)
		{
			alert(str);
			return 0;			
		}
}
	
	//Empty Text Check
function empty_check(val_check,str)
{
	if(val_check==0)
	{
		alert(str);
		return 0;
	}else
	{
		return 1;
	}
	
}

function CheckValidUrl(strUrl)
{
        var RegexUrl = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
        var check=RegexUrl.test(strUrl);
		if(check)
		{
			return true;
		}else
		{
				//alert(strUrl.substring(0,4));
				if((strUrl.substring(0,4))=="www.")
				{
					
					var s;
					var c=strUrl;					
					var count=0;
				for(var i=0;i<c.length;i++)
				{
				s=c.charAt(i);
				if((s=='.')) 		
				{count++}			
				}
				if(count==2)
				{
					document.form1.website.value="http://"+strUrl;
					return true;
					
				}else
				{
					return false;
				}
				
				}else
				{	
					var s1;
					var c1=strUrl;					
					var count1=0;
				for(var i1=0;i1<c1.length;i1++)
				{
				s1=c1.charAt(i1);
				if((s1=='.')) 		
				{count1++}			
				}
				
				if(count1==1)
				{
					document.form1.website.value="http://www."+strUrl;
					return true;
					
				}else
				{
					return false;
				}
				}				
				
		}
}
