// JavaScript Document

var xmlHttp;
function fnXMLRequest()
{
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}

//To populate city dropdown using ajax
function populatecategory(categoryid)
{
	
		//alert(categoryid);
		url = "comp_action.php?action=getsubcategory&id="+categoryid
		fnXMLRequest();	
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange = function(){
			
		if(xmlHttp.readyState==4)
		{
				//alert(xmlHttp.readyState);
				document.getElementById('subcatspan').innerHTML = "<select name='subcategory' id='subcategory' class='join-drpdwn'>"+xmlHttp.responseText+"</select>";
				//alert(xmlHttp.responseText);
		}
			
	}
		xmlHttp.send(null);
		return true;

	
	
}

function populatestate(countryid)
{
	
		url = "comp_action.php?action=getstate&id="+countryid
		fnXMLRequest();
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange = function(){
			//alert(xmlHttp.readyState)
		;if(xmlHttp.readyState==4)
		{
				//alert(xmlHttp.responseText);
				document.getElementById('statespan').innerHTML = "<select name='state1' id='state1' class='join-drpdwn'>"+xmlHttp.responseText;	
		}
			
	}
		xmlHttp.send(null);
		return true;
	
}


function populatestatesearch(countryid)
{
	
		url = "comp_action.php?action=getstate&id="+countryid
		fnXMLRequest();
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange = function(){
			//alert(xmlHttp.readyState)
		;if(xmlHttp.readyState==4)
		{
				//alert(xmlHttp.responseText);
				document.getElementById('statespan').innerHTML = "<select name='state1' id='state12' class='compdrpdown'>"+xmlHttp.responseText;	
		}
			
	}
		xmlHttp.send(null);
		return true;

	
	
}
function populatestatesearch_joinus(countryid)
{
	
		url = "comp_action.php?action=getstate&id="+countryid
		fnXMLRequest();
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange = function(){
			//alert(xmlHttp.readyState)
		;if(xmlHttp.readyState==4)
		{
				//alert(xmlHttp.responseText);
				document.getElementById('statespan12').innerHTML = "<select name='state_searchjoin' id='state_searchjoin' class='compdrpdownlowwidth'>"+xmlHttp.responseText;	
		}
			
	}
		xmlHttp.send(null);
		return true;

	
	
}

function populatestateinnersearch(countryid)
{
	
		url = "comp_action.php?action=getstate&id="+countryid
		fnXMLRequest();
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange = function(){
			//alert(xmlHttp.readyState)
		;if(xmlHttp.readyState==4)
		{
				//alert(xmlHttp.responseText);
				document.getElementById('statespan').innerHTML = "<select name='state1' id='state1' class='rtform-drpdown'>"+xmlHttp.responseText;	
		}
			
	}
		xmlHttp.send(null);
		return true;

	
	
}

function check_url(url) {
     var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
     if (tomatch.test(url))
     {
         return true;
     }

     return false; 
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
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){
                  //alert("Please enter a valid e-mail.")
                  return false
               }
               if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                  //alert("Please enter a valid e-mail.")
                  return false
               }
               if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                   //alert("Please enter a valid e-mail.")
                   return false
               }
                if (str.indexOf(at,(lat+1))!=-1){
                   //alert("Please enter a valid e-mail.")
                   return false
                }
                if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                   //alert("Please enter a valid e-mail.")
                   return false
                }
                if (str.indexOf(dot,(lat+2))==-1){
                  // alert("Please enter a valid e-mail.")
                   return false
                }
                if (str.indexOf(" ")!=-1){
                //   alert("Please enter a valid e-mail.")
                   return false
                }
				
       }
	   
	   
function populatestatenew(countryid)
{
	
		url = "comp_action.php?action=getstate&id="+countryid
		fnXMLRequest();
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange = function(){
			//alert(xmlHttp.readyState)
		;if(xmlHttp.readyState==4)
		{
				//alert(xmlHttp.responseText);
				document.getElementById('statespannew').innerHTML = "<select name='state' id='state' class='join-drpdwn'>"+xmlHttp.responseText;	
		}
			
	}
		xmlHttp.send(null);
		return true;
	
}
function submit_frm(option)
{
  //validation
  var error=0;

	  
  var field=document.getElementById('email').value; 
  var field_div=document.getElementById('email_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter Email</font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   if (echeck(field)==false){field_div.innerHTML="<font color='red'>Please enter valid email address</font>"; error=1}
	  else {
	  field_div.innerHTML=""; 
		   }
	   }
	   
  var password=document.getElementById('password').value; 
  var password_div=document.getElementById('password_div'); 	
  if(password=='') {password_div.innerHTML="<font color='red'>Password cannot be Blank </font>"; error=1}
  else {
  password_div.innerHTML=""; 
	   }
	   
  var confirm_password=document.getElementById('confirm_password').value; 
  var confirm_password_div=document.getElementById('confirm_password_div'); 	
  if(confirm_password=='') {confirm_password_div.innerHTML="<font color='red'>Confirm Password cannot be Blank</font>"; error=1}
  else {
  confirm_password_div.innerHTML=""; 
  if(password!=confirm_password) {
   error=1;
   confirm_password_div.innerHTML="<font color='red'>Password mismatch</font>";
   }else confirm_password_div.innerHTML="";
	   }
	   
  var name=document.getElementById('name').value; 
  var name_div=document.getElementById('name_div'); 	
  if(name=='') {name_div.innerHTML="<font color='red'>Please enter your Name</font>"; error=1}
  else {
  name_div.innerHTML=""; 
	   }
	   
  var name=document.getElementById('designation').value; 
  var name_div=document.getElementById('designation_div'); 	
  if(name=='') {name_div.innerHTML="<font color='red'>Please enter your Designation</font>"; error=1}
  else {
  name_div.innerHTML=""; 
	   }
	   
  var name=document.getElementById('phone').value; 
  var name_div=document.getElementById('phone_div'); 	
  if(name=='') {name_div.innerHTML="<font color='red'>Please enter your Phone</font>"; error=1}
  else {
	  if(name.length!=10)
	  {
		 name_div.innerHTML="<font color='red'>Please Enter Valid Phone Number</font>"; error=1
	  }
	  else
	  {
  name_div.innerHTML=""; 
	  }
	 }
	   
  var name=document.getElementById('company_name').value; 
  var name_div=document.getElementById('company_name_div'); 	
  if(name=='') {name_div.innerHTML="<font color='red'>Please enter Company Name</font>"; error=1}
  else {
  name_div.innerHTML=""; 
	   }
  if(document.getElementById("otherecatdiv").style.display!='none')
  {
		//alert("herer");
		var cat_div=document.getElementById('category_div');
		var subcat_div=document.getElementById('subcategory_div');
		cat_div.innerHTML=""; 
		subcat_div.innerHTML=""; 
		var address1=document.getElementById('other_category').value; 
		  var address1_div=document.getElementById('otherecat_div'); 	
		  if(address1=='') {address1_div.innerHTML="<font color='red'>Please enter other category</font>"; error=1}
		  else {
		  address1_div.innerHTML=""; 
			   }
		
		  var address2=document.getElementById('other_subcategory').value; 
		  var address2_div=document.getElementById('subcateg_div'); 	
		  if(address2=='') {
			 
				  address2_div.innerHTML="<font color='red'>Please enter sub category</font>"; error=1
			
			  }
		  else {
		  address2_div.innerHTML=""; 
			   }
  }
  else
  {
		  var address1=document.getElementById('category').value; 
		  var address1_div=document.getElementById('category_div'); 	
		  if(address1=='') {address1_div.innerHTML="<font color='red'>Please select category</font>"; error=1}
		  else {
		  address1_div.innerHTML=""; 
			   }
		
		  var address2=document.getElementById('subcategory').value; 
		  var address2_div=document.getElementById('subcategory_div'); 	
		  if(address2=='') {
			 
				  address2_div.innerHTML="<font color='red'>Please select sub category</font>"; error=1
			
			  }
		  else {
		  address2_div.innerHTML=""; 
			   }
  }

  var postcode=document.getElementById('country').value; 
  var postcode_div=document.getElementById('country_div'); 	
  if(postcode=='') {postcode_div.innerHTML="<font color='red'>Please select country</font>"; error=1}
  else {
  postcode_div.innerHTML=""; 
	   }

   
  var phone=document.getElementById('state1').value; 
  var phone_div=document.getElementById('state_div');
  //alert(phone);
  if(phone=='') {phone_div.innerHTML="<font color='red'>Please select state</font>"; error=1}
  else {
  phone_div.innerHTML=""; 
	   }
	   
	var field=document.getElementById('city').value; 
  var field_div=document.getElementById('city_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter city</font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   }

/*	var field=document.getElementById('comp_logo').value; 
  var field_div=document.getElementById('comp_logo_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please select logo </font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   }
*/	
   	var field=document.getElementById('comp_weburl').value; 
  var field_div=document.getElementById('comp_weburl_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter company URL </font>"; error=1}
  else {
	  if(!check_url(field))
	  {field_div.innerHTML="<font color='red'>Please enter valid URL </font>"; error=1}
	  else
	  {
  	field_div.innerHTML="";
	  }
	   }	   

   	var field=document.getElementById('description').value; 
  var field_div=document.getElementById('description_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter description </font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   }	   

   	var field=document.getElementById('headquater').value; 
  var field_div=document.getElementById('headquater_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter headquarter address </font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   }
	   
 var users = document.getElementById("morelogin").style.display;
 if(users!="none")
 {
 /////// Validation for 2 more logins//////////////////////////////
  var field=document.getElementById('email2').value; 
  var password=document.getElementById('password2').value;
  var confirm_password=document.getElementById('confirm_password2').value; 
   var field_div=document.getElementById('email2_div');
    var password_div=document.getElementById('password2_div');
	var confirm_password_div=document.getElementById('confirm_password2_div'); 
 //alert(field.length);
 if(field.length==0 && password.length==0 && confirm_password.length==0)
  {
	  field_div.innerHTML=""; password_div.innerHTML=""; confirm_password_div.innerHTML=""; 
  }
  else
  {
  //alert(field);
  var field_div=document.getElementById('email2_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter Email </font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   if (echeck(field)==false){field_div.innerHTML="<font color='red'>Please enter valid email address </font>"; error=1}
	  else {
	  field_div.innerHTML=""; 
		   }
	   }
	   //alert(field_div.innerHTML);
  var password=document.getElementById('password2').value; 
  var password_div=document.getElementById('password2_div'); 	
  if(password=='') {password_div.innerHTML="<font color='red'>Password cannot be Blank </font>"; error=1}
  else {
  password_div.innerHTML=""; 
	   }
	   
  var confirm_password=document.getElementById('confirm_password2').value; 
  var confirm_password_div=document.getElementById('confirm_password2_div'); 	
  if(confirm_password=='') {confirm_password_div.innerHTML="<font color='red'>Confirm Password cannot be Blank </font>"; error=1}
  else {
  confirm_password_div.innerHTML=""; 
 /* if(password!=confirm_password) {
   error=1;
   confirm_password_div.innerHTML="<font color='red'>Password mismatch</font>";
   }else confirm_password_div.innerHTML="";*/
	   }
   
    var confirm_password=document.getElementById('confirm_password2').value; 
  var confirm_password_div=document.getElementById('confirm_password2_div'); 	
  if(confirm_password=='') {confirm_password_div.innerHTML="<font color='red'>Confirm Password cannot be Blank </font>"; error=1}
  else {
  confirm_password_div.innerHTML=""; 
  if(password!=confirm_password) {
   error=1;
   confirm_password_div.innerHTML="<font color='red'>Password mismatch</font>";
   }else confirm_password_div.innerHTML="";
	   }
  }
  //////validation for user 3
    var field=document.getElementById('email3').value; 
  var password=document.getElementById('password3').value;
  var confirm_password=document.getElementById('confirm_password3').value; 
   var field_div=document.getElementById('email3_div');
    var password_div=document.getElementById('password3_div');
	var confirm_password_div=document.getElementById('confirm_password3_div'); 
	 if(field.length==0 && password.length==0 && confirm_password.length==0)
  {
	  field_div.innerHTML=""; password_div.innerHTML=""; confirm_password_div.innerHTML=""; 
  }
  else
  {
  var field=document.getElementById('email3').value; 
  var field_div=document.getElementById('email3_div'); 	
  if(field=='') {field_div.innerHTML="<font color='red'>Please enter Email </font>"; error=1}
  else {
  field_div.innerHTML=""; 
	   if (echeck(field)==false){field_div.innerHTML="<font color='red'>Please enter valid email address </font>"; error=1}
	  else {
	  field_div.innerHTML=""; 
		   }
	   }
	   
  var password=document.getElementById('password3').value; 
  var password_div=document.getElementById('password3_div'); 	
  if(password=='') {password_div.innerHTML="<font color='red'>Password cannot be Blank </font>"; error=1}
  else {
  password_div.innerHTML=""; 
	   }
	   
  var confirm_password=document.getElementById('confirm_password3').value; 
  var confirm_password_div=document.getElementById('confirm_password3_div'); 	
  if(confirm_password=='') {confirm_password_div.innerHTML="<font color='red'>Confirm Password cannot be Blank </font>"; error=1}
  else {
  confirm_password_div.innerHTML=""; 
  if(password!=confirm_password) {
   error=1;
   confirm_password_div.innerHTML="<font color='red'>Password mismatch</font>";
   }else confirm_password_div.innerHTML="";
	   }
    var field=document.getElementById('email').value; 
	var field2=document.getElementById('email2').value; 
	var field3=document.getElementById('email3').value;
	var email2_div=document.getElementById('email2_div');
	var email3_div=document.getElementById('email3_div');
    if(field!='' && field2!='' && field3!='')
	{ //alert{"hello"};
	if(field2==field) {email2_div.innerHTML="<font color='red'>Email cannot be same as first login</font>"; error=1}
	else
	{email2_div.innerHTML="";}
	if(field3==field || field3==field2) {email3_div.innerHTML="<font color='red'>Email cannot be same as first two logins</font>"; error=1}
	else
	{email3_div.innerHTML="";}
	}
  }
	
///////////////////////////////////////////////////////////////////////
 }
   var name=document.getElementById('tandc'); 
  var name_div=document.getElementById('tandc_div'); 	
  if(name.checked==false) {name_div.innerHTML="<font color='red'>Please read terms and conditions.</font>"; error=1}
  else {
  name_div.innerHTML=""; 
	   }

	  
  if(error==1) { 
		if(option==1){} else return false;
  }
  else
   {
	 try
	 {
		 var error_message=document.getElementById('error_message');
		 error_message.innerHTML="<h3><font color='GREEN'>Please wait, submitting information...</font></h3>";
		 document.getElementById('form_submit').value='true';
		 if(option==1){
		 document.stateform.submit(); 
		 } else return true;
	 }
	 catch(e){
		// news_image_div.innerHTML="<font color=red>File location is not accessible </font>";
		 //error_message.innerHTML="";
	 }
  }	
}

var row2=1;			
function add_more_slab() 
{
	//row2=document.getElementById('total_rows2').value;
			
	var add_div = document.getElementById('add_more_slab'+row2);
	row2++;
	document.getElementById('total_rows2').value = row2;
	var HTML ='<div class="column1"><label class="join-lbl">Branch:</label><textarea name="branch[]" cols="32" rows="4" class="join-txtarea" id="branch'+(row2)+'"></textarea></div><div id="branch_div" style="padding-left:200px;"></div><div class="column1"><label class="join-lbl">Zip:</label><input type="text" name="zip[]" cols="32" rows="4" class="join-input" id="zip'+(row2)+'" /></div><div id="zip_div" style="padding-left:200px;"></div><div class="column1"><label class="join-lbl">Country:</label><input type="text" name="country[]" cols="32" rows="4" class="join-input" id="country'+(row2)+'" /></div><div id="country_div" style="padding-left:200px;"></div><div class="column1"><label class="join-lbl">State:</label><input type="text" name="state[]" cols="32" rows="4" class="join-input" id="state'+(row2)+'" /></div><div id="state_div" style="padding-left:200px;"></div><div class="column1"><label class="join-lbl">Phone:</label><input type="text" name="phone[]" cols="32" rows="4" class="join-input" id="phone'+(row2)+'" /></div><div id="phone_div" style="padding-left:200px;"></div><div class="column1"><label class="join-lbl">Google Map:</label><a href="gdata.php?latitude=53.40953185308643&longitude=-7.75634765625" rel="lyteframe" rev="width: 650px; height: 600px; scrolling: no;">Add cordinate</a><input type="hidden" name="latitude[]" cols="32" rows="4" class="join-input" id="longitude[]" /><input type="hidden" name="longitude[]" cols="32" rows="4" class="join-input" id="longitude[]" /></div></div><div id="r" style="padding-left:200px;"></div><br><div id="add_more_slab'+(row2)+'"></div>'; 			
	
	add_div.innerHTML=HTML;
		
}

function onlyNumeric(obj,type,len)
{
var reDigit;
	if(type==1)
	{
		reDigit="^\\d{1,"+len+"}$";
	}
	else if(type==2)
	{
		reDigit="^([0-9]+)([\.]?)([0-9]{0,"+len+"})$"; 
	}
	
	if (obj.value.search(reDigit)==-1) 
		obj.value=obj.value.substring(0,obj.value.length-1);
}
