// Show Hide Buttons
//Middle
function middle_show() {
		//alert('show');
		$('middle').className = 'middle';
		$('upper_fold').show();
		$('show_button').hide();
		$('hide_button').show();
		$('link_ads').show();
		$('second').className = 'second_content';
}
function middle_hide() {
		//alert('show');
		$('middle').className = 'middle cover';
		$('upper_fold').hide();
		$('show_button').show();
		$('hide_button').hide();
		$('link_ads').hide();
		$('second').className = 'second_content cover_2';
}
//Middle with H1
function h1_low() {
		//alert('show');
		$('low').className = 'lower';
}
function h1_high() {
		//alert('show');
		$('low').className = '';
}
function hi(d) {
	try{
   	 	document.getElementById(d).style.display = "none";
	} catch(e) {}
}
function sh(d) {
	try{ 
    	document.getElementById(d).style.display = "block";
  	} catch(e) {}
}
function ads_in_show() {
		//alert('show');
		$('link_ads_in').hide();

}
function ads_in_hide() {
		//alert('show');
		$('link_ads_in').show();

}
function show_article(element, total) {
	for(i=1; i<= total; i++) {
		document.getElementById('article_'+i).style.display = 'none';
	}
	document.getElementById('article_'+element).style.display = 'block';
}
function consult_broker_send(type_of_enquiry){
//type_of_enquiry can be 2 or 3 it depend of the form. I use this variable in order to use the same fonction for the both kind of id $()	

	error = 0;
	field = '';
	if(type_of_enquiry == 2){
		div_id = 'enquire';
		my_function = 'reload_enquire_now';
	}if(type_of_enquiry == 3){
		div_id = 'lightwindow_consult_broker';
	}
	if(type_of_enquiry == 4){
		div_id = 'contact_us';
		my_function = 'reload_enquire_now_get_advice_form';
	}
	if((name = $('name'+type_of_enquiry).value) == ''){
		field += 'Name, ';
		error = 1;
	}
	if((phone = $('phone'+type_of_enquiry).value) == ''){
	field += 'Phone, ';
	error = 1;
	}
	if(((type_of_enquiry == 3) && (company_id = $('company_id'+type_of_enquiry).value) == '')){
		field += "an error occured in your broker's selection process, ";
		//error = 1;
	}
	if(type_of_enquiry == 3 || type_of_enquiry == 4){
		product_id = $('product_id'+type_of_enquiry).value;
	}
	receive_offer = $('receive_offer'+type_of_enquiry).checked;
	if(receive_offer==false) {receive_offer='no';}
	else {receive_offer='yes';}
	
	email = $('email'+type_of_enquiry).value;
	valid = Validate_Email_Address(email);
	if(valid == false){
		field += 'Email, ';
		error = 1;
	}
	if((locationId = $('locationId'+type_of_enquiry).value) == ''){
		field += 'you have to fill in properly Location, ';
		error = 1;
	}
	if(type_of_enquiry == 4){
		home_loan = $('home_loan'+type_of_enquiry).checked;
		car_loan = $('car_loan'+type_of_enquiry).checked;
		personal_loan = $('personal_loan'+type_of_enquiry).checked;
		other = $('other'+type_of_enquiry).checked;
		tax_advice = $('tax_advice'+type_of_enquiry).checked;
		financial_planning=$('financial_planning'+type_of_enquiry).checked;
		
		if( (home_loan==false) && (car_loan==false) && (personal_loan==false) && (other==false)&& (tax_advice==false) && (financial_planning==false)){
			field += 'Product Type, ';
			error = 1;
		}
		product_type = "";
		if(home_loan == true){
			product_type += "&home_loan="+home_loan;
		}
		if(car_loan == true){
			product_type += "&car_loan="+car_loan;
		}
		if(personal_loan == true){
			product_type += "&personal_loan="+personal_loan;
		}
		if(other == true){
			product_type += "&other="+other;
		}
		if(tax_advice == true){
			product_type += "&tax_advice="+tax_advice;
		}
		if(financial_planning == true){
			product_type += "&financial_planning="+financial_planning;
		}
	}
	if(type_of_enquiry == 2){
		car_insurance = $('car_insurance'+type_of_enquiry).checked;
		home_insurance = $('home_insurance'+type_of_enquiry).checked;
		heatlh_insurance = $('heatlh_insurance'+type_of_enquiry).checked;
		other = $('other'+type_of_enquiry).checked;
		
		if( (car_insurance==false) && (home_insurance==false) && (heatlh_insurance==false) && (other==false)){
			field += 'Product Type, ';
			error = 1;
		}
		product_type = "";
		if(car_insurance == true){
			product_type += "&car_insurance="+car_insurance;
		}
		if(home_insurance == true){
			product_type += "&home_insurance="+home_insurance;
		}
		if(heatlh_insurance == true){
			product_type += "&heatlh_insurance="+heatlh_insurance;
		}
		if(other == true){
			product_type += "&other="+other;
		}
	}
	message = $('comment'+type_of_enquiry).value;
	
	if(error == 1){
		alert('Please fill in the following field correctly: '+field);
		$('submit'+type_of_enquiry).value = "Send";
		$('submit'+type_of_enquiry).disabled = false;
		$('submit'+type_of_enquiry).className = "sub";
	}
	if(error == 0){
		if(type_of_enquiry == 2){
			var pars = "name="+escape(name)+"&phone="+escape(phone)+"&email="+escape(email)+"&locationId="+escape(locationId)+"&message="+escape(message)+product_type+"&receive_offer="+escape(receive_offer)+"&my_function="+escape(my_function);
		}
		if(type_of_enquiry == 3){
			var pars = "name="+escape(name)+"&phone="+escape(phone)+"&email="+escape(email)+"&locationId="+escape(locationId)+"&message="+escape(message)+"&company_id="+escape(company_id)+"&product_id="+escape(product_id)+"&receive_offer="+escape(receive_offer);
		}
		if(type_of_enquiry == 4){
			
			var pars = "name="+escape(name)+"&phone="+escape(phone)+"&email="+escape(email)+"&locationId="+escape(locationId)+"&message="+escape(message)+product_type+"&receive_offer="+escape(receive_offer)+"&product_id="+escape(product_id)+"&my_function="+escape(my_function);
		}
		var myAjax_test = new Ajax.Updater(
				div_id, 
				http_host+'/ajax/ajax_send_lead.php?'+pars, {
					evalScripts:true
				}
			);
		
		//if(type_of_enquiry == 2 || type_of_enquiry == 4) {setTimeout(my_function, 7000);}
	}
}
function reload_enquire_now(){
	var pars;	
	var myAjax_test = new Ajax.Updater(
		'enquire', 
		http_host+'/tags/enquire_now.php?'+pars, {
			evalScripts:true
		}
	);
}
function Validate_String(string, return_invalid_chars)
         {
         valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         invalid_chars = '';
         
         if(string == null || string == '')
            return(true);
         
         //For every character on the string.   
         for(index = 0; index < string.length; index++)
            {
            char = string.substr(index, 1);                        
            
            //Is it a valid character?
            if(valid_chars.indexOf(char) == -1)
              {
              //If not, is it already on the list of invalid characters?
              if(invalid_chars.indexOf(char) == -1)
                {
                //If it's not, add it.
                if(invalid_chars == '')
                   invalid_chars += char;
                else
                   invalid_chars += ', ' + char;
                }
              }
            }                     
            
         //If the string does not contain invalid characters, the function will return true.
         //If it does, it will either return false or a list of the invalid characters used
         //in the string, depending on the value of the second parameter.
         if(return_invalid_chars == true && invalid_chars != '')
           {
           last_comma = invalid_chars.lastIndexOf(',');
           
           if(last_comma != -1)
              invalid_chars = invalid_chars.substr(0, $last_comma) + 
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
                      
           return(invalid_chars);
           }
         else
           return(invalid_chars == ''); 
         }


function Validate_Email_Address(email_address)
         {
         //Assumes that valid email addresses consist of user_name@domain.tld
         at = email_address.indexOf('@');
         dot = email_address.indexOf('.');
         
         if(at == -1 || 
            dot == -1 || 
            dot == 0 || 
            dot == email_address.length - 1)
            return(false);
            
         user_name = email_address.substr(0, at);
         domain_name = email_address.substr(at + 1, email_address.length);                  
         
         if(Validate_String(user_name) === false || 
            Validate_String(domain_name) === false)
            return(false);                     
         
         return(true);
         }