// JavaScript Document
$(document).ready(function(){
	$('#country_for_all').change(function(){get_cities(this.value);});
	initMenu();
	$('textarea').elastic();
	form_initialize();
	$("div.form_content").fadeOut(100);
	//get_countries(); //by erhan
	$("#cf1").submit(function() {
		error=0;
		$('.form_all #cf1 .input').each(function(){
			$(this).removeClass('input_error');
			if(this.id=='email' && isEmail(this.value)==false){
				error++;
				$(this).addClass('input_error');
			}
			if (this.value == '' || this.value==this.title){ 
				error++;
				$(this).addClass('input_err');
			}
		});
		$('.form_all #cf1 .input_temp').each(function(){
			$(this).removeClass('input_temp').addClass('input_error');
			error++;
		});		
		$('.form_all #cf1 .input_error').each(function(){
			$(this).removeClass('input_temp').addClass('input_error');
			error++;
		});
		if(error==0){
			current_form='cf1';
			$(this).ajaxSubmit({beforeSubmit:pre_send,success:post_send})
		}else{
			alert(missing_field_error);
		}	
		return false; 
	});
	$('div#close_button').click(function(){$("div.form_content").fadeOut(100);});
	$('div.button_holder').click(function(){$("div.form_content").toggle();});
});
function pre_send(){
	$('#cf1').hide().after("<div style='text-align:center;'><br><br><img src='http://static.whereismydoctor.com/loader.gif' ><br><br>"+message_request_processing+"<br /><br /></div>");

}
function post_send(msg){
	if(msg=='failure'){
		alert(missing_field_error);
		$('#cf1').show().next().html("");
	}else
		$('#cf1').next().html("<div style='text-align:center;'><br><br><img src='http://static.whereismydoctor.com/msg_sent.gif' ><br><br>"+message_request_sent+"<br /><br /></div>");
}
function initMenu() {
	$('#navlist').show();

}
function form_initialize(){
	$('.form_all .input').each(function(){		
		if(this.value=='' || this.value==this.title){
			$(this).val(this.title);
			$(this).removeClass('input_error').addClass('input_temp');
		}
		if(this.id=='confirmdate'){
			$(this).focus(function() {
				if (this.value == this.title){ 
					$(this).removeClass('input_temp').addClass('input');
					this.value = '';
				}
				$(this).dpDisplay();
			});
			$(this).blur(function() {
			if ($.trim(this.value) == ''){
				$(this).addClass('input_temp');
				this.value = (this.title? this.title : '');
			}
    		});
		}else if(this.id=='phone'){
			$(this).focus(function() {
				if (this.value == this.title ){ 
					$(this).removeClass('input_temp');
					this.value = dialing_for_contact;
				}
				$(this).dpDisplay();
			});
			$(this).blur(function() {
			if ($.trim(this.value) == '' || this.value==dialing_for_contact){
				$(this).addClass('input_temp');
				this.value = (this.title? this.title : '');
			}
    		});
		}else{
			$(this).focus(function() {
				if (this.value == this.title){ 
					this.value = '';
					$(this).removeClass('input_temp');
				}
			});
			$(this).blur(function() {
			if ($.trim(this.value) == ''){
				$(this).addClass('input_temp');
				this.value = (this.title?this.title:'');
			}
    		});
		}
		
	});
}
function get_countries(){
	$('#country_for_all').empty().hide();
	$('#city_for_all').empty().hide();
	$('#country_loader').show();
	 $("<option/>").attr({"value":'',"text":contact_all_select_country}).appendTo("#country_for_all");
	$.ajax({
		   type: "POST",
		   url: "/query/get_countries_for_contact",
		   data: 'branch_id='+active_branch_id,
		   dataType:"json",
		   success: function(data){
				$('#country_for_all').show();
				$('#country_loader').hide();
				$('#country_for_all').removeClass('input_temp');
				$.each(data.items, function(i,item){
					  $("<option/>").attr({"value":item.country,"text":item.country_name}).appendTo("#country_for_all");
				});
		   }
		 });
	}
function get_cities(country_id){
	if(country_id!=''){
	$('#city_for_all').empty().hide();
	$('#city_loader').show();
	
		$("<option/>").attr({"value":'',"text":contact_all_select_city}).appendTo("#city_for_all");
		$.ajax({
		   type: "POST",
		   url: "/query/get_cities_for_contact",
		   data: 'branch_id='+active_branch_id+'&country_id='+country_id,
		   dataType:"json",
		   success: function(data){
			$('#city_for_all').removeClass('input_temp');
			$('#city_for_all').show();
			$('#city_loader').hide();
				$.each(data.items, function(i,item){
					  $("<option/>").attr({"value":item.region_id,"text":item.region}).appendTo("#city_for_all");
				});
		   }
		 });
		}else{
			return false;
			}
	}
function show_hide(id){
	if(id==1)
		$("#floating_link").show();
	else
		$("#floating_link").hide();
	}
