function add_message(username,date){

	  $.facebox.loading()


$.ajax({
	
    url: "../include/calendar/planner/planner.php?",
    type: "POST",
    data: "username="+username+"&date="+date,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

		  var content= msg;
		  jQuery.facebox(content); 
		  $("#loadingi").hide();
   }
  });

}

function add_event(username,date){

	   $("#loadingi").show();

		var id_counter= $("#id_counter",top.document).val();
		var sms_count=id_counter;
	//	var code= $("#country_selector",top.document).val();
		var tel_string="";
		while(id_counter>0){
			var temp_number= $("#tel_"+id_counter,top.document).val();
			var country_code= $("#code_"+id_counter,top.document).val();
			if(temp_number==""){
				sms_count--;
				id_counter--;
			//alert("Mobile number is not entered!");
			//$("#loadingi").hide();
			//return
			}
			else{
				//validate
				var i;
			    for (i = 0; i < temp_number.length; i++)
			    {   
			 
			        // Check that current character is number.
			        var c = temp_number.charAt(i);
			        if (((c < "0") || (c > "9"))){ 
			        	alert("Phone number is not correct!");
			        	$("#loadingi").hide();
			        	return
			        	}
			    }

			    var element=country_code+";"+temp_number+"^";
			  var tel_string=tel_string+element;
			id_counter--;
		  }
		}
	

		var id_counter_mail= $("#id_counter_mail",top.document).val();
		var mail_count=id_counter_mail;
		var mail_string="";
		while(id_counter_mail>0){
			var temp_email= $("#email_"+id_counter_mail,top.document).val();
			if(temp_email==""){
				id_counter_mail--;
				mail_count--;
			}
			else{
				if(!checkValidation(temp_email,'1')){
					   $("#loadingi").hide();
					   return;
				}
				else{
					temp_email=temp_email;
					id_counter_mail--;
					if(id_counter_mail==0){
					mail_string=mail_string+temp_email;
					}
					else{
					mail_string=mail_string+temp_email+";";
					}
					
				}

		  }
		}
		
		
	var username=username;
	var date= date;
	//var sms=$('#sms_checkbox').is(':checked'); 
	//var e_mail=$('#e_mail_checkbox').is(':checked'); 
	var sms_time= $("#sms_time",top.document).val();
	var start_hour= $("#start-hour",top.document).val();
	var start_minute= $("#start-minute",top.document).val();	
	//var end_hour= $("#end-hour",top.document).val();
	//var end_minute= $("#end-minute",top.document).val();		
	var title= $("#title",top.document).val();
 	var priority= $("#priority",top.document).val();
 	var description= $("#description",top.document).val();

	var start_time=start_hour+":"+start_minute;
	//var end_time=end_hour+":"+end_minute;

	if(title=="" || description==""){
		if(title==""){$('#title').css('border', '1px solid red');}
		else{$('#title').css('border', '1px solid silver');}
		if(description==""){$('#description').css('border', '1px solid red');}
		else{$('#description').css('border', '1px solid silver');}

		alert("Error! Incomplete form!");$("#loadingi").hide();
		}
	else{
		///check, if this is current day. If true, then check time to know that client is trying to add event in the past
		var d = new Date();
		var month=new Array(12);
		month[0]="01";month[1]="02";month[2]="03";month[3]="04";month[4]="05";month[5]="06";month[6]="07";month[7]="08";month[8]="09";month[9]="10";month[10]="11";month[11]="12";

		var current_month=d.getMonth();
		var menesis=month[current_month];
		

		var date_current = d.getFullYear() + "/" + menesis + "/" + d.getDate();		
		
		if(date_current==date){
			var curr_hour = d.getHours();
			var curr_min = d.getMinutes();
	
			if(start_hour<=curr_hour && start_minute<=curr_min){
				alert("You are adding event in the past!");
			}
		}

		
$.ajax({
	
    url: "../include/calendar/planner/add_event.php?",
    type: "POST",
    data: "username="+username+
		  "&date="+date+
		  "&start_time="+start_time+
		  //"&end_time="+end_time+
		  "&title="+title+
		  "&priority="+priority+
		  //"&sms="+sms+
		 // "&e_mail="+e_mail+
		  "&sms_time="+sms_time+
		  "&tel_string="+tel_string+
		 // "&country_code="+code+
		  "&sms_count="+sms_count+
		  "&mail_count="+mail_count+ 
		  "&mail_string="+mail_string+ 
		  "&description="+description,
		  
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

 $("#message_frame").html(msg);
 // alert(msg);
   calendar_load(username);
   $("#loadingi").hide();

   
   
   if(tel_string!=""){
	   calendar_load(username,1);
	   new_contacts(tel_string,username,sms_count);
   }
   else{
	   calendar_load(username,1);
	   jQuery(document).trigger('close.facebox');  
   }
   
   
   }
  });
}
}


function calendar_load(username,advanced,move){

	 $("#loadingi_calendar").show();

$.ajax({
	
    url: "../include/calendar/calendar_update.php?",
    type: "POST",
    data: "move="+move + "&username="+username+ "&advanced="+advanced,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
   //$("#translit_buffer").text(msg);
   //alert(msg);
   
 $("#calendar_frame").html(msg);
 $("#loadingi").hide();
 $("#loadingi_calendar").hide();
   }
  });
	
}


function edit_message(username,date,actions){
	//alert(month);
	   $("#loadingi").show();

$.ajax({
	
    url: "../include/calendar/planner/planner_edit.php?",
    type: "POST",
    data: "username="+username+"&date="+date+"&actions="+actions,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
   //$("#translit_buffer").text(msg);
   //alert(msg);

		  var content= msg;
		  jQuery.facebox(content);  
		   $("#loadingi").hide();

	      if(actions=="1"){
		   calendar_load(username,1);
		   $("#loadingi").show();
		   }
	 }
  });

}



function delete_event(message_id,username,date){

	   $("#loadingi").show();

$.ajax({
	
    url: "../include/calendar/planner/delete_event.php?",
    type: "POST",
    data: "username="+username+"&message_id="+message_id+"&date="+date,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
   //$("#translit_buffer").text(msg);
   //alert(msg);
   
 $(" #all_events").html(msg);
  calendar_load(username,1);
  edit_message(username,date,1);
  // $("#message_frame").show();
   
 //  $("div #elements").kaut kas
   
   }
  });






}



function edit_event(message_id,username,date,from_where){


	   $("#loadingi").show();

	 if(from_where!=0){var edit=0;}else{edit=1;}


$.ajax({
	
    url: "../include/calendar/planner/load_event.php?",
    type: "POST",
    data: "username="+username+"&message_id="+message_id+"&date="+date+"&edit="+edit,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
		 	
		 var facebox=$('#facebox_overlay').length;
		 
		 
		 
		 if(from_where==0){
			 $("#message_frame").html(msg);
			  $("#loadingi").hide();
			 }
		 else{
			  var content= msg;
			  jQuery.facebox(content);  
			  $("#loadingi").hide();
			 }

		 var date_time=15;
		 var title = $("#title").val();
		 title_lenght=title.length;

		 var description = $("#description").val();
		 description_lenght=description.length;

		 var total= description_lenght + title_lenght + date_time;
		 //alert(total);
		 $("#max_limit").text(total + "/160");
		 if (total > 160){
		    $("#max_limit").css("color", "red");

		 }
		 else{
		    $("#max_limit").css("color", "black");

		 }

   }
  });


}







function save_changes_event(message_id,username,date){


	   $("#loadingi").show();

	 				var id_counter= $("#id_counter",top.document).val();
	 				var sms_count=id_counter;
	 			//	var code= $("#country_selector",top.document).val();
	 				var tel_string="";
	 				while(id_counter>0){
	 					var temp_number= $("#tel_"+id_counter,top.document).val();
	 					var country_code= $("#code_"+id_counter,top.document).val();
	 					if(temp_number==""){
	 						
	 						sms_count--;
	 						id_counter--;
	 						

	 					}
	 					else{
	 						//validate
	 						var i;
	 					    for (i = 0; i < temp_number.length; i++)
	 					    {   
	 					 
	 					        // Check that current character is number.
	 					        var c = temp_number.charAt(i);
	 					        if (((c < "0") || (c > "9"))){ 
	 					        	alert("Phone number is not correct!");
	 					        	$("#loadingi").hide();
	 					        	return
	 					        	}
	 					    }

	 					    var element=country_code+";"+temp_number+"^";
	 					  var tel_string=tel_string+element;
	 					id_counter--;
	 				  }
	 				}
	 			

	 				var id_counter_mail= $("#id_counter_mail",top.document).val();
	 				var mail_count=id_counter_mail;
	 				var mail_string="";
	 				while(id_counter_mail>0){
	 					var temp_email= $("#email_"+id_counter_mail,top.document).val();
	 					if(temp_email==""){
	 						id_counter_mail--;
	 						mail_count--;
	 					}
	 					else{
		 					if(!checkValidation(temp_email)){
		 					   $("#loadingi").hide();
		 						return;
		 					}
		 					else{
			 					temp_email=temp_email;
			 					id_counter_mail--;
			 					if(id_counter_mail==0){
			 					mail_string=mail_string+temp_email;
			 					}
			 					else{
			 					mail_string=mail_string+temp_email+";";
			 			  }
	 					}
 					  }
	 				}

	 			
	var username=username;
	var date= date;
	var message_id=message_id;
	var sms_time= $("#sms_time",top.document).val();	
	var start_hour= $("#start-hour",top.document).val();
	var start_minute= $("#start-minute",top.document).val();	
	//var end_hour= $("#end-hour",top.document).val();
	//var end_minute= $("#end-minute",top.document).val();		
	var title= $("#title",top.document).val();
 	var priority= $("#priority",top.document).val();
 	var description= $("#description",top.document).val();

	var start_time=start_hour+":"+start_minute;
	//var end_time=end_hour+":"+end_minute;


	
	
	if(title=="" || description==""){
		if(title==""){$('#title').css('border', '1px solid red');}
		else{$('#title').css('border', '1px solid silver');}
		if(description==""){$('#description').css('border', '1px solid red');}
		else{$('#description').css('border', '1px solid silver');}

		alert("Error! Incomplete form!");$("#loadingi").hide();
		}
	else{
		
		///check, if this is current day. If true, then check time to know that client is trying to add event in the past
		var d = new Date();
		var month=new Array(12);
		month[0]="01";month[1]="02";month[2]="03";month[3]="04";month[4]="05";month[5]="06";month[6]="07";month[7]="08";month[8]="09";month[9]="10";month[10]="11";month[11]="12";

		var current_month=d.getMonth();
		var menesis=month[current_month];
		

		var date_current = d.getFullYear() + "/" + menesis + "/" + d.getDate();		
		
		if(date_current==date){
			var curr_hour = d.getHours();
			var curr_min = d.getMinutes();
	
			if(start_hour<=curr_hour && start_minute<=curr_min){
				alert("You are adding event in the past!");
			}
		}

		
$.ajax({
	
    url: "../include/calendar/planner/save_changes_event.php?",
    type: "POST",
    data: "username="+username+
          "&message_id="+message_id+
		  "&date="+date+
		  "&start_time="+start_time+
		  //"&end_time="+end_time+
		  "&title="+title+
		  "&priority="+priority+
		//  "&sms="+sms+
		//  "&e_mail="+e_mail+
		  "&sms_time="+sms_time+
		  "&tel_string="+tel_string+
	//	  "&country_code="+code+
		  "&sms_count="+sms_count+
		  "&mail_count="+mail_count+ 
		  "&mail_string="+mail_string+ 
		  "&description="+description,
		  
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

 $("#info_field").html(msg);
 $("#loadingi").hide();
 if(tel_string!=""){
	 calendar_load(username,1);
	   new_contacts(tel_string,username,sms_count);
 }
 else{
	 	calendar_load(username,1);
	   jQuery(document).trigger('close.facebox');  
 }

   }
  });
 }
}




function add_deffined(username,date,action,def_spend_id){
	 $("#loadingi").show();
	 var money_cost;
	 	var def_name= $("#def_name",top.document).val();
		if(def_name=="" && action=='1'){$('#def_name').css('border', '1px solid red');
		alert("Error! Incomplete form!");$("#loadingi").hide();
		return
		}
		else{$('#def_name').css('border', '1px solid silver');}
		
		
		if(action=="3"){
			var r=confirm("Deleteing the deffined spending, it will be erased from all days. Proceed?");
			if (r)
			  {}else{
			  $("#loadingi").hide();
				return
			  }
			  }
	 
		if(action=="4"){
			money_cost= $("#money_cost"+def_spend_id,top.document).val();
		}
		
		
		
	 
	 $.ajax({
			url: "../include/calendar/money/deffined.php?",
		    type: "POST",
		    data: "username="+username+
		    	  "&date="+date+
		    	  "&action="+action+
		    	  "&def_spend_id="+def_spend_id+
		    	  "&money_cost="+money_cost+
		    	  "&def_name="+def_name,
		    cache: false,
			
			error: function(){ alert("error"); },  
			complete: function(){ //alert("OK");
			 } ,
			
			success: function(msg){
		 if(action==1 || action==3) {  
			 // $("#info_field").html(msg);
			  //$("#loadingi").hide();
			$("#def_name",top.document).val("");
			  add_deffined(username,date,2);
		 }
		if(action==2){
			
			  $("#load_defined_cost_frame").html(msg);
			  $("#loadingi").hide();
		}
		if(action=="4"){
			money(username,date,'11')
		}
		   
		   }
		  });	 
	 
	 
}



function money(username,date,action,money_id){

	 $("#loadingi").show();

if(action==2){
	var money_event= $("#money_event",top.document).val();
	var money_cost= $("#money_cost",top.document).val();
	if(money_event=="" || money_cost==""){
		alert("Error! Incomplete form!");
		$("#loadingi").hide();
		return;
	}
	
}


$.ajax({
	
    url: "../include/calendar/money/money.php?",
    type: "POST",
    data: "username="+username+"&date="+date+"&action="+action+"&money_event="+money_event+"&money_cost="+money_cost+"&money_id="+money_id,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
 if(action==1) { 
	  var content= msg;
	  jQuery.facebox(content);  
	  $("#loadingi").hide();
      $("#loading").hide();
      add_deffined(username,date,2);
 }
if(action==2 || action==3){
	
	money(username,date,11);
}
if(action==11) { 
	
	  $("#message_frame").html(msg);
	  add_deffined(username,date,2);
}  

   }
  });

}






function money_diagram(username,date,action,type,action2){

   $("#loadingi").show();

   if(type==""){
	   type=$("#type").val(); 
	   //alert(type);
   }

$.ajax({
	
    url: "../include/calendar/money/money_diagram.php?",
    type: "POST",
    data: "username="+username+
    	  "&date="+date+
    	  "&action="+action+
    	  "&action2="+action2+
    	  "&type="+type,
    cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
		
		
if(action==0 || action2==0){
	  var content= msg;
	  jQuery.facebox(content);  
	
}
else{
	 $("#diagram_frame").html(msg);
}

		 

$(document).ready(function () {
	
$("#datepick").mousedown(function() { 
$('#datepick').datepick({dateFormat: 'yy/mm/dd',firstDay: 1});

 });

});

$("#loadingi").hide();
   }

  });
}

function diagram_jump(username,action,type){


	var date= $("#datepick",top.document).val();

	if(date==""){
		alert("Click in the date field and pick the date!");
		
		return
	}
	else{
	money_diagram(username,date,action,type);
	}
}


///catalog function
function catalog(contact_id,username){

	if(contact_id==""){
	  $.facebox.loading()
	}
$.ajax({
	
  url: "../include/calendar/catalog/catalog.php?",
  type: "POST",
  data: "username="+username+
  		"&contact_id="+contact_id,
  cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

			if(contact_id==""){
		  jQuery.facebox(msg); 
			}
			else{
		  $("#message_frame").html(msg);
			}
		  load_all_contact(username);
 }
});

}


function add_contact(username){
	  $("#loadingi").show();
	//  $.facebox.loading()
	var code= $("#code",top.document).val();
	var tel= $("#tel",top.document).val();	
	var contact_name= $("#contact_name",top.document).val();	

	
//validate fields
var i;
	if(tel=="" || contact_name==""){

			if(contact_name==""){$('#contact_name').css('border', '1px solid red');}
			else{$('#contact_name').css('border', '1px solid silver');}
			if(tel==""){$('#tel').css('border', '1px solid red');}
			else{$('#tel').css('border', '1px solid silver');}
			alert("Error! Incomplete form!");
			$("#loadingi").hide();
			return
			}
	
	for (i = 0; i < tel.length; i++)
	{   
	    // Check that current character is number.
	    var c = tel.charAt(i);
	    if (((c < "0") || (c > "9"))){
	    	$('#tel').css('border', '1px solid red');
	    	alert("Phone number is not correct!");
	    	$("#loadingi").hide();
	    	return
	    	}
	    else{
	    	$('#tel').css('border', '1px solid silver');
	    }
	}	

$.ajax({
	
url: "../include/calendar/catalog/add_contact.php?",
type: "POST",
data: "username="+username+
	  "&code="+code+
	  "&tel="+tel+
	  "&contact_name="+contact_name,
cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

	
		$("#tel",top.document).val("");	
		$("#contact_name",top.document).val("");
		 
		 load_all_contact(username);
}
});

}



function delete_contact(contact_id,username){
	  $("#loadingi").show();
	
$.ajax({
	
url: "../include/calendar/catalog/delete_contact.php?",
type: "POST",
data: "contact_id="+contact_id,
cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
		 load_all_contact(username);
}
});

}


function load_all_contact(username){
	  $("#loadingi").show();
	
$.ajax({
	
url: "../include/calendar/catalog/load_all_contact.php?",
type: "POST",
data: "username="+username,
cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

		  //var content= msg;
		 //alert(msg);
		  $("#all_contacts_frame").html(msg);
		  $("#loadingi").hide();
}
});

}


function load_contact(contact_id,username){
	  $("#loadingi").show();
	
$.ajax({
	
url: "../include/calendar/catalog/load_contact.php?",
type: "POST",
data: "username="+username+
	  "&contact_id="+contact_id,
cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

		  //var content= msg;
		 //alert(msg);
		  $("#contact_input_row").html(msg);
		  $("#loadingi").hide();
}
});

}



function save_changes_contact(contact_id,username){
	  $("#loadingi").show();
		//  $.facebox.loading()
		var code= $("#code",top.document).val();
		var tel= $("#tel",top.document).val();	
		var contact_name= $("#contact_name",top.document).val();	

		//validate fields
		var i;
			if(tel=="" || contact_name==""){

					if(contact_name==""){$('#contact_name').css('border', '1px solid red');}
					else{$('#contact_name').css('border', '1px solid silver');}
					if(tel==""){$('#tel').css('border', '1px solid red');}
					else{$('#tel').css('border', '1px solid silver');}
					alert("Error! Incomplete form!");
					$("#loadingi").hide();
					return
					}
			
			for (i = 0; i < tel.length; i++)
			{   
			    // Check that current character is number.
			    var c = tel.charAt(i);
			    if (((c < "0") || (c > "9"))){
			    	$('#tel').css('border', '1px solid red');
			    	alert("Phone number is not correct!");
			    	$("#loadingi").hide();
			    	return
			    	}
			    else{
			    	$('#tel').css('border', '1px solid silver');
			    }
			}
		
		
	$.ajax({
		
	url: "../include/calendar/catalog/save_changes_contact.php?",
	type: "POST",
	data: "username="+username+
		  "&code="+code+
		  "&tel="+tel+
		  "&contact_name="+contact_name+
		  "&contact_id="+contact_id,
	cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
		catalog(contact_id,username);

}
});

}


function add_to_list(row_id){
	
	  $("#loadingi").show();
		
	  $.ajax({
	  	
	  url: "../include/calendar/catalog/load_all_contact_small.php?",
	  type: "POST",
	  data: "row_id="+row_id,
	  cache: false,
	  	
	  	error: function(){ alert("error"); },  
	  	complete: function(){ //alert("OK");
	  	 } ,
	  	
	  	success: function(msg){

	  		  //var content= msg;
	  		 //alert(msg);
	  		  $("#all_contacts").html(msg);
	  		  $("#all_contacts").show();	
	  		  $("#loadingi").hide();
	  }
	  });
}

function load_contact_in(contact_id,username,row_id){
	
	  $("#loadingi").show();
		
		var id_counter= $("#id_counter",top.document).val();
		var sms_count=id_counter;
	  
	  $.ajax({
	  	
	  url: "../include/calendar/catalog/load_contact_in.php?",
	  type: "POST",
      data: "username="+username+
      		"&contact_id="+contact_id+
      		"&sms_count="+sms_count+
      		"&row_id="+row_id,
	  cache: false,
	  	
	  	error: function(){ alert("error"); },  
	  	complete: function(){ //alert("OK");
	  	 } ,
	  	
	  	success: function(msg){

	  		  $("#row_tel"+row_id).html(msg);
	  		$("#all_contacts").hide();
	  		  $("#loadingi").hide();
	  }
	  });
	
}




function close_small_list(){
	  $("#all_contacts").hide();
}



function  new_contacts(tel_string,username,sms_count){
	  $.facebox.loading()
	  $("#loadingi").show();
		
  
	  $.ajax({
	  	
	  url: "../include/calendar/catalog/new_contacts.php?",
	  type: "POST",
	  data: "username="+username+
		    "&tel_string="+tel_string+
		    "&sms_count="+sms_count,
	  cache: false,
	  	
	  	error: function(){ alert("error"); },  
	  	complete: function(){ //alert("OK");
	  	 } ,
	  	
	  	success: function(msg){

			  var content= msg;
			  if(content=='1'){
				calendar_load(username,1);
				jQuery(document).trigger('close.facebox');   
			  }
			  else{
			  jQuery.facebox(content); 
			  $("#loadingi").hide();
			  }
	  }
	  });	
	
}


function save_new_contact(username,row_id){
	
	  $("#loadingi").show();
		//  $.facebox.loading()
		var code= $("#code"+row_id,top.document).val();
		var tel= $("#tel"+row_id,top.document).val();	
		var contact_name= $("#contact_name"+row_id,top.document).val();	

		//validate fields
		var i;
			if(tel=="" || contact_name==""){

					if(contact_name==""){$('#contact_name'+row_id).css('border', '1px solid red');}
					else{$('#contact_name'+row_id).css('border', '1px solid silver');}
					if(tel==""){$('#tel'+row_id).css('border', '1px solid red');}
					else{$('#tel'+row_id).css('border', '1px solid silver');}
					alert("Error! Incomplete form!");
					$("#loadingi").hide();
					return
					}
			
			for (i = 0; i < tel.length; i++)
			{   
			    // Check that current character is number.
			    var c = tel.charAt(i);
			    if (((c < "0") || (c > "9"))){
			    	$("#tel"+row_id).css('border', '1px solid red');
			    	alert("Phone number is not correct!");
			    	$("#loadingi").hide();
			    	return
			    	}
			    else{
			    	$("#tel"+row_id).css('border', '1px solid silver');
			    	
			    }
			}
		
		
	$.ajax({
		
	url: "../include/calendar/catalog/add_contact.php?",
	type: "POST",
	data: "username="+username+
		  "&code="+code+
		  "&tel="+tel+
		  "&contact_name="+contact_name,
	cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){
		//catalog(contact_id,username);
		 $("#contact_input_row" + row_id).remove(); 
		 $("#loadingi").hide();
}
});
	
}



























//ADD or REMOVE MORE TEL NUMBER FIELDS
function addFormField(phone_country) {
	var id_x = $("#id_counter").val();
	
	if(id_x==0){
		id_x=1;
	}


	//validate


		var temp_number = $("#tel_"+id_x,top.document).val();
		
	var i;
		//if(temp_number==""){
			//	alert("Mobile number is not entered!");
			//	$("#loadingi").hide();
			//	return
				//}
    for (i = 0; i < temp_number.length; i++)
    {   
        // Check that current character is number.
        var c = temp_number.charAt(i);
        if (((c < "0") || (c > "9"))){ 
        	alert("Phone number is not correct!");
        	$("#loadingi").hide();
        	return
        	}
    }	

	id_x++;
	if(id_x < 21){
	
		$.ajax({
			
		    url: "../include/calendar/planner/code.php?",
		    type: "POST",
		    data: "phone_country="+phone_country,
		    cache: false,
			
			error: function(){ alert("error"); },  
			complete: function(){ //alert("OK");
			 } ,
			
			success: function(msg){
				var code_droplist=msg;
				var code= $("#country_selector",top.document).val();
				$("#additionaltelnum").append("<div id='row_tel" + id_x + "'><div class='tel_rinda'><div class='contact_from_catalog' id='contact_"+ id_x +"'></div><div class='code'><select id='code_"+id_x+"'style='width: 55px;'>"+code_droplist+"</select></div><input style='position: relative; float: left;width: 70px;' type='text' name='tel_" + id_x + "' id='tel_" + id_x + 
				"'><img  style='position: relative; float: left;margin-left: 2px;' id='cont_"+ id_x +"' class='mouse_pointer' onclick='add_to_list("+ id_x +")' src='../include/calendar/images/book_open.png'/><img src='../images/minus.png' class='mouse_pointer'  class='mouse_pointer' style='position: relative; float: left;margin-left: 2px;' onClick='removeFormField(\"#row_tel" + id_x + "\");' id='btn_" + id_x + "' /></div></div>");
				

				
				if(id_x > 2){
					
					var previusID = id_x - 1;
					removeButton(previusID);
				}
				
				$("#id_counter").val(id_x);
				//$("#totalEmails").val(id_x);
				//$("#additionaltelnum input").css();
		   }
		  });
		



	}
	else(
	alert("Maximal phone number fields reached!")		
	)
}

function removeFormField(row_id) {
	$(row_id).remove();
	var current_val = $("#id_counter").val();
	$("#tel_hide_" + current_val).remove();
	current_val--;
	$("#id_counter").val(current_val);
	$("#totalEmails").val(current_val);

	if(current_val >= 2){
	
		$("#cont_" + current_val).after("<img src='../images/minus.png'  style='position: relative; float: left; margin-left: 2px;' class='mouse_pointer' onClick='removeFormField(\"#row_tel" + current_val + "\");' id='btn_" + current_val + "' />");
	}
}

function removeButton(btn_id) {
	$("#btn_" + btn_id).remove();
}

function add_code(){
	
	var code= $("#country_selector",top.document).val();

	$("#code").html("+"+code);
	
}




//ADD or REMOVE MORE EMAIL FIELDS
function addFormField_mail() {
	var id_x = $("#id_counter_mail").val();
	
	
	var temp_email = $("#email_"+id_x,top.document).val();
	
	//!checkValidation(test)
	
//	if(!checkValidation(temp_email)){
	//	$("#loadingi").hide();
	//	return;
		
	//}
	//else{
	
	
	
	
	var id_x = $("#id_counter_mail").val();
	id_x++;
	if(id_x < 21){		
		$("#additionalEmails").append("<div id='row" + id_x + "'><div class='mail_rinda'><input type='text' name='email_" + id_x + "' id='email_" + id_x + 
		"'>&nbsp;<img src='../images/minus.png' class='mouse_pointer' onClick='removeFormField_mail(\"#row" + id_x + "\");' id='btn_mail" + id_x + "' /></div></div>");
		

		
		if(id_x > 2){
			
			var previusID = id_x - 1;
			removeButton_mail(previusID);
		}
		
		$("#id_counter_mail").val(id_x);
		//$("#totalEmails").val(id_x);
		$("#additionalEmails input").css("width", "180px");
	}
	else{
		alert("Maximum email fields reached!");
	}
 // }
}

function removeFormField_mail(row_id) {
	$(row_id).remove();
	var current_val = $("#id_counter_mail").val();
	$("#email_hide_" + current_val).remove();
	current_val--;
	$("#id_counter_mail").val(current_val);
	$("#totalEmails").val(current_val);
	
	if(current_val >= 2){
		$("#email_" + current_val).after("&nbsp;<img src='../images/minus.png' class='mouse_pointer' onClick='removeFormField_mail(\"#row" + current_val + "\");' id='btn_mail" + current_val + "' />");
	}
}

function removeButton_mail(btn_id) {
	$("#btn_mail" + btn_id).remove();
}


function limitText(limitField, limitNum){
	
	
	var domain=17;
	
	var title = $("#title").val();
	title_lenght=title.length;

	var description = $("#description").val();
	description_lenght=description.length;
	
	var total= description_lenght + title_lenght + domain;
	//alert(total);
	$("#max_limit").text(total + "/160");
   if (total > 160){
	   $("#max_limit").css("color", "red");
	   $("#max_limit").css("font-weight", "bold");
   }
   else{
	   $("#max_limit").css("color", "black");
	   $("#max_limit").css("font-weight", "normal");
   }
}





///E-mail validation


function checkValidation(formInput,advanced) {

   // if (typeof(formInput) != "object") {
   //     alert("Validation not supported on this browser.");
  //      return(false);
  //  }

    var message;

   // if (stringEmpty(formInput)) {
      //  message = "Error! There is no e-mail entered.";
     //   alert(message);
    //} else
    	if (noAtSign( formInput )) {
        message = "Error! The address \"" + formInput + "\" does not contain an '@' character.";
        alert(message);
    } else if (nothingBeforeAt(formInput)) {
        message = "Error! The address \"" + formInput;
        message += "\" must contain at least one character before the '@' character";
        alert(message);
    } else if (noLeftBracket(formInput)) {
        message = "Error! The address \"" + formInput;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        alert(message);
    } else if (noRightBracket(formInput)) {
        message = "Error! The address \"" + formInput;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        alert( message);
    } else if (noValidPeriod(formInput)) {
        message = "Error! The address \"" + formInput + "\" must contain a period ('.') character.";
        alert(message);
    } else if (noValidSuffix(formInput)) {
        message = "Error! The address \"" + formInput;
        message += "\" must contain a two, three or four character suffix.";
        alert(message);
    } else {
     //   message = "Success! The email address \"" + formInput + "\" validates OK.";
       // alert(message);
    	return (true);
    }

  //  var objType = typeof(formInput.focus);
    //if (objType == "object" || objType == "function") {
    //     formInput.focus();
    //}

   // return (false);
}

function checkValid (formField) {
    if ( checkValidation ( formField ) == true ) {
     //   alert ( 'E-Mail Address Validates OK' );
    }

    return ( false );
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
	
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}

/////NOte Java Scripts

function show_note(username,date){

	  $.facebox.loading()


$.ajax({
	
  url: "../include/calendar/note/note.php?",
  type: "POST",
  data: "username="+username+"&date="+date,
  cache: false,
	
	error: function(){ alert("error"); },  
	complete: function(){ //alert("OK");
	 } ,
	
	success: function(msg){

		  var content= msg;
		  jQuery.facebox(content); 
		  $("#loadingi").hide();
 }
});

}

function save_note(username,date){
	
	  $("#loadingi").show();
		
		var description= $("#description",top.document).val();

		if(description==""){
			alert("Error! Incomplete form!");$("#loadingi").hide();
			return
		}
		
	  $.ajax({
	  	
	  url: "../include/calendar/note/save_note.php?",
	  type: "POST",
    data: "username="+username+
    		"&date="+date+
    		"&description="+description,
	  cache: false,
	  	
	  	error: function(){ alert("error"); },  
	  	complete: function(){ //alert("OK");
	  	 } ,
	  	
	  	success: function(msg){

	  		  $("#info_field").html(msg);
	  		 calendar_load(username);
	  		  $("#loadingi").hide();
	 	}
	  });
}


function delete_note(username,date){
	
	  $("#loadingi").show();
	var r=confirm("Do you want to delete this note?");
	if (r)
	  {}else{
	  $("#loadingi").hide();
		return
	  }

		var description= "";

		
	  $.ajax({
	  	
	  url: "../include/calendar/note/save_note.php?",
	  type: "POST",
  data: "username="+username+
  		"&date="+date+
  		"&description="+description,
	  cache: false,
	  	
	  	error: function(){ alert("error"); },  
	  	complete: function(){ //alert("OK");
	  	 } ,
	  	
	  	success: function(msg){
	  		calendar_load(username);
	  	   jQuery(document).trigger('close.facebox'); 
	 	}
	  });
}











