// Business account registration form validation and functions
// PhatDeals.net
//

function validate_form()
{
   document.getElementById('guidelines').style.display = 'none';
   document.getElementById('bus_info').style.display = 'block';
   
    var formErrors = new Array();
    
    if ($('#first_name').val() == '') {
        $('#first_name').addClass('error');
        formErrors[formErrors.length] = 'err0'; //No email address
    } if ($('#email').val() == '') {
        $('#email').addClass('error');
        formErrors[formErrors.length] = 'err1'; //No email address
    } if ($('#passwd').val() == '') {
        $('#passwd').addClass('error');
        formErrors[formErrors.length] = 'err2'; //No password
    } if ($('#passwd_confirm').val() != $('#passwd').val() ||
          ($('#passwd_confirm').val() == '' && $('#passwd').val() == '')) {
        $('#passwd_confirm').addClass('error');
        formErrors[formErrors.length] = 'err3'; //Passwords do not match
    } if ($('#business_name').val() == '') {
        $('#business_name').addClass('error');
        formErrors[formErrors.length] = 'err4'; //No business name
    } if ($('#borough').val() == '') {
        $('#borough').addClass('error');
        formErrors[formErrors.length] = 'err5'; //No borough selected
    } if ($('#business_address').val() == '') {
        $('#business_address').addClass('error');
        formErrors[formErrors.length] = 'err6'; //No business address
    } if ($('#tos_accept').attr('checked') == false) {
        $('label.tos_accept').addClass('error');
        formErrors[formErrors.length] = 'err7'; //No TOS
    }
    
    if (formErrors.length == 0) {
        return true;
    } else {
        $('div.error').css('display', 'block');

        for (var i = 0;i < formErrors.length; i++)
        {
            $('#' + formErrors[i]).css('display', 'list-item');
        }
        
        window.location = "register.php#errors";
        return false;
    }
}

var geocoder = new GClientGeocoder();
var viewport = new GLatLngBounds();// = new GLatLngBounds(new GLatLng(34.299203,-120.114899),  new GLatLng(34.617387,-119.437866) );

function geocode(){
   var address = document.getElementById('business_address').value;
   
   //address is 3 chars or less, so don't bother
   if(address.length <= 3){
      document.getElementById('geo_msg').innerHTML = '';
      return;
   }
   
   document.getElementById('lat').value = '';
   document.getElementById('lng').value = '';

   geocoder.getLocations(
      address,
      
      function(response){
         
         //address not found
         if(!response || response.Status.code != 200){
           document.getElementById('geo_msg').innerHTML = 'The address as entered can not be found by Google Maps.';
	        document.getElementById('geo_msg').style.color = 'red';
         }
         else{
            
            place = response.Placemark[0];
            
            //address found and is in the loaded viewport
	         //if(viewport.containsLatLng( new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]) )){
	            document.getElementById('geo_msg').innerHTML = 'This address has been found by Google Maps: '+place.address;
	            document.getElementById('geo_msg').style.color = 'green';
	            document.getElementById('lat').value = place.Point.coordinates[1];
               document.getElementById('lng').value = place.Point.coordinates[0];
	         //}
            //address found but not in viewport
            //else{
            //   var message = 'The address as entered is found by Google Maps, but not in your selected community.';
            //   if(document.getElementById('borough_id').value == '')message += ' Please select a community above.';
            //   document.getElementById('geo_msg').innerHTML = message;
	         //   document.getElementById('geo_msg').style.color = 'purple';
	         //}
            
         }
      }
   );
}

function load_viewport(){
   var borough_id = document.getElementById('borough_id').value;
   if(borough_id == ''){
      viewport = new GLatLngBounds()
      geocode();
      return;
   }
	var xmlHttp;
	if(window.XMLHttpRequest)xmlHttp = new XMLHttpRequest();
	else if(window.ActiveXObject)xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlHttp.open('GET','php/get_viewport.php?borough_id='+borough_id);
	xmlHttp.onreadystatechange = function(){
	    if(xmlHttp.readyState == 4){
		    coords = xmlHttp.responseText.split(',');
          viewport = new GLatLngBounds(new GLatLng(coords[0],coords[1]),  new GLatLng(coords[2],coords[3]) );
          geocode();
	    }
	}
	xmlHttp.send(null);
}

function toggle_guidelines(){
   var guidelines = document.getElementById('guidelines');
   var info = document.getElementById('bus_info');
   if(guidelines.style.display == 'block'){
      guidelines.style.display = 'none';
      info.style.display = 'block';
   }
   else{
      guidelines.style.display = 'block';
      info.style.display = 'none';
   }
   return false;
}
