// JavaScript Document
$j = jQuery.noConflict();
function getSubCat(site_url,cat,count)
{
	if(cat.value!=0)
	{
		$j.ajax({
			type: "POST",
			url: site_url+"/index.php",
			data:"option=com_classified&task=getsubcat&catid="+cat.value+"&newcat="+count,
			beforeSend: function ()
			{
				$j("#cat_"+count).html("Rettriving...");
			},
			success: function(result){
			$j("#cat_"+count).html(result);
			}
		 });
	}
	
	for(var i=1 ;i <=count ; i++ )
	{
			if($j("#category_"+i).val()==0);
			{
			$j("#ad_form_fields").hide();
			break;
			}
	}
}

function displayAdForm(count , elements)
{	
	for(var i=1 ;i <=count ; i++ )
	{	
		if($j("#category_"+i).val()==0)
		{
			$j("#ad_form_fields").hide();
			break;
		}
		else
		{
			$j("#ad_form_fields").fadeIn("slow");
			$j("#formExptaFields").html(elements);
		}
	}
}


$j().ready(function() {
	// validate the comment form when it is submitted
	// validate signup form on keyup and submit
	
	$j("#ad_post").validate({
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
		/*	description:"required",*/
			ad_state:"required",
			ad_city:"required"
		},
		messages: {
			ad_city:"Please add the City",
			ad_state:"Please add the State",
		/*	description:"Please add the description of ad",*/
			name: "Please enter your name of ad",
			email: "Please enter a valid email address"
		}
	});
	
});

function DelImages(id,site_url)
{
	
	if(!confirm('Are you sure want to delete this'))
	return false;
	
	if(id!=0 || id!='')
	{
		$j.ajax({
			type: "POST",
			url: site_url+"/index.php",
			data:"option=com_classified&task=delete_img&mid="+id,
			beforeSend: function ()
			{
				$j('#'+id+'_img').html('<img src="'+site_url+'/components/com_classified/images/loading.gif" />');
			},
			success: function(result){
			
				if(result==1)
				{
					$j('#'+id+"_img").html('<img src="'+site_url+'/components/com_classified/images/deleted.jpg" />');
				}
				else
				{
					$j('#'+id+"_img").html(result)	;
				}
			}
		 });
	}	
}

$j().ready(function() {
	// validate the comment form when it is submitted
	// validate signup form on keyup and submit
	$j("#msg_send").validate({
		rules: {
			message: "required",
			email: {
				required: true,
				email: true
			},
			description:"required",
			scode:"required"
		},
		messages: {
			message:"Please type the message here",
			scode:"Please enter security code",
			email: "Please enter a valid email address"
		}
	});
	
});
 
function checkOn(id,feat,spot,ad_id)
{
	$result = getTotalPrice(feat,spot,ad_id);
	
	if(feat.length>0 && id.name=='featured')
	{
		if(id.checked==true)
		{
			$j("#featured_p").show();
		}
		else
		$j("#featured_p").hide();
		
	}
	if(spot.length >0 && id.name=='spotlight')
	{
		if(id.checked==true)
		{
			$j("#spot_p").show();
		}
		else
			$j("#spot_p").hide();
	}
	
	
	
	
}

function getTotalPrice(feat,spot,ad_id)
{	
	var feat_val = '';
	var feat_check = '';
	var spot_val = '';
	var spot_check = '';
	
	if(feat.length>0)
	{	
		feat_val = feat.get(0).value;
		feat_check = $j('#featured').get(0).checked;
	}
	if(spot.length >0)
	{	
		spot_val = spot.get(0).value;
		spot_check = $j('#spotlight').get(0).checked;
	}
	
	$j.ajax({
			type: "POST",
			url: "index.php",
			data:"option=com_classified&task=planprice&featured_plan="+feat_val+"&spot_plan="+spot_val+
			"&featured="+feat_check+"&spotlight="+spot_check+"&ad_id="+ad_id,
			
			beforeSend: function ()
			{
				if(feat.length>0 && $j('#featured').get(0).checked==true)
				{
					$j("#featured_price").html("Rettriving...");
				}
				
				if(spot.length >0 && $j('#spotlight').get(0).checked==true)
				{
					$j('#spot_price').html("Rettriving...");
				}
				
			},
			
			success: function(result){ //alert(result);
			
			var t_price = result.getElementsByTagName('totalprice')[0].firstChild;
			
			if(feat.length>0 && $j('#featured').get(0).checked==true)
			{
				
				var f_price = result.getElementsByTagName('featuredprice')[0].firstChild;
				if(f_price)	
				{
					$j('#featured_price').html(f_price.nodeValue);
				}
			}
			
			if(spot.length >0 && $j('#spotlight').get(0).checked==true)
			{
				var s_price = result.getElementsByTagName('spotprice')[0].firstChild;
				if(s_price)	
				{
					$j('#spot_price').html(s_price.nodeValue);
				}
			}
			if(t_price)	
			{
				if($j('#featured').get(0).checked==false && $j('#spotlight').get(0).checked==false)
					$j('#total_price').html('');
				else
				{
					$j('#total_price').show();
					$j('#total_price').html(t_price.nodeValue);
				}
			}
				
			}
		 });
}

function featurecheck(id,feat,ad_id)
{
	$result = featureSelect(feat,ad_id);
	
	if(id.name=='featured')
	{
		if(id.checked==true)
		{
			$j("#featured_p").show();
		}
		else
		$j("#featured_p").hide();
		
	}
}
function featureSelect(feat,ad_id)
{
	$j.ajax({
			type: "POST",
			url: "index.php",
			data:"option=com_classified&task=planprice&featured_plan="+feat.get(0).value+"&featured="+$j('#featured').get(0).checked+"&ad_id="+ad_id,
			
			beforeSend: function ()
			{
				if($j('#featured').get(0).checked==true)
				{
					$j("#featured_price").html("Rettriving...");
				}
				
			},
			
			success: function(result){// alert(result);
			
			var t_price = result.getElementsByTagName('totalprice')[0].firstChild;
			
			if($j('#featured').get(0).checked==true)
			{
				
				var f_price = result.getElementsByTagName('featuredprice')[0].firstChild;
				if(f_price)	
				{
					$j('#featured_price').html(f_price.nodeValue);
				}
			}
		
			if(t_price)	
			{
				if($j('#featured').get(0).checked==false)
					$j('#total_price').html('');
				else
				{
					$j('#total_price').show();
					$j('#total_price').html(t_price.nodeValue);
				}
			}
				
			}
		 });
}

function spotcheck(id,spot,ad_id)
{
    $result = spotSelect(spot,ad_id);
    
    if(id.name=='spotlight')
    {
        if(id.checked==true)
        {
            $j("#spot_p").show();
        }
        else
        $j("#spot_p").hide();
        
    }
}
function spotSelect(spot,ad_id)
{
    $j.ajax({
            type: "POST",
            url: "index.php",
            data:"option=com_classified&task=planprice&spot_plan="+spot.get(0).value+"&spotlight="+$j('#spotlight').get(0).checked+"&ad_id="+ad_id,
            
            beforeSend: function ()
            {
                if($j('#spotlight').get(0).checked==true)
                {
                    $j("#spot_price").html("Rettriving...");
                }
                
            },
            
            success: function(result){// alert(result);
            
            var t_price = result.getElementsByTagName('totalprice')[0].firstChild;
            
            if($j('#spotlight').get(0).checked==true)
            {
                
                var f_price = result.getElementsByTagName('spotprice')[0].firstChild;
                if(f_price)    
                {
                    $j('#spot_price').html(f_price.nodeValue);
                }
            }
        
            if(t_price)    
            {
                if($j('#spotlight').get(0).checked==false)
                    $j('#total_price').html('');
                else
                {
                    $j('#total_price').show();
                    $j('#total_price').html(t_price.nodeValue);
                }
            }
                
            }
         });
}

function PagingOnSubmit(orderBy,order,index,Page)
{
	document.searchForm.orderby.value=orderBy;
	document.searchForm.order.value=order;
	document.searchForm.Index.value=index;
	document.searchForm.submit();
	
}

$j().ready(function() {
	$j('#recurring').click(function()
	{
			if($j('#recurring').is(":checked")==true)	
			{
				$j("#recurring_tbl").show("1000");	
				if($j("#featured"))
				{
					$j("#featured").attr('checked',false)
					$j("#featured").attr('disabled',true)
					$j("#featured_p").hide();
				}
				if($j("#spotlight"))
				{	
					$j("#spotlight").attr('checked',false)
					$j("#spotlight").attr('disabled',true)
					 $j("#spot_p").hide();
				}
			}
			else
			{
			 	$j("input[name = 'recurr_radio']").attr('checked',false)
				$j("#recurring_tbl").hide("1000");
				if($j("#featured"))
				{
					$j("#featured").attr('disabled',false)
				}
				if($j("#spotlight"))
				{	
					$j("#spotlight").attr('disabled',false)
				}
				
			}
	}		
);
	});

$j().ready(function() {
	$j("#feat_rad").click(function(){
		$j("#featured").attr('disabled',false);
		$j("#spotlight").attr('disabled',true);
		$j("#spotlight").attr('checked',false)
		$j("#spot_p").hide();
	} );				
});

$j().ready(function() {
	$j("#spot_rad").click(function(){
		$j("#featured").attr('disabled',true);
		$j("#spotlight").attr('disabled',false);
		$j("#featured").attr('checked',false)
		$j("#featured_p").hide();
	} );				
});


