//Client scripts

//Add Category Item
function addCategoryItem(){
    positionElement('#categories');
}

//Delete Category Item
function delCategoryItem(){
	var con = confirm('Are you sure you want to delete this category?');
	var itemId = $('#sow_CategoryItemId').val();
	
	if(con){
		if(itemId==""){
			alert("Please choose a category from the list first.");
			return false;
		} else {
	        var url = "actions.asp?action=del-category-item&itemId=" + itemId ;
			window.location.href=url;
		}
	}
}

//Delete Image
function deleteImage(Id,fld,img){
    var con = confirm("Are you sure you want to delete this Image?\n\nWARNING\nThis image will be permanently deleted.");

    if(con){
        var url = "actions-uploads.asp?action=delete-image&pageId=" + Id + "&field-name=" + fld + "&image-name=" + img;
		window.location.href=url;
	}
}

//Delete page
function deletePage(Id){

	if(Id==''){
		alert('First choose a page to delete from the dropdown menu below.');
		return false;
	}

    var con = confirm("Are you sure you want to delete this page?\n\nWARNING\nAll data for this page will be permanently deleted.");

    if(con){
		$.ajax({
		  url: "actions.asp?action=delete-page&pageId=" + Id,
		  success: function(r){
		    showMessage('#message', 'Page deleted successfully.', 'success');
		  },
		  error: function(r){
		        var errMess = r.responseText;
		        if(errMess==''){errMess='An error has occurred.'};
			    showMessage('#message', errMess, 'fail');
			}
		});
	}
}

//Delete section
function deleteSection(Id, section){

	if(Id==''){
		alert('First choose an item from the dropdown menu below.');
		return false;
	}

    var con = confirm("Are you sure you want to delete this " + section + "?\n\nWARNING\nAll data for this " + section + " will be permanently deleted.");

    if(con){
		$.ajax({
		  url: "actions.asp?action=delete-section&pageId=" + Id + "&section=" + section,
		  success: function(r){
		    showMessage('#message', section + ' deleted successfully.', 'success');
		    $('#copy').html(r);
		  },
		  error: function(r){
		        var errMess = r.responseText;
		        if(errMess==''){errMess='An error has occurred.'};
			    showMessage('#message', errMess, 'fail');
			}
		});
	}
}

function showMessage(ele, messText, className){

    var mess = $(ele);
    var divWidth, setLeft;

	//Get screen width
	var screenWidth = window.innerWidth;
	if(navigator.appName!='Netscape'){
		var screenWidth = document.body.offsetWidth;
	}

	mess.removeClass('fail');
	mess.removeClass('success');
	mess.removeClass('info');
	mess.html(messText);
	mess.addClass(className);

	//Get message width
	divWidth = mess.width();
	setLeft = (screenWidth-divWidth)/2

	//Position element and show
	mess.css({left: setLeft + 'px'});
	$('html, body').animate({ scrollTop: 0 }, 'slow');
	mess.fadeIn().delay(4000).fadeOut();
}

function getJustInSortable(){
    //Sort JustIn items
    if($("#justin-grid")){
	   	$("#justin-grid").sortable({
	          update: function(e){
	              var results = $("#justin-grid").sortable('toArray');
				$.ajax({
				  url: "actions.asp?action=save-justin-order&order=" + results,
				  success: function(r){
				    showMessage('#sub-message', 'Just In details saved successfully.', 'success');

				  },
				  error: function(r){
				        $('#sow_JustIn').removeAttr('checked');
					    showMessage('#sub-message', r.responseText, 'fail');
					}
				});
			}
		 });
	    positionElement('#justin-order');
    }
}

function getMenuSortable(){
    //Sort menu
    if($("#menu-sort-box")){
	   	$(".sort-menu").sortable({
	          update: function(e){
	              var results = $(this).sortable('toArray');
				$.ajax({
				  url: "actions.asp?action=save-menu-order&order=" + results,
				  success: function(r){
				    showMessage('#sort-message', 'Menu order saved successfully.', 'success');

				  },
				  error: function(r){
					    showMessage('#sort-message', r.responseText, 'fail');
					}
				});
			}
		 });
	    positionElement('#menu-sort-box');
    }
}

function getProductSortable(){
	var sortFlag = false;
    //Sort products
    if($("#products")){
		 if(!sortFlag){
		 	showMessage('#message', 'Sorting is active.', 'info');
		 	sortFlag = true;
		}
        $("#products tbody.row_sort").css('cursor','pointer');
	   	$("#products tbody.row_sort").sortable({
	          update: function(e){
	              var results = $(this).sortable('toArray');
				$.ajax({
				  url: "actions.asp?action=save-product-order&order=" + results,
				  success: function(r){
				    showMessage('#message', 'Product order saved successfully.', 'success');

				  },
				  error: function(r){
					    showMessage('#message', r.responseText, 'fail');
					}
				});
			}
		 });
		 $("#products tbody.row_sort").disableSelection();
    }
}

function getNewPage(ele){
	positionElement(ele);

}

function hideModal(ele){
	$(ele).fadeOut();
	$('#overlay').fadeOut();
}

function positionElement(ele){
	var screenWidth = window.innerWidth;
	var screenHeight = window.innerHeight;
	if(navigator.appName!='Netscape'){
		var screenWidth = document.body.offsetWidth;
		var screenHeight = document.body.offsetHeight;
	}
	var divWidth = $(ele).width();
	var divHeight = $(ele).height();
	var setLeft = (screenWidth/2)-(divWidth/2);
	var setTop =  (screenHeight/3)-(divHeight/2);

	//Show overlay
	$('#overlay').show();
	
	//Position element
	$(ele).css({'left': setLeft});
	$(ele).css({'top': 50});

    //Show element
    $(ele).fadeIn('slow');
}

function go(loc) {
	window.location.href = loc;
}
	
function popUp(url, wdth, hght) {
	window.open(url,"mywindow","menubar=1,scrollbars=1,statusbar=1,resizable=1,width=" + wdth + ",height=" + hght + ",left=5,top=5"); 
}

function toggleElement(Id){
	animatedcollapse.addDiv(Id, 'fade=3')
	animatedcollapse.init();
	animatedcollapse.toggle(Id);
}

function showElement(Id){
	animatedcollapse.addDiv(Id, 'fade=3')
	animatedcollapse.init();
	animatedcollapse.show(Id);
}

function hideElement(Id){
	animatedcollapse.addDiv(Id, 'fade=3')
	animatedcollapse.init();
	animatedcollapse.hide(Id);
}

function showHint(str,tbl,fld,hint){
	//Set local variables
	var url = "";
	var elehint = document.getElementById(hint);
	var sto;	
	
	//Set url and send request
	url=url+"search_hint.asp?q="+str;
	url=url+"&t="+tbl;
	url=url+"&f="+fld;
	url=url+"&sid="+Math.random();
	
	//Hide the hint box	
	if (str.length==0){ 
		elehint.innerHTML="";
		elehint.style.display = 'none';
		return;
	}
		 	
	if(str.length>2){
		if(sto!=undefined) clearTimeout(sto);
		sto = setTimeout("getRequest('" + url + "','" + hint + "')",1200); 
	}
}

function getRequest(url,hint){

	var xmlhttp = null;
	var elehint = document.getElementById(hint);	
	
	if (window.XMLHttpRequest){// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject){// code for IE5 and IE6
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
			
	xmlhttp.open("GET",url,false);
	xmlhttp.send(null);
	if(xmlhttp.responseText!=''){
		//Show hint element
		elehint.style.display = 'block';
		//Set response
		elehint.innerHTML=xmlhttp.responseText;
	} else {
		elehint.style.display = 'none';
	}

}
