/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//AFFILIATE.PAGE.JS
$(document).ready(function()
{
	disableField()
	 var countryID  =   $('#affiliateCountryID').val();
	 var state		=   $('#statehidValue').val(); 
	 var city 		=   $('#cityhidValue').val();
	 listStates(countryID,state);
	$(".affdelete").click(function(){
		var delUrl = $(this).attr("rel");
		jConfirm(msgConfirmsub, 'Alert', function(r) {
			if(r==true) 
			{
				 window.location.href = GLOBAL_BASE_PATH +delUrl;
			}
		});
	
	});
	    
	// AFFILIATE STATUS CHANGE - check box validation to update status
	$(".update_btn_table").click(function(){
		var ListAllCheckedCount = $(".ListAll:checked").size(); 
		var affiliateStatus= $('#affiliateStatusList').val();
		if($(".ListAll:checked").size()==0)
		{
			jAlert(msgSelectItem);
			return false;
		} 
		else if(affiliateStatus=="")
		{
			jAlert(lblSelectAction);
			 return false; 
		}		
		if(affiliateStatus=='Deleted')
		{
			jConfirm(msgConfirmsub, 'Alert', function(r) {
				if(r==true) 
				{
					$('#formAffiliate').submit();
				}
			});
		} 
		else	
		{
			return true;
		}
		return false;
	 });
	//CHECK BOX VALIDATION TO UPDATE STATUS
	$("#submitSearch").click(function(){
		
		if($('#affiliateID').val()=="" &&  jQuery.trim($('#affiliateName').val())=="" && jQuery.trim($('#affDate').val())==""){
		    jAlert(msgSelectSearchCriteria);
			return false;
		}
		return true;
	 });
	 	
		
	
});
 //STATES

function getStateHiddenValue(val)
{
     $('#statehidValue').val(val);
}
function listStates(countryID,stateID)
{
//	alert(stateID);
  if(countryID)
  {
	  $('#statehidValue').val(stateID);
	  $.ajax({
			type: "POST",
			url: GLOBAL_BASE_PATH+"/city/ajaxState/",
			data: "countryID="+countryID+"&stateID="+stateID+"&currentLanguageID="+currentLanguageID,
			beforeSend:function(){
				$("#loadProcessImage").append('<img src="'+GLOBAL_ROOT_PATH+'/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
			},
			success: function(msg)
			{  
				 var msg = $.trim( msg );
				$("#loadProcessImage").empty();	
				if(msg!='')
				{
					$("#stateInput").show();
					$('#stateID').empty();
					$("#stateProvinceInput").hide();
					$("#stateID").append('<option value="">'+lblSelect+'</option>'+msg);
					$("#stateSelectedType").val("stateID");
					$("#province").val('');
					$("#div_err_frmAffiliate_provincehidden").css("display","none");
				} 
				else 
				{
					$("#stateInput").hide();
					$("#stateProvinceInput").show();
					$("#stateID").val('');
					$("#stateSelectedType").val("province");
					$("#div_err_frmAffiliate_statehidden").css("display","none");
				}
		   }
	  });
  } 
}
//CHK LENGTH
function ismaxlength(obj)
{
   var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
   if (obj.getAttribute && obj.value.length>mlength){
   obj.value=obj.value.substring(0,mlength)
    jAlert(lblMaxCharaLength);
	}
}
var globalSettings={};
function show_global_settings()
{
  	if ($('#globalCommission').prop('checked')) {
		if(typeof globalSettings['global']!='undefined'){
			var msg = globalSettings['global'];
			$("#loadImage").empty();	
			if(msg!='')
			{
				$('#ajaxCommission').html(msg);
			} 
		}
		else{
			$.ajax({
				type: "POST",
				data: "globalVal=1",
				beforeSend:function(){
					$("#loadImage").append('<img id="ajaxLoaderImage" src="'+GLOBAL_ROOT_PATH+'/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />')
				},
				url: GLOBAL_BASE_PATH+"/affiliate/ajaxGlobalSettings/",
				success: function(msg)
				{
					 var msg = $.trim( msg );
					 globalSettings['global']=msg;
					 $("#loadImage").empty();	
					 if(msg!='')
					 {
						$('#ajaxCommission').html(msg);
					 } 
				}
			});
		}
 	}
	else
	{
		var id=$('#affiliateUserID').val();
		if(typeof globalSettings['normal']!='undefined'){
			var msg = globalSettings['normal'];
			$("#loadImage").empty();
			if(msg!='')
				{
				$('#ajaxCommission').html(msg);
				} 
		}
		else{
			$.ajax({
				type: "POST",
				async :false,
				beforeSend:function(){
					$("#loadImage").append('<img id="ajaxLoaderImage" src="'+GLOBAL_ROOT_PATH+'/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />')
				},
				data: "globalVal=2&id="+id,
				url: GLOBAL_BASE_PATH+"/affiliate/ajaxGlobalSettings/",
				success: function(msg)
				{  
					 var msg = $.trim( msg );
					 globalSettings['normal']=msg;
					  $("#loadImage").empty();
					 if(msg!='')
					 {
						$('#ajaxCommission').html(msg);
					 } 
				}
			 });
		}
	 disableField();
	}
}
function disableField()
{
	    if(!$('#enableSalesCommission').prop('checked')){
			 $('#firstCommissionPaymentType').attr('disabled','disabled');
			 $('#firstPurchaseCommissionValue').attr('disabled','disabled');
			 $('#recurringPurchaseCommissionType').attr('disabled','disabled');
			 $('#recurringPurchaseCommissionValue').attr('disabled','disabled');
			  $('.salesComClass').val("");
		}
		else
		{
			$('#firstCommissionPaymentType').removeAttr('disabled');
			$('#firstPurchaseCommissionValue').removeAttr('disabled');
			$('#recurringPurchaseCommissionType').removeAttr('disabled');
			$('#recurringPurchaseCommissionValue').removeAttr('disabled');
		}
	   if(!$('#enableTierCommission').prop('checked')){
			 $('#tierCommissionLevel').attr('disabled','disabled');
			 $('#tierCommissionType').attr('disabled','disabled');
			 $('#tierCommissionValue').attr('disabled','disabled');
			  $('.tierComClass').val("");
		}
		else
		{
			$('#tierCommissionLevel').removeAttr('disabled');
			$('#tierCommissionType').removeAttr('disabled');
			$('#tierCommissionValue').removeAttr('disabled');
		}
		if(!$('#subscriptionCommission').prop('checked')){
			 $('#subscriptionCommissionAmount').attr('disabled','disabled');
		}
		else
		{
			$('#subscriptionCommissionAmount').removeAttr('disabled');
		}	
		if(!$('#registrationCommission').prop('checked')){
			 $('#registrationCommissionAmount').attr('disabled','disabled');
		}
		else
		{
			$('#registrationCommissionAmount').removeAttr('disabled');
		}
		if(!$('#bulkSubscriberImport').prop('checked')){
			 $('#bulkSubscriberImportCommission').attr('disabled','disabled');
		}
		else
		{
			$('#bulkSubscriberImportCommission').removeAttr('disabled');
		}	
		
		if(!$('#bulkUserImport').prop('checked')){
			$('#bulkUserImportCommission').attr('disabled','disabled');
		}
		else
		{
			$('#bulkUserImportCommission').removeAttr('disabled');
		}		
}