$(document).ready(function(){
 $(".categoryTree").change(function () {
        setSelected('category');
    });   
});
 function setSelected(type) {
        var categorySelected = 0;
        if(type!='custom'){
        $("."+type+"Tree").each(function () {
            if ($(this).is(':checked')) categorySelected = 1;
        })
        }        
        if (categorySelected == 1 || $('#type').val()=='custom') $("#categorySelected").val('1');
        else $("#categorySelected").val('');
        $("#categorySelected").trigger('change');
    } 
function showPath(cat,prodid){
    if($('#type').val()!='product'){
     $("#objectID").val($(cat).val());
    }
if($(cat).val()>0 || prodid>0){
    dataArray[0] 			= $('#type').val();
   if($('#type').val()!='product'){
    dataArray[1] 			= $(cat).val();
   } else {
     dataArray[1] 			= prodid;  
    dataArray[2] 			= $(cat).val();
   }
    if($('#type').val()=='menu')
    dataArray[3] 			= $('#menuorder_'+$(cat).val()).val();;
    $.ajax({
                                    type: "POST",
                                    url: GLOBAL_BASE_PATH+"/urlrewrite/ajaxpath/",

                            data: { ajax_request: dataArray},
                                    success: function(msg){ 
                                    if(msg!='1')
                                    {
                                        resarray=msg.split('~*');
                                        $('#idPath').val(resarray[0]).trigger('blur');
                                        $('#requestPath').val(resarray[1]).trigger('blur');
                                        $('#oldrequestPath').val(resarray[1]) 
                                        $('#targetPath').val(resarray[2]).trigger('blur'); ;
                                        $('#AjaxObjectPath').show();
                                        checkPathExists(resarray[1]);
                                        $("#categorySelected").trigger('blur');
                                    }
                                    }
       });
}
}
function listObjectTypes()
{	
    dataArray			= new Array();
    $('#AjaxDivList').hide();$('#AjaxObjectPath').hide();
    $('#AjaxDivProductCategoryList').hide();
    var type         = $('#type').val();
     if(type==''){
         jAlert(errSelectType);
         return false;
     }else if(type=='custom'){
         $("#AjaxDivList").html('');  
         $("#idPath,#targetPath,#requestPath").attr("readonly",false).removeClass("disabled").val('')
         $("#idPath,#targetPath,#requestPath").trigger('blur');
         $('#AjaxObjectPath').show();
         setSelected($('#type').val());
     }else{
       $("#idPath,#targetPath").attr("readonly",true).addClass("disabled")
       $('#cityLoc_loader').removeClass("hide");
       dataArray[0] 			= type;	       
       $.ajax({
                type: "POST",
                url: GLOBAL_BASE_PATH+"/urlrewrite/ajax/",
                data: { ajax_request: dataArray},
                        success: function(msg){
                        if(msg!='1')
                        {
                                $('#cityLoc_loader').addClass("hide");
                                $("#AjaxDivList").show().html(msg);   
                        }
                        }
       });
    }
}
function resetAll()
{
    $('#searchName,#searchCategory').val('');   
    refreshListItems();
}
function refreshListItems()
{
   dataArray			= new Array(); 
   $('#AjaxObjectPath').hide();
    $('#AjaxDivProductCategoryList').hide();
    var type         = $('#type').val();
     if(type==''){
         jAlert(errSelectType);
         return false;
     }else{
       $("#idPath,#targetPath").attr("readonly",true).addClass("disabled")
       $('#ListLoader').removeClass("hide");
       dataArray[0] 			= type;	
       dataArray[1] 			= $('#searchName').val();	
       dataArray[2] 			= $('#searchCategory').val();	
       dataArray[3] 			= 'search';	
       $.ajax({
                type: "POST",
                url: GLOBAL_BASE_PATH+"/urlrewrite/ajax/",
                data: { ajax_request: dataArray},
                        success: function(msg){ 
                        if(msg!='1')
                        {   
                                setSelected('');
                                $('#ListLoader').addClass("hide");
                                $("#AjxRfrshDiv").show().html(msg);
                        }
                        }
       });
    } 
}
function showCategory(prod)
{
    showPath(0,$(prod).val()); 
if($(prod).val()>0 && $('#type').val()=='product'){
     $("#objectID").val($(prod).val());
     dataArray[0] 			= $('#type').val();
    dataArray[1] 			= $(prod).val();
    $.ajax({
                                    type: "POST",
                                    url: GLOBAL_BASE_PATH+"/urlrewrite/ajaxProdCatalog/",

                            data: { ajax_request: dataArray},
                                    success: function(msg){ 
                                    if(msg!='1')
                                    {
                                       $('#cityLoc_loader').addClass("hide");
                                       $("#AjaxDivProductCategoryList").show().html(msg);  
                                    }
                                    }
       });
}
}
$('#requestPath').on("blur",function(){
var requestPath   =   $(this).val();
var arrayP   =   requestPath.split('/')
for(var i=0;i<arrayP.length;i++){
    arrayP[i] =  $.formatUrl(arrayP[i]);
}
var productUrl=arrayP.join('/');
$("#requestPath").val(productUrl);
checkPathExists(productUrl);
});
function checkPathExists(url)
{
        var rewriteID = $('#rewriteID').val();  
        dataArray			= new Array();
        dataArray[0] 			= rewriteID;   
        dataArray[1] 			= url;
        dataArray[2] 			= $('#isSystem').val();
        if($.trim(url)!='') {  
	   $.ajax({
				   	type: "POST",
                                        async:"false",
					url: GLOBAL_BASE_PATH+"/urlrewrite/ajaxPathExists/",		      		
		           	data: { ajax_request: dataArray},
				   	success: function(msg){ 
					if(msg=='1')
					{
                                          $('#countExists').val(1);                                         
                                          $('#div_err_formUrlRewrite_validateState').removeClass("hide"); 
                                         // alert(errDeliveryZoneAlreadyExist)
                                        }else {
                                            $('#countExists').val(0);
                                             $('#div_err_formUrlRewrite_validateState').addClass("hide"); 
                                        }                                        	
				}
	   });
        }else {             
           $('#countExists').val(0);
           $('#div_err_formUrlRewrite_validateState').addClass("hide"); 
        }
         $("#countExists").trigger('blur');
}