/**
 * Author: Subash Tamang
 * 
 */
$(document).ready(function(){
	//$("dd:not(:first)").hide();
		$("dd").hide();
		$("dt a").click(function(){
			$("dd:visible").slideUp("normal");
			$(this).parent().next().slideDown("normal");
			return false;
		});
		$("#ajax_notifier").ajaxStart(function(){
   			$(this).text("Loading...").fadeIn("slow");
 		});
 		$("#ajax_notifier").ajaxStop(function(){
   			$(this).fadeOut(2500);
 		});

 		MM_preloadImages('./images/btn_editstore_o.gif',
 						 './images/btn_Previews_o.gif',
 						 './images/btn_Clist_o.gif',
 						 './images/btn_Lcart_o.gif',
 						 './images/btn_Fscript_o.gif',
 						 './images/btn_Dstore_o.gif',
 						 './images/btn_editfeature_o.gif',
 						 './images/btn_deletefeature_o.gif');
});

function showStoreDetails() {
	var $store_id = $("input:radio:checked").val();
	if(typeof $store_id == "undefined") {
		alert("Select the store first..");
		return false;
	}

	var $table = "#store_"+$store_id;
	if($($table).siblings().length > 0) {return false;}
	$.post('index.php',
			{store_id: $store_id,
			ajax_handler: true,
			callback: 'showStoreDetails'},
			function($data) {
				$($table).after($data);
				$("#storedetail_" + $store_id).fadeIn("normal");
				/*$("#storelist form input").attr("readonly", "true");*/
				$("textarea.resizable:not(.processed)").TextAreaResizer();
			}
		);
}

function cancelAction(elm) {
	$frm = $(elm).parent().parent().parent().parent().parent();
	$frm.slideUp("normal", function(){$frm.prev().find("img:first").attr("src", "./images/lock_add.png");$frm.remove();});
}

function enableForm(elm) {
	$frm = $(elm).parent().parent().parent().parent().parent();
	$frm.find("input").attr("readonly", "");
	$frm.find("input").attr("disabled", "");
}

function addStore(elm) {
	
	if($('#frm_addstore').checkValidity() == false)
	return false;
	
	if($("#frm_addstore :submit").attr("value") == "Add New")
	{
		$("#frm_addstore :input").attr("readonly", "");
		$("#frm_addstore :submit").val("Submit");
		$("#frm_addstore :reset").trigger('click');
		$("#frm_addstore input:first").select();
		return false;
	}
	$str = $("#frm_addstore").serialize();
	$.ajax({
		type:"POST",
		url: "index.php",
		data: $str + '&ajax_handler=true&callback=addStore',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200);
			$("#frm_addstore :input").attr("readonly", "readonly");
			$("#frm_addstore :submit").val("Add New");
		}
	});
	return false;
}


function aliasReviewCriteria() {
    $str = $("#frm_review_alias").serialize();
    $.ajax({
        type:"POST",
        url: "index.php",
        data: $str + '&ajax_handler=true&callback=addReviewAlias',
        success: function($data){
            $("#ajax_notifier").text($data).fadeIn(1200);
        }
    });
    return false;
}

function editStore(elm) {
	$frm = $(elm).parent().parent().parent().parent().parent();

	if($frm.checkValidity() == false)
	return false;
	
	
	$str = $frm.serialize();
	$.ajax({
		type:"POST",
		url: "index.php",
		data: $str + '&ajax_handler=true&callback=editStore',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200);
		}
	});
}

function deleteStore() {
	var $store_id = $("input:checked").val();
	if(typeof $store_id == "undefined") {
		alert("Select the store first..");
		return false;
	}
	
	if(confirm("This action deletes the store.\nAre you sure?")) {
		var $table = "#store_"+$store_id;
		var $row = $($table).parent().parent().parent();
		$.ajax({
			type:"POST",
			url: "index.php",
			data: 'store_id='+$store_id+'&ajax_handler=true&callback=deleteStore',
			success: function($data){
				$("#ajax_notifier").text($data).fadeIn(1200, function(){$("#ajax_notifier").fadeOut(1200);});
				if($($table).siblings().length > 0) {$($table).next().slideUp("normal", function(){$($table).next().remove();})}
				$row.fadeOut("normal", function(){$row.remove();});
			}
		});
		
	
	} else {
		return false;
	}
}

function doAction($param) {
	var $store_id = $("input:checked").val();
	if(typeof $store_id == "undefined") {
		alert("Select the store first..");
		return false;
	}
	var $url = $baseUrl+$param+"&store_id="+$store_id;
	window.location = $url;
}

/************************************Reviews***********************************************/
function publishReview($review_id, $option) {
	
	if($option == 1) {
		$add_to_div = '#product_review_published';
		$remove_from_div = '#product_review_unpublished';
	} else {
		$add_to_div = '#product_review_unpublished';
		$remove_from_div = '#product_review_published';
	}
	
	$toggle = ($option == 1)?0:1;
	$toggleText = ($option == 1)?'<img src="./images/publish.png" />&nbsp;UnPublish':'<img src="./images/publish.png" />&nbsp;Publish';
	$table_id = '#'+$review_id;
	
	$.ajax({
	type:"POST",
	url:"index.php",
	data:'review_id='+$review_id+'&option='+$option+'&ajax_handler=true&callback=publishReview',
	success: function($data) {
		
		$toggleFunc = 'publishReview('+$review_id+','+$toggle+')';
		
		if(($("#product_review").find($add_to_div).length) == 0 ) {
			if($option == 0)
			$("#product_review").prepend('<div id="product_review_unpublished"><span id="product_review_unpublished_header">UnPublished Product Reviews</span></div>');
			
			if($option == 1)
			$("#product_review").append('<div id="product_review_published"><span id="product_review_published_header">Published Product Reviews</span></div>');
		}

		$($table_id).slideUp("slow", function() {
		
			//$($table_id+' tr:first td:first p:last a:last').attr("onclick", $toggleFunc);
			$($table_id+' tr:first td:first p:last a:last').click(function(){eval($toggleFunc)});
			$($table_id+' tr:first td:first p:last a:last').html($toggleText);
			$($table_id).appendTo($add_to_div).slideDown("slow", function() {
				if($($remove_from_div).children().length < 2) {
					$($remove_from_div).slideUp("slow").remove();
				}
			});
		
		});
	}
	});
}

function showEditReviewForm($review_id) {
  $div_frm = '#review_div_'+ $review_id;
  $($div_frm).slideDown("normal");
}

function cancelEditReview($review_id) {
  $div_frm = '#review_div_'+ $review_id;
  $($div_frm).slideUp("normal");
}

function editReview($review_id) {
	$frm = '#review_frm_'+$review_id;
	$str = $($frm).serialize();
	
	$p_review_title = '#p_review_title_'+$review_id;
	$p_review_description = '#p_review_description_'+$review_id;
	
	
	$i_review_title = '#i_review_title_'+$review_id;
	$i_review_description = '#i_review_description_'+$review_id;
	
	//alert($($review_title).val());return false;
	
	
	$.ajax({
		type:"POST",
		url: "index.php",
		data: $str + '&ajax_handler=true&callback=editReview',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200);
			$($p_review_title).html('<b>"'+$($i_review_title).val()+'"</b>');
			$($p_review_description).text($($i_review_description).val());
		}
	});
}

function deleteReview(elm) {
	if(confirm("This action deletes the product's review.\nAre you sure?")) {
		var $table = $(elm).parent().parent().parent().parent().parent();
		var $review_id = $table.get(0).id;
		$.ajax({
		type:"POST",
		url: "index.php",
		data: 'review_id='+$review_id+'&ajax_handler=true&callback=deleteReview',
		success: function($data){
			//$("#ajax_notifier").text($data).fadeIn(1200, function(){$("#ajax_notifier").fadeOut(1200);});
			$("#ajax_notifier").text($data).fadeIn(1200);
			$table.slideUp("normal", function(){$table.remove();});
		}
	});
	} else {
		return false;
	}
}

/******************************************************************************************************/
function showFeatureDetails(elm) {
	
	var $feature_id = $("input:radio:checked").val();
	if(typeof $feature_id == "undefined") {
		alert("Select the feature first..");
		return false;
	}
	
	var $table = "#feature_"+$feature_id;
	if($($table).siblings().length > 0) {return false;}
	
	$.post('index.php',
			{feature_id: $feature_id,
			ajax_handler: true,
			callback: 'showFeatureDetails'},
			function($data)
			{
				$($table).after($data);
				$("#featuredetail_" + $feature_id).fadeIn("normal");
				$("textarea.resizable:not(.processed)").TextAreaResizer();
			}
		);
}

function addFeature() {
	
	if($('#frm_addfeature').checkValidity() == false)
	return false;
	
	if($("#frm_addfeature :submit").attr("value") == "Add New")
	{
		$("#frm_addfeature :input").attr("readonly", "");
		$("#frm_addfeature :submit").val("Submit");
		$("#frm_addfeature :reset").trigger('click');
		$("#frm_addfeature input:first").select();
		return false;
	}
	$str = $("#frm_addfeature").serialize();
	
	$.ajax({
		type:"POST",
		url: "index.php",
		data: $str + '&ajax_handler=true&callback=addFeature',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200);
			$("#frm_addfeature :input").attr("readonly", "readonly");
			$("#frm_addfeature :submit").val("Add New");
		}
	});
	return false;
}


function editFeature(elm) {
	$frm = $(elm).parent().parent().parent().parent().parent();
	if($frm.checkValidity() == false)
	return false;
	
	$str = $frm.serialize();
	
	$.ajax({
		type:"POST",
		url: "index.php",
		data: $str + '&ajax_handler=true&callback=editFeature',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200, function(){$("#ajax_notifier").fadeOut(1200);});
		}
	});
}

function deleteFeature(elm) {
	
	var $feature_id = $("input:checked").val();
	if(typeof $feature_id == "undefined") {
		alert("Select the feature first..");
		return false;
	}
	
	if(confirm("This action deletes the feature.\nAre you sure?")) {
		
		var $table = "#feature_"+$feature_id;
		var $row = $($table).parent().parent();
		
		$.ajax({
		type:"POST",
		url: "index.php",
		data: 'feature_id='+$feature_id+'&ajax_handler=true&callback=deleteFeature',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200, function(){$("#ajax_notifier").fadeOut(1200);});
			if($($table).siblings().length > 0) {$($table).next().slideUp("normal", function(){$($table).next().remove();})}
			$row.fadeOut("normal", function(){$row.remove();});
		}
	});
	} else {
		return false;
	}
}

function changePassword() {
	
	if($('#frm_chpassword').checkValidity() == false)
	return false;
	
	if($('#newpass').val() != $('#rnewpass').val()) {
		alert('Password retype mismatch');
		return false;
	}
	
	$str = $("#frm_chpassword").serialize();
	
	$.ajax({
		type:"POST",
		url: "index.php",
		data: $str + '&ajax_handler=true&callback=changePassword',
		success: function($data){
			$("#ajax_notifier").text($data).fadeIn(1200);
			//$("#frm_chpassword :input").attr("readonly", "readonly");
		}
	});
	return false;
}


function addUser() {
    
    if($('#frm_adduser').checkValidity() == false)
    return false;
    
    if($('#password').val() != $('#rpassword').val()) {
        alert('Password retype mismatch');
        return false;
    }
    
    if($("#frm_adduser :submit").attr("value") == "Add New")
    {
        $("#frm_adduser :input").attr("readonly", "");
        $("#frm_adduser :submit").val("Submit");
        $("#frm_adduser :reset").trigger('click');
        $("#selected_stores option").each(function(){$(this).remove().appendTo("#listed_stores").attr("selected","")});
        $("#frm_adduser input:first").focus();
        
        return false;
    }
    $str = $("#frm_adduser").serialize();
    $.ajax({
        type:"POST",
        url: "index.php",
        data: $str + '&ajax_handler=true&callback=addUser',
        success: function($data){
            $("#ajax_notifier").text($data).fadeIn(1200);
            $("#frm_adduser :input").attr("readonly", "readonly");
            $("#frm_adduser :submit").val("Add New");
        }
    });
    return false;
}

function showUserDetails() {
    
    var $uid = $("input:radio:checked").val();
    if(typeof $uid == "undefined") {
        alert("Select the user first..");
        return false;
    }
    
    var $table = "#user_"+$uid;
    if($($table).siblings().length > 0) {return false;}
    
    $.post('index.php',
            {uid: $uid,
            ajax_handler: true,
            callback: 'showUserDetails'},
            function($data)
            {
                $($table).after($data);
                $("#userdetail_" + $uid).fadeIn("normal");
                $("#add_store").click(function(){return $("#listed_stores option:selected").remove().appendTo("#selected_stores");});
                $("#listed_stores").dblclick(function(){return $("#listed_stores option:selected").remove().appendTo("#selected_stores");});
                $("#remove_store").click(function(){return $("#selected_stores option:selected").remove().appendTo("#listed_stores").attr("selected", "");});
                $("#selected_stores").dblclick(function(){return $("#selected_stores option:selected").remove().appendTo("#listed_stores").attr("selected", "");});
                $("#addall_store").click(function(){return $("#listed_stores option").each(function(){$(this).remove().appendTo("#selected_stores").attr("selected", "selected");})});
                $("#removeall_store").click(function(){return $("#selected_stores option").each(function(){$(this).remove().appendTo("#listed_stores").attr("selected", "");})});
            }
        );
}

function editUser(elm) {
    $frm = $(elm).parent().parent().parent().parent().parent();
    if($frm.checkValidity() == false)
    return false;
    
    $("#selected_stores option").each(function(){$(this).attr("selected", "selected");});
    $str = $frm.serialize();
    
    $.ajax({
        type:"POST",
        url: "index.php",
        data: $str + '&ajax_handler=true&callback=editUser',
        success: function($data){
            $("#ajax_notifier").text($data).fadeIn(1200, function(){$("#ajax_notifier").fadeOut(1200);});
        }
    });
}

function deleteUser() {
    var $uid = $("input:checked").val();
    if(typeof $uid == "undefined") {
        alert("Select the user first..");
        return false;
    }
    
    if(confirm("This action deletes the user.\nAre you sure?")) {
        
        var $table = "#user_"+$uid;
        var $row = $($table).parent().parent();
        
        $.ajax({
        type:"POST",
        url: "index.php",
        data: 'uid='+$uid+'&ajax_handler=true&callback=deleteUser',
        success: function($data){
            $("#ajax_notifier").text($data).fadeIn(1200, function(){$("#ajax_notifier").fadeOut(1200);});
            if($($table).siblings().length > 0) {$($table).next().slideUp("normal", function(){$($table).next().remove();})}
            $row.fadeOut("normal", function(){$row.remove();});
        }
    });
    } else {
        return false;
    }
}

/**************************************************************************************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
