var validate_on_submit = 0;  

function validateOnSubmit()
{
	error_log = '';
	validate_on_submit = 1;
	validateAll();
	validate_on_submit = 0;
	if(error_log != '')
	{
		error_log = 'Please correct the following problems:\n'+error_log;
	   alert(error_log);
	   logError('general_contact_us: '+error_log, '/general_enquiry.js',13);
	   return false;	
	}
	return true;  
}


function validateAll()
{
   validate('from_name',1);
   validate('from',3);
   validate('telephone',2);
   validate('message',1);
   validate('word',11);   
}


var error_log = '';
function logSubmitError(field)
{
	error_log += '- ';
   switch(field)
   {
   	case 'from_name':
   	error_log += "Your  name is required when making an enquiry\n";
   	break;  	   	    	   		
   
   	case 'from':
   	error_log += "Your email address is required when making an enquiry\n";
   	break;
   	
   	case 'telephone':
   	error_log += "Please provide your phone number if you would like to be contacted by phone\n";
   	break;
   	
   	case 'message':
   	error_log += "Please ensure your enquiry is not blank\n";
   	break;    	
   	
   	case 'word':
   	error_log += "Please ensure you typed the word correctly\n";
   	break; 
   	
	}	
}

function check_email_address(email)
{
   var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/  
   return email.match(re); 	
}

function check_word(word)
{
   var url = '/ajax/check_captcha_word.ajax.php?word='+word;
   return (ajaxRequest(url, false) == '00') ? true : false;
}


function validate(field,type,flag)
{
	
	// Flag used to suppress alerts
	master_flag = flag;   
	
	var val = document.getElementById(field).value;
	   
	if(val == '' && (field != 'telephone' || (field == 'telephone' && document.getElementById('preference').value == 'By Phone'))) 
	{
	   if(document.getElementById('v_'+field))
	   {
   	   document.getElementById('v_'+field).innerHTML = (validate_on_submit == 1) ? '<img src="/images/error_s_trans.png" border="0" width="20" height="20">' : '<span class="mandatory_field">&laquo;</span>';             
		}
   	if(validate_on_submit == 1) logSubmitError(field);
	   return false;
	}
   var rv = true;
   switch(type)
   {
   	// Not blank
   	case 1:
   	if(val == '') 
   	{
   		rv = false;
   		if(validate_on_submit == 1) logSubmitError(field);  
		}
   	break;
   	
   	
   	// Telephone
   	case 2:
   	if(document.getElementById('preference').value == 'By Phone')
   	{
   		if(isNaN(val) || val.length <= 5) 
   		{
   			rv = false;
   			if(validate_on_submit == 1) logSubmitError(field); 
			}
		}
		else
		{
		   document.getElementById('v_telephone').innerHTML = '<span class="mandatory_field">&nbsp;</span>';   	
		   return;
		}
   	break;   	
   	
   	
   	// Email
   	case 3:
   	
		
   	// Check if 'customer_email' is blank
	   if(document.getElementById(field).value == '') 
	   {
	   	// Client side failed  
	   	rv = false;
   		if(validate_on_submit == 1) logSubmitError('email');
		}
		
		// If it's not blank, client side validation
		else
		{
		   if(!check_email_address(document.getElementById(field).value))
		   {
		   	// Client side failed
		   	rv = false;
				if(validate_on_submit == 1) logSubmitError('email'); 
			}	
			else
			{
				rv = true;
			}
		}
   	break;
   	
   	
  	   // Captcha word
   	case 11:
   	
		
   	// Check if 'word' is blank
	   if(document.getElementById('word').value == '') 
	   {
	   	// Client side failed  
	   	rv = false;
   		if(validate_on_submit == 1) logSubmitError('word');
		}
		
		// If it's not blank, client side validation
		else
		{
		   if(!check_word(document.getElementById('word').value))
		   {
		   	// Client side failed
		   	rv = false;
				if(validate_on_submit == 1) logSubmitError('word'); 
			}	
			else
			{
				rv = true;
			}
		}
   	break; 
   	
   	
   	// Preference
   	case 13:
   	
   	if(val == 'By Phone')
   	{	
   	   validate('telephone',2);
		}
		else
		{
		   document.getElementById('v_telephone').innerHTML = '<span class="mandatory_field">&nbsp;</span>';	
		}
		return;
		break;
   	
      
	}
	
	if(document.getElementById('v_'+field))
	{
	   var error_img = (validate_on_submit == 1) ? '<img src="/images/error_s_trans.png" border="0" width="20" height="20">' : '<img src="/images/facebook_x_trans.png" border="0" width="15" height="15">';
	   var iHTML = (rv) ? '<img src="/images/facebook_tick_trans.png" border="0" width="15" height="15">' : error_img;
	   document.getElementById('v_'+field).innerHTML = iHTML;	
	}
}



function getMostApplicableFAQsResponse()
{
	if (xmlHttp.readyState==4)
	{
		rc = xmlHttp.responseText;
		if(rc != '')
		{
			var applicable_faqs_div = document.getElementById("applicable_faqs_div");
			var x = findPosX(document.getElementById('message'));
			var y = findPosY(document.getElementById('message'));
			applicable_faqs_div.style.marginLeft = (x-270)+'px';
			applicable_faqs_div.style.marginTop = (y)+'px';
			applicable_faqs_div.innerHTML = rc;
			applicable_faqs_div.style.display = '';
		}
		else
		{
			var applicable_faqs_div = document.getElementById("applicable_faqs_div");
			applicable_faqs_div.innerHTML = '';
			applicable_faqs_div.style.display = 'none';
		}
	}
}

function getMostApplicableFAQs(text)
{
	if(text.length >= server_character_threshold_faqs_on_enquiry)
	{
		var url = '/ajax/get_applicable_faqs.ajax.php?text='+encodeURIComponent(text)+'&language_id=1';
		ajaxRequest(url, true, 'getMostApplicableFAQsResponse');
	}
	else
	{
		var applicable_faqs_div = document.getElementById("applicable_faqs_div");
		applicable_faqs_div.innerHTML = '';
		applicable_faqs_div.style.display = 'none';
	}
}


function closeApplicableFAQs()
{
	document.getElementById("applicable_faqs_div").innerHTML = '';
	document.getElementById("applicable_faqs_div").style.display = 'none';
}

function findPosX(obj)
{
 var curleft = 0;
 if(obj.offsetParent)
	  while(1)
	  {
		 curleft += obj.offsetLeft;
		 if(!obj.offsetParent)
			break;
		 obj = obj.offsetParent;
	  }
 else if(obj.x)
	  curleft += obj.x;
 return curleft;
}



function findPosY(obj)
{
 var curtop = 0;
 if(obj.offsetParent)
	  while(1)
	  {
		 curtop += obj.offsetTop;
		 if(!obj.offsetParent)
			break;
		 obj = obj.offsetParent;
	  }
 else if(obj.y)
	  curtop += obj.y;
 return curtop;
}




function sendEnquiry()
{
	 var from_name = document.getElementById('from_name').value;
	 var from = document.getElementById('from').value;
	 var telephone = document.getElementById('telephone').value;
	 var preference = document.getElementById('preference').value;
	 var message = document.getElementById('message').value;
	 var word = document.getElementById('word').value;
	 var contact_location = document.getElementById('contact_location').value;
	 var qs = 'from_name='+encodeURIComponent(from_name)+'&from='+encodeURIComponent(from)+'&telephone='+encodeURIComponent(telephone)+'&preference='+encodeURIComponent(preference)+'&message='+encodeURIComponent(message)+'&word='+encodeURIComponent(word)+'&contact_location='+encodeURIComponent(contact_location);
	 var url = '/ajax/send_general_enquiry.ajax.php?'+qs;
	 ajaxRequest(url, true, 'sendEnquiryResponse'); 
}


function sendEnquiryResponse()
{
   if (xmlHttp.readyState==4)
   {
      rc = xmlHttp.responseText;
      if(rc.indexOf("|")!=-1)
      {
         var parts = rc.split("|");	
         if(parts.length == 2)
         {
            alert(parts[1]);
            // Success
            if(parts[0] == 0)
            {
                     _gaq.push(['_trackPageview','/funnel_G4/general_enquiry/enquiry_sent.html']);   
					 document.getElementById('from_name').value = '';
					 document.getElementById('from').value = '';
					 document.getElementById('telephone').value = '';
					 document.getElementById('preference').value = 'By E-mail';
					 document.getElementById('message').value = '';
					 document.getElementById('word').value = '';
					 validate('from_name',1);
					 validate('from',3);
					 validate('telephone',2);
					 validate('word',11);					 
				}
			}
		}
   }
}


