function validPhone(phone_field,which_leadbox)
{	
	var which_leadbox = which_leadbox;
	var area_codes = "02,03,04,08,09,072,073,076,077,078,079,050,052,054,057";
	var phone_number = document.getElementById(phone_field).value;
	var pre_code = document.getElementById(which_leadbox +"_pre_phone_code").value; // need to see there is no hitnagshot here

	phone_number = pre_code + phone_number;

	var valid = false;
	// Check for correct phone number
	 var rePhoneNumber = new RegExp(/^([0-9]{7})$/);
	 var phone_areas = area_codes.split(",");
	 for(area_code=0;area_code<phone_areas.length;area_code++)
	 {
		if(phone_number.substr(0, phone_areas[area_code].length) == phone_areas[area_code])
		{
			full_phone = phone_number.substr(phone_areas[area_code].length);
			if(rePhoneNumber.test(full_phone))
			{
				valid = true;
			}
		}//If area code found
	 }//For each area code in list
	return valid;
}

function validEmail(which_leadbox)
{	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var email_address = document.getElementById(which_leadbox +"_email").value;
	if(reg.test(email_address) == false) 
	  return false;
	else
		return true;
}

function check_leadbox_details(which_leadbox)
{

	// For business part
	if (which_leadbox == "")
	{
		var $roof_size = $("#roof_size");
		var $building_type = $("#building_type");
		var $which_way = $("#which_way");
	}
	// Get into objects the alerts messages

	var $fullname_alert = $("#"+ which_leadbox + "_fullname_alert");
	var $phone_alert = $("#" + which_leadbox +"_phone_alert");
	var $pre_phone_alert = $("#" +which_leadbox +"_pre_phone_alert");
	var $pre_phone_code_alert = $("#" +which_leadbox +"_pre_phone_code_alert");
	var $email_alert = $("#" +which_leadbox +"_email_alert");
	

	var $home_alert = $("#" +which_leadbox +"_home_alert");
	var $property_alert = $("#" +which_leadbox +"_property_alert");
	var $client_type_alert = $("#" +which_leadbox +"_client_type_alert");
	var $size_alert = $("#" +which_leadbox +"_size_alert");
	var $roof_type_alert = $("#" +which_leadbox +"_roof_type_alert");
	var $building_alert = $("#" +which_leadbox +"_building_alert");
	// Hide all alerts
	$("#alerts span").hide();
	$("#errors span").hide();
	
	if ($("#" + which_leadbox +"_fullname").val() == "")
	{
		$("#" + which_leadbox +"_fullname").focus();
		$fullname_alert.show();
		return;
	}

	if ($("#" + which_leadbox +"_pre_phone_code").val() == "")
	{	
		$("#" + which_leadbox +"_pre_phone_code").focus();
		$pre_phone_code_alert.show();
		return;
	}
	
	if ($("#" + which_leadbox +"_phone").val() == "")
	{	
		$("#" + which_leadbox +"_phone").focus();
		$pre_phone_alert.show();
		return;
	}
	
	if (!validPhone(which_leadbox + "_phone",which_leadbox))
	{
		$("#" + which_leadbox +"_phone").focus();
		$phone_alert.show();
		return;
	}
	
	if (!validEmail(which_leadbox))
	{
		$("#"+ which_leadbox + "_email_alert").show();
		$("#"+ which_leadbox + "_email").focus();
		return;
	}
	
	if ($("#" + which_leadbox +"_home").val() == "")
	{	
		$("#" + which_leadbox +"_home").focus();
		$home_alert.show();
		return;
	}
	
	
	/*
	if ($("#" + which_leadbox +"_roof_type").val() == "")
	{	
		$("#" + which_leadbox +"_roof_type").focus();
		$roof_type_alert.show();
		return;
	}
	
	if ($("#" + which_leadbox +"_size").val() == "")
	{	
		$("#" + which_leadbox +"_size").focus();
		$size_alert.show();
		return;
	}
	
	if ($("#" + which_leadbox +"_client_type").val() == "")
	{	
		$("#" + which_leadbox +"_client_type").focus();
		$client_type_alert.show();
		return;
	}
	
	if ($("#" + which_leadbox +"_building").val() == "")
	{	
		$("#" + which_leadbox +"_building").focus();
		$building_alert.show();
		return;
	}
	*/
	jConfirm($("#" + which_leadbox +"_fullname").val() +', האם '+ $("#" + which_leadbox +"_pre_phone_code").val() + $("#" + which_leadbox +"_phone").val() +' הוא אכן הטלפון שלך?','עוד רגע סיימנו...', function(r) {
			if(r == true)
			{
				$("#" + which_leadbox +'_leadbox').submit();
			}
			else
			{
				$("#" + which_leadbox +"_pre_phone").focus();
				$("#" + which_leadbox +"_fix_phone_alert").show();
			}
		});	
}

/*
function facebook_frame()
{
$('#facebook_div').show();
$('#facebook_frame').attr('src','http://www.facebook.com/sharer.php?u=http://www.solarenergyil.co.il?from=Fmenushare');
}
*/

function Comma(number) 
{
	number = '' + number;
	if (number.length > 3) 
	{
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length / 3); i++) 
		{
			if ((mod == 0) && (i == 0))
				output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
			else
				output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
		}
		return (output);
	}
	else return number;
}
