// JavaScript Document
$.errorAlert = function(t, m){
		$('#dialog-error-msg').html(m);
		$( "#dialog-error").dialog({resizable: false,
												height:200,
												position: ['center',150],
												modal: true,
												title: t
										
											 });
	};
	
$.notify = function(m){
	e=$('#notify-div');
	e.html(m);
	e.slideDown("slow", function(){ setTimeout('e.slideUp("slow");',5000); });
	}
	
$.updateCCdata = function(loginid,ccid,datatype,txt) {
	$.post('/ajax/addCC.php',
		   {c: 'edit',
			loginid: loginid,
			ccid: ccid,
			type: datatype,
			value: txt},
		   function(data){
				d = $.json.decode(data);
				if(d.status=='error'){
					$.notify(d.message);
					if(datatype == 'name'){
						t='#addName'+ccid;
					}else if(datatype == 'position'){
						t='#addPos'+ccid;
					}else if(datatype == 'email'){
						t='#addEmail'+ccid;
					}
					if(datatype == 'email'){
						$(t).focus();
					}
				}
			});
};
	

$(document).ready(function(){
						   
	//$(document).dblclick(function(){
		//alert($(window).width());
	//});
	
	
	$('.hh2').click(function(){
			$(this).next('.dtoggle').slideToggle();							
	});

	
	$(window).scroll(function(){
		var offset = $(window).scrollTop() + 'px';
		$('#notify-div').css('top',offset);				  
	});
						   
	$('.pane, .subpanel').corner();
	$(' #navPanel .link, #navPanel .link_sel, .hh1').corner("top");
	$('.buttonGr, .buttonRe, .buttonYe').corner();
	
	$('.addCC').focus(function(e){
							  
					if($(this).attr('id')=='ccAddName'){
						if($(this).val()!='name'){
							return;
						}
					}
					else if($(this).attr('id')=='ccAddPos'){
						if($(this).val()!='position'){
							return;
						}
					}
					else if($(this).attr('id')=='ccAddEmail'){
						if($(this).val()!='email'){
							return;
						}
					}
					$(this).val('');
					return;
				}).blur(function(e){
					if($(this).val()==''){
						if($(this).attr('id')=='ccAddName'){
							$(this).val('name');
						}
						else if($(this).attr('id')=='ccAddPos'){
							$(this).val('position');
						}
						else if($(this).attr('id')=='ccAddEmail'){
							$(this).val('email');
						}
						return;
					}
				});
	$('#ccAdd').click(function(e){
						
						$('#ccAdd').attr('disabled',true);
						if($('#ccAddName').val()==''||$('#ccAddName').val()=='name'){
							$('#ccAddName').css('background-color','#fffdbb');
							$('#ccAdd').attr('disabled',false);
							return;
						}
						if($('#ccAddPos').val()==''||$('#ccAddPos').val()=='position'){
							$('#ccAddPos').css('background-color','#fffdbb');
							$('#ccAdd').attr('disabled',false);
							return;
						}
						if($('#ccAddEmail').val()==''||$('#ccAddEmail').val()=='email'){
							$('#ccAddEmail').css('background-color','#fffdbb');
							$('#ccAdd').attr('disabled',false);
							return;
						}
						$('.addCC').css('background-color','#fff');
						$.post('/ajax/addCC.php',
							   {c:'add',
							    loginid: $('#loginid').val(),
								name: $('#ccAddName').val(),
								position: $('#ccAddPos').val(),
								email: $('#ccAddEmail').val()},
							   function(data){
								   		try{
								   			var d = $.json.decode(data);
											$.errorAlert(d.status, d.message);
											$('#ccAdd').attr('disabled',false);
										}catch(err){
											$('#ccDiv').html(data);
											$( "#dialog-confirm-ccnotify" ).dialog({resizable: false,
																			height:200,
																			position: ['center',150],
																			modal: true,
																			buttons: {
																				"Send": function() {
																					$( this ).dialog( "close" );
																					$.post('/ajax/notify.php',
													   {type: 'cc-notify',
													   loginid : $('#loginid').val(),
													    name: $('#ccAddName').val(),
														email: $('#ccAddEmail').val(),
														company: $('#ccAddCompany').val()},
														function(data){
															try{
																d = $.json.decode(data);
																$.notify(d.message);
																$('#ccAddName').val('name');
																$('#ccAddPos').val('position');
																$('#ccAddEmail').val('email');
																$('#ccAdd').attr('disabled',false);
																		
															}catch(err){
																$.notify(data);
																$('#ccAdd').attr('disabled',false);
															}
														});
																				},
																				Cancel: function() {
																					$( this ).dialog( "close" );
																					$('#ccAdd').attr('disabled',false);
																					$('#ccAddName').val('name');
																$('#ccAddPos').val('position');
																$('#ccAddEmail').val('email');
																				}
																			}
																		});
											
											
											
										}
								   });
					 });
	
	$('.butdelcc').live('click',function(e){
		ab = $(this);
		$( "#dialog-confirm-ccdelete" ).dialog({resizable: false,
												height:200,
												position: ['center',150],
												modal: true,
												buttons: {
													"Delete": function() {
														$( this ).dialog( "close" );
														var ccidLong = $(ab).attr('id');	
														var ccid = ccidLong.match(/\d+/);
														$.post('/ajax/addCC.php',
															   {c: 'delete',
																loginid: $('#loginid').val(),
																idcc: ccid.toString()},
															   function(data){
																   try{
																		var d = $.json.decode(data);
																		$.errorAlert(d.status, d.message);
																	}catch(err){
																		$('#ccDiv').html(data);
																	}
															   });
														
													},
													Cancel: function() {
														$( this ).dialog( "close" );
													}
												}
											 });
		
		
		
	});
	

	
	$('.nameCC, .posCC, .emailCC').blur(function(e){
		if(!$(this).val()){
			$(this).focus();
		}
	});

	$('.nameCC').keyup(function(){
	  $(this).doTimeout( 'text-type', 1000, function(){
		if ($(this).val()) {
			var ccid = $(this).attr('id').match(/\d+/).toString();
			var name = $(this).val();
			$.updateCCdata($('#loginid').val(),ccid,'name',name);
		}
	  });
	});
	$('.posCC').keyup(function(){
	  $(this).doTimeout( 'text-type', 1000, function(){
		if ($(this).val()) {
			var ccid = $(this).attr('id').match(/\d+/).toString();
			var pos = $(this).val();
			$.updateCCdata($('#loginid').val(),ccid,'position',pos);
		}
	  });
	});
	$('.emailCC').keyup(function(){
	  $(this).doTimeout( 'text-type', 1000, function(){
		if ($(this).val()) {
			var ccid = $(this).attr('id').match(/\d+/).toString();
			var email = $(this).val();
			$.updateCCdata($('#loginid').val(),ccid,'email',email);
		}
	  });
	});
	
	
	

	
	
	
	
	
});
