//author : Ramshid.com

function newtab(id){
	var newop=$('#cat_item_'+id);
	var editop=$('#sav_can_'+id);
	editop.hide();
	newop.show();
	$('#newop_'+id).addClass('selected');
	$('#editop_'+id).removeClass('selected');
	
}

function edittab(id){
	var newop=$('#cat_item_'+id);
	var editop=$('#sav_can_'+id);
	editop.show();
	newop.hide();
	$('#newop_'+id).removeClass('selected');
	$('#editop_'+id).addClass('selected');
	
}

//random numbers
jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	}
});

function edit(id){
	
	var optnx=$('#optnx_'+id);
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var xtitle_value=xtitle.html();
	var xdets_value=xdets.html();
	var sav_can=$('#sav_can_'+id);
	var upl_btn=$('.upload_'+id);
	//var elm1=$('#elm1');
	var topics=$('#topics');
	xtitle.hide();
	xdets.hide();
	topics.hide();
	sav_can.hide();
	
	var title=$('input#title_'+id);
	var dets=$('textarea#dets_'+id);
	$('#newoptions_'+id).hide();
	title.show();
	dets.show();
	optnx.show();
	//elml.show();
	//elml.val(topics.html());
	upl_btn.show();
	title.val(xtitle_value);
	dets.val(xdets_value);
	
	$('.silverheader').click(function(){

	editop.hide();
	//newop.hide();
	title.hide();
	xtitle.show();
	sav_can.show();
	dets.hide();
	xdets.show();
	optnx.hide();
	});
}



function cancel(id){
	$('#newoptions_'+id).show();
	var optnx=$('#optnx_'+id);
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var title=$('input#title_'+id);
	var dets=$('textarea#dets_'+id);
	var sav_can=$('#sav_can_'+id);
	var upl_btn=$('.upload_'+id);
	title.hide();
	xtitle.show();
	sav_can.show();
	dets.hide();
	xdets.show();
	optnx.hide();
	upl_btn.hide();
}
function up_img(id){
		var mainId=id;
		var randid = $.randomBetween(1234506, 889900066);
		var btnUpload=$('#upload_'+mainId);		
		var status=$('#status_'+mainId);
		if(id==0){
		var urls='rid='+randid+'&new=1';
		}
		else{
			var urls='rid='+randid+'&imgid='+id;
		}
		new AjaxUpload(btnUpload, {
			
			action: 'system/upload-file.php?'+urls,
			
			
			
			name: 'uploadfile',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
				status.html('<img src="images/ajax-loader.gif"/> Uploading...');
				btnUpload.hide();
			},
			onComplete: function(file, response){
				//On completion clear the status
				status.text('');
				//Add uploaded file to list
				if(response==="success"){
					$('img#images_'+mainId).hide();
					$('#files_'+mainId).show();
					$('#files_'+mainId).html('<img src="images/products/images/'+randid+file+'" alt="image is loading" width="115" height="80"/>');
					btnUpload.show();
					$('#url_image').val('images/products/images/'+randid+file);
					btnUpload.html('Change Image');
					$('#savCat').show();
				} else{
					$('#files_'+mainId).text(file).addClass('error');
				}
			}
		});
		
	};
	function uploadMyCats(id){
		var mainId=id;
		var randid = $.randomBetween(1234506, 889900066);
		var btnUpload=$('#upload_'+mainId);		
		var status=$('#status_'+mainId);
		
			var urls='mycats=1&rid='+randid+'&imgid='+id;
		
		new AjaxUpload(btnUpload, {
			
			action: 'system/upload-file.php?'+urls,
			
			
			
			name: 'uploadfile',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
				status.html('<img src="images/ajax-loader.gif"/> Uploading...');
				btnUpload.hide();
			},
			onComplete: function(file, response){
				//On completion clear the status
				status.text('');
				//Add uploaded file to list
				if(response==="success"){
					$('img#images_'+mainId).hide();
					$('#files_'+mainId).show();
					$('#files_'+mainId).html('<img src="images/products/images/'+randid+file+'" alt="image is loading" width="115" height="80"/>');
					btnUpload.show();
					$('#url_image').val('images/products/images/'+randid+file);
					btnUpload.html('Change Image');
					$('#savCat').show();
				} else{
					$('#files_'+mainId).text(file).addClass('error');
				}
			}
		});
		
	};
	
function save(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	
	
	$.post("system/options.php",{title:title,dets:dets,id:id});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}
	
	function save_p(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	
	
	$.post("system/system_edit.php?prd=1",{title:title,dets:dets,id:id});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}
	
function addNewP(){
var newp=$('#newproduct').val();
var image=$('#p_image').val();
var details=$('#p_details').val();

	if(image==''){
		if (confirm('Are you sure you want to add this Product without image?')) {
			$.ajax({
			type: "POST",
			data: 'newp='+newp+'&details='+details+'&images=no',
			url: 'system/options.php',
			success: function(data) {
			if(data=='OK'){
			// $('.result').html(data);
			window.location='products.php';
			}
			else{
				alert(data);
			}
 			 }
			});
		}
	}
	else{
			$.ajax({
			type: "POST",
			data: 'newp='+newp+'&details='+details+'&images='+image,
			url: 'system/options.php',
			success: function(data) {
			if(data=='OK'){
			// $('.result').html(data);
			window.location='products.php';
			}
 			 }
			});
		
	}


	}//addNewP
	
	
	
	function newCat(){
		
		var title=$('#newcat_title').val();
		var details=$('#newcat_det').val();
		var url_id=$('input#url_id').val();
		var url_product=$('input#url_product').val();
		var url_action=$('input#url_action').val();
		var url_image=$('#url_image').val();

		if(url_image==''){
			if (confirm('Are you sure you want to add this Category without image?')) {
		$.ajax({
		type: "POST",
		data: 'newCat=1&title='+title+'&details='+details+'&pid='+url_id+'&img=no',
  		url: 'system/system_edit.php',
  		success: function(data) {
		if(data=='OK'){
		// $('.result').html(data);
    	window.location='more_products.php?pid='+url_id+'&product='+url_product+'&action='+url_action;
		  }
	 	 else{
		  alert(data);
	 	 }
  }
});
			}//confirm
		}//image if
		else{
					$.ajax({
		type: "POST",
		data: 'newCat=1&title='+title+'&details='+details+'&pid='+url_id+'&img='+url_image,
  		url: 'system/system_edit.php',
  		success: function(data) {
		if(data=='OK'){
		// $('.result').html(data);
    	window.location='more_products.php?pid='+url_id+'&product='+url_product+'&action='+url_action;
		  }
	 	 else{
		  alert(data);
	 	 }
  }
});
		}
//else
	}//new cat
	function newCat_item(){
		
		var title=$('#newcat_title').val();
		var details=$('#newcat_det').val();
		var url_id=$('input#url_id').val();
		var url_cat=$('input#url_cat').val();
		var url_action=$('input#url_action').val();
		var url_image=$('#url_image').val();
		//alert(url_id);
		
		if(url_image==''){
			if (confirm('Are you sure you want to add this Category without image?')) {
		$.ajax({
			 type: "POST",
			 data: 'newCat_item=1&title='+title+'&details='+details+'&pid='+url_id+'&img=no',
  url: 'system/system_edit.php',
  success: function(data) {
	  if(data=='OK'){
   // $('.result').html(data);
    window.location='products_category.php?category='+url_cat+'&id='+url_id;
	  }
	  else{
		  alert(data);
	  }
  }
});
			}
		}
		else{
			$.ajax({
			 type: "POST",
			 data: 'newCat_item=1&title='+title+'&details='+details+'&pid='+url_id+'&img='+url_image,
  url: 'system/system_edit.php',
  success: function(data) {
	  if(data=='OK'){
   // $('.result').html(data);
    window.location='products_category.php?category='+url_cat+'&id='+url_id;
	  }
	  else{
		  alert(data);
	  }
  }
});
		}

		
	}//newCat_item
	
	function savecat(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var cat='true';
	
	
	$.post("system/options.php",{title:title,dets:dets,id:id,cat:cat});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}
	function savecat_item(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var cat_item='true';
	
	
	$.post("system/options.php",{title:title,dets:dets,id:id,cat_item:cat_item});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}
function saveDets(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var cat_item='true';
	
	
	$.post("system/options.php",{title:title,dets:dets,id:id,cat_item:cat_item});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}
	
	
	function newItem(){
		
		var title=$('#newcat_title').val();
		var details=$('#newcat_det').val();
		var url_id=$('input#url_id').val();
		var url_product=$('input#url_product').val();
		var url_action=$('input#url_action').val();
		var url_image=$('#url_image').val();
		var topic=tinyMCE.activeEditor.getContent();
		
		if(url_image==''){
			if (confirm('Are you sure you want to add this Item without image?')) {
			//alert(topic);
					$.ajax({
					 type: "POST",
				 data: 'newItem=1&title='+title+'&details='+details+'&pid='+url_id+'&img=no'+'&topic='+topic,
  			url: 'system/system_edit.php',
 			 success: function(data) {
			  if(data=='OK'){
  		 // $('.result').html(data);
   		 window.location='more_products.php?pid='+url_id+'&product='+url_product+'&action='+url_action;
			  }
			  else{
		  alert(data);
	 	 }
 	 }
		});
			}//confirm
		}//if image
		else{
			//alert(topic);
					$.ajax({
					 type: "POST",
				 data: 'newItem=1&title='+title+'&details='+details+'&pid='+url_id+'&img='+url_image+'&topic='+topic,
  			url: 'system/system_edit.php',
 			 success: function(data) {
			  if(data=='OK'){
  		 // $('.result').html(data);
   		 window.location='more_products.php?pid='+url_id+'&product='+url_product+'&action='+url_action;
			  }
			  else{
		  alert(data);
	 	 }
 	 }
		});
			
		}
//else val
		
	}//new Item
	
		function newCatItem_item(){
		
		var title=$('#newcat_title').val();
		var details=$('#newcat_det').val();
		var url_id=$('input#url_id').val();
		var url_cat=$('input#url_cat').val();
		var url_action=$('input#url_action').val();
		var url_image=$('#url_image').val();
		var topic=tinyMCE.activeEditor.getContent();
		//alert(topic);
		
		if(url_image==''){
			if (confirm('Are you sure you want to add this Item without image?')) {
		$.ajax({
			 type: "POST",
			 data: 'newCatItem_item=1&title='+title+'&details='+details+'&pid='+url_id+'&img=no'+'&topic='+topic,
  url: 'system/system_edit.php',
  success: function(data) {
	  if(data=='OK'){
   // $('.result').html(data);
   
    window.location='products_category.php?category='+url_cat+'&id='+url_id;
	  }
	  else{
		  alert(data);
	  }
  }
});
			}
		}
		else{
			$.ajax({
			 type: "POST",
			 data: 'newCatItem_item=1&title='+title+'&details='+details+'&pid='+url_id+'&img='+url_image+'&topic='+topic,
  url: 'system/system_edit.php',
  success: function(data) {
	  if(data=='OK'){
   // $('.result').html(data);
   
    window.location='products_category.php?category='+url_cat+'&id='+url_id;
	  }
	  else{
		  alert(data);
	  }
  }
});
		}

		
	}//new ItemnewCatItem_item
	
	
	
	
		
function savecat_items(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var xsave='1';
	
	
	$.post("system/options.php",{title:title,dets:dets,id:id,xsave:xsave});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}//category items
	
	function save_p_catItems(id){
	var title=$('input#title_'+id).val();
	var dets=$('textarea#dets_'+id).val();
	var xtitle=$('#xtitle_'+id);
	var xdets=$('#xdets_'+id);
	var xsave_p='1';
	
	
	$.post("system/options.php",{title:title,dets:dets,id:id,xsave_p:xsave_p});
	
xtitle.html(title);
	xdets.html(dets);
	cancel(id);
	
	
	}//save_p_catItems
	
	function up_img_cat_item(id){
		var mainId=id;
		var randid = $.randomBetween(1234506, 889900066);
		var btnUpload=$('#upload_'+mainId);		
		var status=$('#status_'+mainId);
		if(id==0){
		var urls='rid='+randid+'&new=1';
		}
		else{
			var urls='rid='+randid+'&imgid='+id;
		}
		new AjaxUpload(btnUpload, {
			
			action: 'system/upload-file.php?item=1&'+urls,
			
			
			
			name: 'uploadfile',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
				status.html('<img src="images/ajax-loader.gif"/> Uploading...');
				btnUpload.hide();
			},
			onComplete: function(file, response){
				//On completion clear the status
				status.text('');
				//Add uploaded file to list
				if(response==="success"){
					$('img#images_'+mainId).hide();
					$('#files_'+mainId).show();
					$('#files_'+mainId).html('<img src="images/products/images/'+randid+file+'" alt="image is loading" width="115" height="80"/>');
					btnUpload.show();
					$('#url_image').val('images/products/images/'+randid+file);
					btnUpload.html('Change Image');
					$('#savCat').show();
				} else{
					$('#files_'+mainId).text(file).addClass('error');
				}
			}
		});
		
	};


//-----product page new image//
function up_img_product(id){
		var mainId=id;
		var randid = $.randomBetween(1234506, 889900066);
		var btnUpload=$('#upload_'+mainId);	
		var uuru=$('#uuru');
		var status=$('#status_'+mainId);
		if(id==0){
		var urls='rid='+randid+'&new=1';
		}
		else{
			var urls='rid='+randid+'&imgid='+id;
		}
		new AjaxUpload(btnUpload,{
			
			action: 'system/upload-file.php?prd=1&'+urls,
			
			
			
			name: 'uploadfile',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					status.text('Only JPG, PNG or GIF files are allowed');
					return false;
				}
				status.html('<img src="images/ajax-loader.gif"/> Uploading...');
				btnUpload.hide();
			},
			onComplete: function(file, response){
				//On completion clear the status
				status.text('');
				//Add uploaded file to list
				if(response==="success"){
					$('img#images_'+mainId).hide();
					$('#files_'+mainId).show();
					$('#files_'+mainId).html('<img src="images/products/images/'+randid+file+'" alt="image is loading" width="115" height="80"/>');
					btnUpload.show();
					$('#p_image').val('images/products/images/'+randid+file);
					btnUpload.html('Change Image');
					$('#savCat').show();
				} else{
					$('#files_'+mainId).text(file).addClass('error');
				}
			}
		});
		
	};
	
function saveDets_it(id){
	var topic=tinyMCE.activeEditor.getContent();
	
	var from=$('#from').val();
	$.ajax({
type: "POST",
data: 'id='+id+'&topic'+topic,
url: 'system/system_edit.php?topics=1',
success: function(data) {
if(data=='OK'){
// $('.result').html(data);
window.location=from;
}
else{
	alert(data);
}
  }
});
	
}
	
//------------------------Delete Functions----------------------//
function del(id){
	if (confirm('Deleting this Category will remove all sub categories and Items listed in this Category! Are you sure?')) {
	var from=$('#from').val();
	var loader=$('#del_loader_'+id);
	loader.show();
	$.ajax({
			 type: "POST",
			 data: 'id='+id,
  url: 'system/delete.php?cats=1',
  success: function(data) {
	  if(data=='OK'){
   // $('.result').html(data);
   
    window.location=from;
	  }
	  else{
		  alert(data);
	  }
  }
});//ajax
	
	}
	
}
function del_prd(id){
	if (confirm('Deleting this Product will remove all sub categories and Items listed in this Product! Are you sure?')) {
	var from=$('#from').val();
	var loader=$('#del_loader_'+id);
	loader.show();
	$.ajax({
	type: "POST",
	data: 'id='+id,
	url: 'system/delete.php?prd=1',
	success: function(data) {
		if(data=='OK'){
		// $('.result').html(data);
   
		window.location='products.php';
		}
		else{
	 	alert(data);
		}
  }
});//ajax
	
	
	}
	
}

function delItem(id){
	if (confirm('Are you sure want to delete this Item?')) {
	var from=$('#from').val();
	var loader=$('#del_loader_'+id);
	loader.show();
	$.ajax({
	type: "POST",
	data: 'id='+id,
	url: 'system/delete.php?item=1',
	success: function(data) {
		if(data=='OK'){
		// $('.result').html(data);
   
		window.location=from;
		}
		else{
	 	alert(data);
		}
  }
});//ajax
	}
	
}
function delCat(id){
	if (confirm('Deleting this Category will remove all sub categories and Items listed in this Category! Are you sure?')) {
	var from=$('#from').val();
	var loader=$('#del_loader_'+id);
	loader.show();
	$.ajax({
	type: "POST",
	data: 'id='+id,
	url: 'system/delete.php?cat=1',
	success: function(data) {
		if(data=='OK'){
		// $('.result').html(data);
   
		window.location=from;
		}
		else{
	 	alert(data);
		}
  }
});//ajax
	}
	
}
