var DELETE_MSG = 'Are You Sure You Want to Delete This Iteam ?';
var CHANGE_STATUS_MSG = 'Are You Sure You Want to Change Status of This Iteam ?';
var BASE = $('#base').val();


//////////////////////////////////////////Menue\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function insertSectionItem(path,divid){
	if(!chkSectionForm()){
		return false;
	}
	var name = $('#name').val();
	var published = $('#password').val();		
	//alert('path: '+path+' name: '+name+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "name="+name+"&password="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);	
			$('#name').val('');
	  	}
	});
}

function insertUserItem(path,divid){
	if(!chkUserForm()){
		return false;
	}
	var username = $('#username').val();
	var password = $('#password').val();	
	var type = $('#type').val();	
	//alert('path: '+path+' name: '+name+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "username="+username+"&password="+password+"&type="+type,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);	
			$('#username').val('');
			$('#password').val('');
			$('#type').val('');
	  	}
	});
}

function editUserForm(id, path, divid){
	
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function editUserItem(id, path, divid){
	//alert('path: '+path+' id: '+id+' divid: '+divid);
	if(!chkUserForm()){
		return false;
	}
	var username = $('#username').val();
	var password = $('#password').val();
	var type = $('#type').val();	
	//alert('path: '+path+' name: '+name+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id+"&username="+username+"&password="+password+"&type="+type,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);	
			$('#username').val('');
			$('#password').val('');
			$('#type').val('');			
	  	}
	});	
	//url: 'http://localhost/ring/index.php/admin/section/addform',
	$.ajax({
		type: "POST",
	    url: BASE+'section/addform',	    
	    success: function(response){			
			$('#sec_form').html(response);			
	  	}
	});
}


function delIteam(id,path,divid){
	if(confirm(DELETE_MSG)){
		//alert('id: '+id+' path: '+path+' div: '+divid);
	  	$.ajax({
			type: "POST",
		    url: path,
		    data: "id="+id,
		    success: function(response){	
	  			//alert(response);
	  			if(response > 0 ){
	  				alert("Delete Child First");
	  				return;
	  			}
	  			$('#'+divid).html(response);

		  	}
		});
	}
}

function editSectionForm(id, path, divid){
	
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function editSectionItem(id, path, divid){
	if(!chkSectionForm()){
		return false;
	}
	var name = $('#name').val();
	var published = $('#published').val();		
	//alert('path: '+path+' name: '+name+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id+"&name="+name+"&published="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);	
			$('#name').val('');
	  	}
	});	
	//url: 'http://localhost/ring/index.php/admin/section/addform',
	$.ajax({
		type: "POST",
	    url: BASE+'section/addform',	    
	    success: function(response){			
			$('#sec_form').html(response);			
	  	}
	});
}

function insertCategoryItem(path,divid){
	
	if(!chkCategoryForm()){
		return false;
	}
	var name = $('#name').val();
	var section_id = $('#section').val();
	var published = $('#published').val();		
	//alert('path: '+path+' name: '+name+' sec_id: '+section_id+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "name="+name+'&section_id='+section_id+"&published="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);	
			$('#name').val('');
	  	}
	});
}

function editCategoryForm(id, path, divid){
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id,
	    success: function(response){
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function updateCategory(id, path, divid){
	if(!chkCategoryForm()){
		return false;
	}
	var name = $('#name').val();
	var section_id = $('#section').val();
	var published = $('#published').val();		
	//alert('path: '+path+' name: '+name+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id+"&name="+name+'&section_id='+section_id+"&published="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);	
			$('#name').val('');
	  	}
	});	
	//url: 'http://localhost/ring/index.php/admin/category/addform',
	$.ajax({
		type: "POST",
	    url: BASE+'category/addform', 
	    success: function(response){			
			$('#cat_form').html(response);			
	  	}
	});
}

function editForm(id, path, divid){
        //alert(id+'    '+path+'    '+divid);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id,
	    success: function(response){
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function addForm(path, divid){
	$.ajax({
		type: "POST",
	    url: path,
	    success: function(response){
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function insertArticle(path, divid){
	
	if(!chkArticleForm()){
		return false;
	}
	document.forms.article_form.submit();
	return;
	
	var category_id = $('#category_id').val();
	var title = $('#title').val();
	var body = tinyMCE.get('body').getContent();
	var published = $('#published').val();		
	//alert('path: '+path+' name: '+title+' body: '+body+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "category_id="+category_id+"&title="+title+'&body='+body+"&published="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});	
}


function updateArticle(){
	if(!chkArticleForm()){
		return false;
	}
	document.forms.article_form.submit();
}
function updateArticle22(id, path, divid){
	if(!chkArticleForm()){
		return false;
	}
	var category_id = $('#category_id').val();
	var title = $('#title').val();
	var body = tinyMCE.get('body').getContent();
	var published = $('#published').val();		
	//alert('path: '+path+' name: '+title+' body: '+body+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "id="+id+"&category_id="+category_id+"&title="+title+'&body='+body+"&published="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
		
	});
	alert("hi");
	
	//window.location = 'http://localhost/ring/index.php/admin/article/showarticles';
	window.location = BASE+'article/showarticles';
}

function insertService(path, divid){
	//alert('hi');
	var category_id = $('#category_id').val();
	var type_id = $('#type_id').val();
	var name = $('#name').val();
	var description = tinyMCE.get('description').getContent();
	var published = $('#published').val();
	//alert('path: '+path+' name: '+name+' body: '+description+' publish: '+published);
	$.ajax({
		type: "POST",
	    url: path,
	    data: "category_id="+category_id+"&type_id="+type_id+"&name="+name+'&description='+description+"&published="+published,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});	
}

function admin_menu(val,path){
	var page = $(val).html();
	window.location = path;
}


function selectCategoryItem(section_id, path, divid){
    //alert(section_id+'path '+path );
    $.ajax({
        type: "POST",
        url: path,
        data: "section_id="+section_id,
        success: function(response){
            //alert(response);
            $('#'+divid).html(response);
        }
    });
}

function insertService(id){	
	//return false;
	//alert("type id::"+id);
	/*if(!chkServiceForm(id)){
		return false;
	}*/
	document.forms.service_form.submit();
	
}

function updateService(id, sl){	
	//return false;
	
	if(!chkServiceForm(id, sl)){
		return false;
	}
	document.forms.service_form.submit();
	
}
///////////////////////////////search/////////////////////////////////
function searchHomepageItem(id, path, divid){
//alert('ID:'+id+'PATH:'+path+'DIVID:'+divid);
	$.ajax({
		type: "POST",
	    url: path,
	    //data: "cat_id="+id,
		data: "",
	    success: function(response){
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

//////////////////////// VALIDATION \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

function notEmpty(elem, helperMsg){
	if(elem.length == 0){
		alert(helperMsg);
		elem.focus();
		return true;
	}
	return false;
}

function chkServiceForm(id, sl){
		
	var category_id = document.getElementById('category_id'+sl).value;
	//var code = $('#code').val();
	var name = document.getElementById('name'+sl).value;
	var dns_code = document.getElementById('dns_code'+sl).value;

	//var description = $('#description').val();
	//var description = tinyMCE.get('description').getContent();
	/*if(id==2){
		var service = tinyMCE.get('service').getContent();
	}else if(id==1){
		var service = $('#service').val();
	}*/
	var service = document.getElementById('service'+sl).value;
	
	if(notEmpty(dns_code, 'Please Give a Code')){
		return false;
	}
	
	if(notEmpty(category_id, 'Please Select a Category')){
		return false;
	}
	if(notEmpty(name, 'Please Give a Service Name')){
		return false;
	}
	//if(notEmpty(description, 'Please Give a Description')){
	//	return false;
	//}
	/*if(notEmpty(service, 'Please Give a Service')){
		return false;
	}*/

	return true;
}

function chkArticleForm(){
	
	var category_id = $('#category_id').val();
	var title = $('#title').val();
	var body = tinyMCE.get('body').getContent();	
	//alert(' name: '+title+' body: '+body);
	//return false;
	
	if(notEmpty(category_id, 'Please Select a Category')){
		return false;
	}
	if(notEmpty(title, 'Please Give a Article Title')){
		return false;
	}
	if(notEmpty(body, 'Please Give a Article')){
		return false;
	}	

	return true;
}

function chkSectionForm(){
	
	var name = $('#name').val();
	//alert(' name: '+title+' body: '+body);
	//return false;
	if(notEmpty(name, 'Please Give a Section Name')){
		return false;
	}		
	return true;
}

function chkUserForm(){
	
	var uname = $('#username').val();
	var upass = $('#password').val();
	var utype = $('#type').val();
	//alert(uname);

	if(notEmpty(uname, 'Please Give User Name')){
		return false;
	}else if (notEmpty(upass, 'Please Give Passowrd')){
		return false;
	}else if (notEmpty(utype, 'Please Give User Type')){
		return false;
	}
	return true;
}

function chkCategoryForm(){
	
	var name = $('#name').val();
	var section = $('#section').val();	
	//alert(' name: '+title+' body: '+body);
	//return false;
	if(notEmpty(section, 'Please Select a Section')){
		return false;
	}
	
	if(notEmpty(name, 'Please Give a Category Name')){
		return false;
	}	

	return true;
}

////////////////////////////// END VALIDATION \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\



///////////////////////////////////////////////////////////////////
/*function addForm(path,divid){
    //alert('addForm hit');
    var ajax_url = path;
    //alert(ajax_url+ ' div: '+divid);
    $.ajax({
        type: "POST",
        url: ajax_url,
        success: function(response){
                    $('#'+divid).html(response);
            }
    });
}



function editIteam(id,path,divid){	
	
  	var ajax_url = path;
	$.ajax({
		type: "POST",
	    url: ajax_url,
	    data: "id="+id,
	    success: function(response){	  		  	
			$('#'+divid).html(response);	
	  	}
	});

}

function updateSingleIteam(id,field,value,path,divid){
	//alert(id+" "+field+"= "+value+" path:::"+path+" div:::"+divid);
	if(confirm(CHANGE_STATUS_MSG)){
	  	var ajax_url = path;
		$.ajax({
			type: "POST",
		    url: ajax_url,
		    data: "id="+id+"&status="+value,
		    success: function(response){	 
				//alert(response);
				$('#'+divid).html(response);			
		  	}
		});
	}
}

function insertMenueIteam(path,divid){
	var name = $('#name').val();
	var parent_id = $('#parent_id').val();
	var lavel = $('#lavel').val();
	var status = $('#status').val();
	var ajax_url = path;
	//alert(path);
	$.ajax({
		type: "POST",
	    url: ajax_url,
	    data: "name="+name+"&parent_id="+parent_id+"&lavel="+lavel+"&status="+status,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function updateMenueIteam(id,path,divid){
	var name = $('#name').val();
	var parent_id = $('#parent_id').val();
	var lavel = $('#lavel').val();
	var status = $('#status').val();
	//alert('id: '+id+' name: '+name+' parent_id: '+parent_id+' lavel: '+lavel+' status: '+status);
	var ajax_url = path;
	$.ajax({
		type: "POST",
	    url: ajax_url,
	    data: "id="+id+"&name="+name+"&parent_id="+parent_id+"&lavel="+lavel+"&status="+status,
	    success: function(response){	  		 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function checkParent(parent,msg){
	if(parent=='yes'){
		alert(msg);
		return true;
	}else{
		return false;
	}
}


function editMenueProcess(id,path,divid){
	if(editIteam(id,path,divid)){	
		$('#add_menue_submit').val('update');
	}
}

//////////////////////////////////////////Article\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

function insertArticleIteam(path,divid){
	var name = $('#name').val();
	//var article = $('textarea#article').val();
	var article = tinyMCE.get('article').getContent();
	var status = $('#status').val();
	var ajax_url = path;	
	var article1 = article.replaceAll( "&", "~");
	alert(article1);
	$.ajax({
		type: "POST",
	    url: ajax_url,
	    data: "name="+name+"&article="+article1+"&status="+status,
	    success: function(response){	 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}

function updateArticleIteam(id,path,divid){
	var name = $('#name').val();
	var article = $('textarea#article').val();
	var status = $('#status').val();
	//alert('id: '+id+' name: '+name+' parent_id: '+parent_id+' lavel: '+lavel+' status: '+status);
	var ajax_url = path;
	$.ajax({
		type: "POST",
	    url: ajax_url,
	    data: "id="+id+"&name="+name+"&article="+article+"&status="+status,
	    success: function(response){	  		 
			//alert(response);
			$('#'+divid).html(response);			
	  	}
	});
}*/