function validPhone(which_leadbox)
{	
	var area_codes = "02,03,04,08,09,072,073,076,077,078,079,050,052,054,057";
	var phone_number = $("#"+ which_leadbox + "_pre_phone").val();
	var pre_code = $("#"+ which_leadbox + "_pre_phone_code").val();

	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(which_leadbox)
{

	$("#alert p").hide();
	$("#alertAsk p").hide();


	if ($("#" + which_leadbox +"_fullname").val() == "")
	{
		$("#" + which_leadbox +"_fullname").focus();
		$("#" + which_leadbox +"_fullname_alert").show();
		return;
	}

	if ($("#" + which_leadbox +"_pre_phone_code").val() == "")
	{	
		$("#" + which_leadbox +"_pre_phone_code").focus();
		$("#" + which_leadbox +"_pre_phone_code_alert").show();
		return;
	}
	
	if ($("#" + which_leadbox +"_pre_phone").val() == "")
	{	
		$("#" + which_leadbox +"_pre_phone").focus();
		$("#" + which_leadbox +"_pre_phone_alert").show();
		return;
	}
	
	if (!validPhone(which_leadbox))
	{
		$("#" + which_leadbox +"_pre_phone").focus();
		$("#" + which_leadbox +"_phone_alert").show();
		return;
	}
	/*
	if (!validEmail(which_leadbox))
	{
		$("#"+ which_leadbox + "_email_alert").show();
		$("#"+ which_leadbox + "_email").focus();
		return;
	}
	*/
	
	if (which_leadbox == "jumping")
	{ 
		// Add question to database
		var question_val = $("#question").val();
		if(question_val != "")
		{
			$.get("/script/add_question.php", {question: question_val},
			function (data){
			
				alert('good! submitted! And added question!');
				//$("#" + which_leadbox +'_leadbox').submit();
			})
		}
	}
	else
	{
		jConfirm($("#" + which_leadbox +"_fullname").val() +', האם '+ $("#" + which_leadbox +"_pre_phone_code").val() +"-"+ $("#" + which_leadbox +"_pre_phone").val() +' הוא אכן הטלפון שלך?','עוד רגע סיימנו...', function(r) {
			if(r == true)
			{
				//alert('good! submitted');
				$("#" + which_leadbox +'_leadbox').submit();
			}
			else
			{
				$("#" + which_leadbox +"_pre_phone").focus();
				$("#" + which_leadbox +"_fix_phone_alert").show();
			}
		});
	}
}

function add_story()
{
	var story_value = $("#story").val();
	var fullname_value = $("#story_fullname").val();
	$.get("/script/add_story.php", {story: story_value,fullname: fullname_value},
	function(data){
	$("#golshimBox").hide();
	$("#after_sent").html(data);
	$("#after_sent").hide();
	$("#after_sent").fadeIn('slow');
	})
}

function open_question_leadbox()
{
	tb_show('','/includes/jumping_question_leadbox.php?KeepThis=true&TB_iframe=true&height=430&width=360')
}

jQuery(document).ready(function(){
	// Not working good!
	$('.questions_accordion').click(function() {
		$(this).next().toggle('slow');
		return false;
	}).next().hide();
	// Show order_now leadbox
	$('.order_now').click(function(){
		tb_show('','/includes/order_now_leadbox.php?KeepThis=true&TB_iframe=true&height=430&width=360')
	})
	
	// Clear content from question_leadbox
	$('#Askbox input').one('focus', function(){
	  $(this).attr('value', '');
	});
	
	// Clear content from question_leadbox
	$('#golshimBox input').one('focus', function(){
	  $(this).attr('value', '');
	});
	
});
