function checkUsername()
{ 
  jQuery.post(siteurl+"customer/check_exists_email_ajax", { email: jQuery("#emailid").val() },
  function(data){
  	if(data=="1")
  	{
  		jQuery("#error").html('Email already exists in our records');
  		jQuery("#add").attr('disabled','disbaled');
  		jQuery("#add").attr('class','btnregistergraybig');
  		
  	}else
  	{
  		 
  		if(jQuery("#add").attr("disabled") != true){
  		 
	  		jQuery("#error").html('');
	  		jQuery("#add").attr('disabled','');
	  		jQuery("#add").attr('class','btnregisterbig');
  		}
  		else{
  		 if(checkLab()==1) {
  		jQuery("#error_lab").html('Lab already exists in our records.');
  		jQuery("#add").attr('disabled','disabled');
  		jQuery("#add").attr('class','btnregistergraybig');
  		
  		
  		 }
  		 else{
  		  	jQuery("#error").html('');
	  		jQuery("#add").attr('disabled','');
	  		jQuery("#add").attr('class','btnregisterbig');
  		  
  		 }
  		
  		}
	  		 
  	}
  	
  });
}
function checkLab()
{ 
  jQuery.post(siteurl+"customer/check_exists_lab_ajax", { company: jQuery("#company").val() },
  function(data){
  	if(data=="1")
  	{
  		jQuery("#error_lab").html('Lab already exists in our records.');
  		jQuery("#add").attr('disabled','disabled');
  		jQuery("#add").attr('class','btnregistergraybig');
  		
  	}else
  	{
  	 if(jQuery("#add").attr("disabled") != true){
  		jQuery("#error_lab").html('');
  		jQuery("#add").attr('disabled','');  		
  		jQuery("#add").attr('class','btnregisterbig');
  	 }
  	 		else{
  		 if(checkUsername()==1) {
  			jQuery("#error").html('Email already exists in our records');
   		jQuery("#add").attr('disabled','disbaled');
   		jQuery("#add").attr('class','btnregistergraybig');
  		
  		
  		 }
  		 else{
  		  		jQuery("#error_lab").html('');
      		jQuery("#add").attr('disabled','');  		
      		jQuery("#add").attr('class','btnregisterbig');
  		  
  		 }
  		
  		}
  		 
  	}
  	return data;
  }); 

}

function get_material(mid,pid,element_name)
{   
	if(jQuery("#products_id :selected").text()!="")
    {
    	jQuery("#main_head").html('<h2 class="headclass">'+jQuery("#products_id :selected").text()+'</h2>');
    }
	 jQuery.post(siteurl+"materials/get_materials_ajax_refined", { material_id: mid, product_id:pid },
	 
	 function(data){
	 
	 	  if(element_name!="")
	 	  jQuery("[name="+element_name+"]").html(data); 
	 	   
	 	  else	 	  
			jQuery("#material_id").html(data);
			get_shade(jQuery("[name="+element_name+"]").val(),element_name.split("material_id_")[1]);
			

			
				 //alert( jQuery("[name="+element_name+"]").val());
			success: getMaterialDtl(jQuery("#material_id").val());			
		}
	  );
}

function get_shade(mid,id)
{  

	 jQuery.post(siteurl+"materials/get_shade_ajax",{ material_id: mid },
	  function(data){	
	   if(data==1){
	   jQuery("#shadeval_"+id).attr("value",1);
	   jQuery("#shade_"+id).show()
	   }
	   else{
	    jQuery("#shadeval_"+id).attr("value",0);
	    jQuery("#shade_"+id).hide()
	   }
 	
		}
	  );
}
 
function get_product_name(mid)
{  
	 jQuery.post(siteurl+"materials/get_products_name_ajax",{ material_id: mid },
	  function(data){	
	   
    		jQuery("#main_head").html('<h2 class="headclass">'+data+'</h2>'); 
		}
	  );
}

function getMaterialDtl(mid)
{ 
	jQuery.post(siteurl+"materials/get_materials_dtl_ajax", { material_id: mid },
	 function(data){
	 		get_product_name(mid);
	 		jQuery("#material_dtl_td").html(data);			
		}
	  );
	
}

function addSubscribe()
{ 
	if (echeck(jQuery("#email").val())==false){
			jQuery("#email").select();
			jQuery("#email").focus()
			return false;
	}
	jQuery.post(siteurl+"newsletter_subscriber/add_ajax", { email: jQuery("#email").val() },
	 function(data){
	 	if(data==1)
			jQuery("#sub_msg").html('Email id has added to subscribe list');
	 
		else		 
			jQuery("#sub_msg").html('Email already exists in our records.');
		 
	 } );
	
}

function customer_register()
{
	 if(jQuery("#emailOptOut").is(':checked'))
		{
			emailOptOut=1;
		}else
		{
			emailOptOut=0;
		}
		alert(siteurl);
	 if(ValidatecustomerRegister() != false)
	 { 
	 	jQuery("#add").attr('value','Processing...');
	 	jQuery("#add").attr('disabled','disabled');
	 	 jQuery.post(siteurl+"customer/register", { 	 	 	
	 	 		fname:jQuery("#fname").val(),
	 	 		lname:jQuery("#lname").val(),
	 	 		company:jQuery("#company").val(),
	 	 		email:jQuery("#email").val(),
	 	 		street:jQuery("#street").val(),
	 	 		city:jQuery("#city").val(),
	 	 		state:jQuery("#state").val(),
	 	 		zip:jQuery("#zip").val(),
	 	 		phone:jQuery("#phone").val(),
	 	 		email_optout:emailOptOut,
	 	 		voucher_promo_id:jQuery("#voucher_promo_id").val()
	 	 		
	 	  },function(data){			 	 
		 	if(data == 0)	 	 
			 jQuery("#sub_msg").html('Lab & email already exist in our records.');
			else	
			if(data == 1)
			{
				success:  document.frm_sales.submit();
				
			}
			 
		 } ); 	  
	 }
	
	
}


function customer_free_promo()
{
 	 if(jQuery("#fname").val()=="")
	{
		alert("Please enter First Name.");
		jQuery("#fname").focus();
		return false;
	}
		 if(jQuery("#lname").val()=="")
	{
		alert("Please enter Last Name.");
		jQuery("#lname").focus();
		return false;
	}
	 if(jQuery("#email").val()=="")
	{
		alert("Please enter email.");
		jQuery("#email").focus();
		return false;
	}
	
	if (echeck(jQuery("#email").val())==false){
    	jQuery("#email").select();
    	jQuery("#email").focus()
    	return false;
    }

	if(jQuery("#company").val()=="")
	{
		alert("Please enter Lab name.");
		jQuery("#company").focus();
		return false;
	}
		if(jQuery("#street1").val()=="")
	{
		alert("Please enter Address.");
		jQuery("#street1").focus();
		return false;
	}
		if(jQuery("#city").val()=="")
	{
		alert("Please enter City.");
		jQuery("#city").focus();
		return false;
	}
			if(jQuery("#state").val()=="")
	{
		alert("Please enter State.");
		jQuery("#state").focus();
		return false;
	}
	
		if(jQuery("#zip").val()=="")
	{
		alert("Please enter Zip.");
		jQuery("#zip").focus();
		return false;
	}
		if(jQuery("#phone").val()=="")
	{
		alert("Please enter Phone.");
		jQuery("#phone").focus();
		return false;
	}
	
 		
	
	 	jQuery("#add").attr('value','Processing...');
	 	jQuery("#add").attr('disabled','disabled');
	 	 jQuery.post(siteurl+"customer/free_promos", { 	 	 	
	 	 		fname:jQuery("#fname").val(),
	 	 		lname:jQuery("#lname").val(),
	 	 		company:jQuery("#company").val(),
	 	 		email:jQuery("#email").val(),
	 	 		street1:jQuery("#street1").val(),
	 	 		street2:jQuery("#street2").val(),
	 	 		city:jQuery("#city").val(),
	 	 		state:jQuery("#state").val(),
	 	 		zip:jQuery("#zip").val(),
	 	 		phone:jQuery("#phone").val(),
	 	 		voucher_promo_id:jQuery("#voucher_promo_id").val(),
	 	 		iajax:1
	 	 		
	 	 		
	 	  },function(data){			 	 
		
			if(data == 1)
			{
				success:  document.frm_sales.submit();
				
				
			}
			 
		 } ); 	  
	 
	
	
}


function validateLogin()
{
	if(jQuery("#username").val()=="")
	{
		alert("Please enter username.");
		jQuery("#username").focus();
		return false;
	}
	if(jQuery("#pwd").val()=="")
	{
		alert("Please enter password.");
		jQuery("#pwd").focus();
		return false;
	}
}

function login()
{	 
	
	 if(validateLogin() != false)
	 { 
	 	jQuery("#singin").attr('value','Processing...');
	 	jQuery("#singin").attr('disabled','disabled');
	 	
	 	 jQuery.post(siteurl+"customer/login_ajax", {username:jQuery("#username").val(),pwd:jQuery("#pwd").val()},
		 function(data){	
		  
		 	if(data == 0){	 	 
			 jQuery("#sub_msg").html('Invalid Username & Password.');
			 	jQuery("#singin").attr('value','Sign In');
			 	jQuery("#singin").attr('disabled','');
		 	}
			else	
			if(data == 1)
			{
				window.location.href=siteurl+'workorder/create';
				
			}
			 
		 } ); 	  
	 }
	
	
}

function checkPromo()
{
    if(jQuery("#promo_code").val() != "")
    { 
        jQuery.post(siteurl+"vouchers/validate_ajax_promo",{promo_code:jQuery("#promo_code").val()},
		function(data){			  
		 	if(data == 0){	 	 
			  jQuery("#promo_error").html('We are sorry. That voucher promo code is not valid.'); 
			  jQuery("#is_promo").attr("value",'N'); 
		 	}
		 	else if(data ==1) 	
		 	{
		 	    jQuery("#promo_error").html(''); 
		 	    jQuery("#is_promo").attr("value",'Y'); 
		 	}
			 
			 
		 } ); 	  
    }
    else
    {
          jQuery("#is_promo").attr("value",''); 
          jQuery("#promo_error").html(''); 
    } 
}

function SaveVocuher(id)
{
    jQuery.post(siteurl+"workorder/validate_ajax_promo_files",{workorder_id:id},
		function(data){			  
		 	if(data == 1) 	 
			   window.location=siteurl+'workorder/update_status/'+id+'/S';
		 	else if(data == 2){ 
		 	     jQuery("#msg").html('Promo code and files are mandatory . Please <a href="'+siteurl+'workorder/edit/'+id+'#promocode" class="normallink">click here</a> to add.<br/>'); 
		 	     jQuery("#msg").attr("class","msgerr");
		 	}else if(data == 3){ 
		 	     jQuery("#msg").html('Files are mandatory. Please <a href="'+siteurl+'workorder/edit/'+id+'" class="normallink">click here</a> to add.<br/>'); 
		 	     jQuery("#msg").attr("class","msgerr");
		 	}else if(data == 4){ 
		 	     jQuery("#msg").html('Promo code and files are mandatory. Please <a href="'+siteurl+'workorder/edit/'+id+'" class="normallink">click here</a> to add.<br/>'); 
		 	     jQuery("#msg").attr("class","msgerr");
		 	}
			 
			 
		 } ); 	 
    
}
function removeWorkorderFile(id,element_id)
{
    
      jQuery.post(siteurl+"workorder_file/ajax_delete_attached_files", 
                {  
    	 	 		id:id
    	 	 		 	 	 		
	 	        },
	 	        function (data){
	 	            if(data  == 1){	 	                

	 	              jQuery("#"+element_id).html('File deleted');
	 	              jQuery("#"+element_id).fadeOut(2000, function () {
	 	                    jQuery("#"+element_id).remove();    
	 	              });
	 	               
	 	            }
	 	        });
}

function mark_chapter_viewed(chvideo)
{  
	 jQuery.post(siteurl+"chapters/mark_viewed",{ chapter_video: chvideo },
	  function(data){	
	   
    		//jQuery("#main_head").html('<h2 class="headclass">'+data+'</h2>'); 
		}
	  );
}
function get_material_tutorial(mid)
{
	if(mid!="")
	{
		jQuery.post(siteurl+"materials/get_material_video_ajax/", { material_id: mid },
		function(data){	
		 
			if(data!='')
			{
			jQuery("#tutorial_container").html(data);
			start_player();
			}
			
		});
	}
	 
}