$(function(){

	//check to see if menu exists 
	
	if($('#admin_menu')){
			$('#admin_menu a').click(function(){
				//clear div if populated 
				$('#main_content').hide();
				$('#loader').show();
				var holder;
				$.ajax(
					{type:"POST",
					 url:"processor.php",
					 dataType:"text/html",
					 data: $(this).attr("id"),
					 success: function(holder)
					 	{ 
					 	  $('#loader').hide(); 
					 	  $('#main_content').html(holder).show();
					 	} 
					});
			});
			return false;
	}//end if
});

billing = function(){
	
	if( $('#same_as_billing').val() == 'Y'){
		$('#same_as_billing').val('N');
		$('#billing_form').slideDown();
	}else{
		$('#same_as_billing').val('Y');
		$('#billing_form').slideUp();
	}//end if
	
}//end 

function checkMail(x)
{
	
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else return false;
}


saveNewUser = function(){


	//validation
	var message = '';
	$('#name_input input').each(function(){
		if (this.id == 'username') {
			var isCorrect = checkMail(this.value);
			if (isCorrect === false){
				message = message+'Please enter a valid Email Address. \n';
				$('#'+this.id).css('border-color','red');

			}
			
			//ajax check user name
			$.ajax({ 
			  type: "POST", 
			  url: "processor.php", 
			  data: "check="+this.value, 
			  dateType: "text/html",
			  async: false,
			  success: function(msg){ 
			    if(msg == 'no'){
			    
			    	message = message+'Email addresss is used by another user. \n';
			    	$('#'+this.id).css('border-color','red');
			 	 } 
			 	 }
			});
			
			
		} else {
			switch(this.id){
				case 'c_name':
					var title = 'Clinic Name';
				break;
				case 'f_name':
					var title = 'First Name';
				break;
				case 'l_name':
					var title = 'Last Name';
				break;
				case 'password':
					var title = 'Password';
				break;
			}//end switch

			if(this.value == ''){
				message = message+title+' must be filled in. \n';
				$('#'+this.id).css('border-color','red');
			} else {
				$('#'+this.id).removeAttr('style');
			}
		}//end if

	});//end 
//shipping

 $('#shipping_form input').each(function(){
 	if( this.id !== 'phone2' && this.id !== 'fax' && this.id !== 'address2' ){
 		if(this.value == ''){
 			message = message+'Shipping '+this.id.toUpperCase()+' must be filled in. \n';
 			$('#shipping_form #'+this.id).css('border-color','red');
 			
 		} else {
 			$('#shipping_form #'+this.id).removeAttr('style');
 		}//end
 	}//end
 
 });

if( document.getElementById('same_as_billing').value == 'N' ){
 $('#billing_form input').each(function(){
 	if( this.id !== 'phone2' && this.id !== 'fax' && this.id !== 'address2' ){
 		if(this.value == ''){
 			message = message+'Billing '+this.id.toUpperCase()+' must be filled in. \n';
 			$('#billing_form #'+this.id).css('border-color','red');
 			
 		} else {
 			$('#billing_form #'+this.id).removeAttr('style');
 		}//end
 	}//end
 
 });

}

	if(message != ''){
		alert(message);
		return false;
	}




	var post_data = 'request=users&proc=saveNewUser';
	$('#shipping_form input').each(function(){
		post_data = post_data+'&shipping_'+this.id+'='+this.value;
	});
	
 	if( $('#same_as_billing').val() == 'N'){
 		post_data = post_data+'&billing=N';
 		
		$('#billing_form input').each(function(){
		post_data = post_data+'&billing_'+this.id+'='+this.value;
		});
	} else {
		post_data = post_data+'&billing=Y';
 		
		$('#shipping_form input').each(function(){
		post_data = post_data+'&billing_'+this.id+'='+this.value;
		});
	}
	var c_name = $('#c_name').val();
	var user = $('#username').val();
	var pass = $('#password').val();
	var user_type = $('#user_type').val();
	var f_name  = $('#f_name').val();
	var l_name = $('#l_name').val();
	post_data = post_data+'&username='+user+'&password='+pass+'&user_type='+user_type;
	post_data = post_data+'&f_name='+f_name+'&l_name='+l_name+'&c_name='+c_name;
	
	$.ajax(
			{type:"POST",
			 url:"processor.php",
			 dataType:"html",
			 data: post_data,
			 success: function()
			 	{ 
			 	  $('#main_content').html('<strong> Saved </strong>');
			 	} 
			});
}

showOrder = function(num){
	
	var post_data = 'request=review&proc=po&order='+num; 
	$.ajax(
			{type:"POST",
			 url:"processor.php",
			 dataType:"html",
			 data: post_data,
			 success: function(msg)
			 	{ 
			 	  $('#main_content').html(msg);
			 	} 
			});
	return true;
}

editUser = function(id){
		var post_data = 'request=users&proc=doEdit&id='+id; 
		$.ajax(
			{type:"POST",
			 url:"processor.php",
			 dataType:"html",
			 data: post_data,
			 success: function(msg)
			 	{ 
			 	  $('#main_content').html(msg);
			 	} 
			});


}//end

saveEdit = function(id){

	//quick user validation
		//ajax check user name
		if($('#username').val() != $('#orgUsername').val() ){
		  var message = '';
			$.ajax({ 
			  type: "POST", 
			  url: "processor.php", 
			  data: "check="+$('#username').val(), 
			  dateType: "text/html",
			  async: false,
			  success: function(msg){ 
			 
			    if(msg == 'no'){
			    	
			    	 message = message+'Email addresss is used by another user. Please use a different address. \n';
			    	
			    //	alert(message);
			    //	$('#'+this.id).css('border-color','red');
			    	
			 	 } 
			 	 }
			});

		}
// had to trim the message, for some reason it
// was coming through as a space and throwing no error
// BVH 10/5
if($.trim(message) != ''){
 	alert(message);
 	return false;
 }
	var post_data = "request=users&proc=saveEdit";
	
	$('#name_input input').each(function(){
		post_data = post_data+'&'+this.id+'='+escape(this.value);
	});//end 

//shipping

 $('#shipping_form input').each(function(){
 	post_data = post_data+'&shipping_'+this.id+'='+escape(this.value);
 });

 $('#billing_form input').each(function(){
 	post_data = post_data+'&billing_'+this.id+'='+escape(this.value);
 
 });
 
 post_data = post_data+'&t_id='+$('#user_type').val();
 
 
	post_data = post_data+'&id='+id;
	$.ajax(
			{type:"POST",
			 url:"processor.php",
			 dataType:"text/html",
			 data: post_data,
			 success: function(msg)
			 	{ 
			 	  $('#main_content').html(msg);
			 	} 
			});
}//end

deleteUser = function(id){

	if( confirm("Are you SURE you want to delete this user?") ){
	

	var post_data = 'request=users&proc=inactive&id='+id;
		$.ajax(
			{type:"POST",
			 url:"processor.php",
			 dataType:"html",
			 data: post_data,
			 success: function(msg)
			 	{ 
			 	  $('#main_content').html(msg);
			 	} 
			});
	}else{
		return false;
		}
}















