// JavaScript Document


// layouts used in page


var login_ani = function() {
    var animArgs = {
	node: "login",
	duration: 1000, // ms to run animation
	delay: 150 // ms to stall before playing
    };
    dojo.fadeIn(animArgs).play();
	

}


function mW(id,dWidth,disp) {
	var query = '#' + id;
	
dojo.query(query).style('display', disp);
dojo.animateProperty({node: id,duration: 500, properties: { width: {end: dWidth}}}).play();
}


function wipeIn(id) {
var node =  dojo.byId(id);	
dojo.fx.wipeIn({node: node,duration: 1000});
}

function fadeIn(id) {
	var node =  dojo.byId(id);	
 dojo.fadeIn({node: node}).play();
}

var load = function(id, url) {

var _slist = dijit.byId('id');

dojo.connect(mW,"onEnd",function(){ _slist.setHref(url); });
var animOut= dojo.fx.chain([wipeOut, mW, wipeIn]);

animOut.play();
}


 
 function EditformSubmit(e) {
	// prevent the form from actually submitting
	e.preventDefault(); 
	// submit the form in the background	
	
	dojo.io.iframe.send({
				method: "POST",
				contentType: "multipart/form-data", 
				url: "edit_item_submit.php",
				form: dojo.byId('editForm'),
				handleAs: "text",
				handle: function(data,args){
				if(data instanceof Error){
							console.log("Request FAILED: ", data);
						}else{
							dijit.byId('edit').setContent(data);
					var _utab = dijit.byId(G_SELECTED_TAB);
					
					_utab.setHref('fulllist.php?view='+ G_SELECTED_TAB + '&disable_toogle=' + DISABLE_TOOGLE+ '&current_cat=' + CURRENT_CATOGORY + '&page=' + CURRENT_PAGE);
						}

				}
		});	
	
	
}

 function EditformAvailSubmit(e) {
	// prevent the form from actually submitting
	e.preventDefault(); 
	// submit the form in the background	
	dojo.xhrPost({
		url: "edit_item_avail_submit.php",
		form: "editForm",
		handleAs: "text",
		handle: function(data,args){
			if(typeof data == "error"){
				console.warn("error!",args);
			}else{
				// show our response 
				var result = data;
				
	//dijit.byId('loadingDialog').destroy();
	dijit.byId('edit').setContent(result);
					var _utab = dijit.byId(G_SELECTED_TAB);
					_utab.setHref('availList.php?view='+ G_SELECTED_TAB + '&list=' + LIST + '&current_cat=' + CURRENT_CATOGORY + '&page=' + CURRENT_PAGE);
					
					
				
			
			}
		}
	});
}

function AddformSubmit(e) {

// prevent the form from actually submitting
	e.preventDefault(); 
	// submit the form in the background
	
	dojo.io.iframe.send({
				method: "POST",
				contentType: "multipart/form-data", 
				url: "add_item_submit.php",
				form: dojo.byId('addForm'),
				handleAs: "text",
				handle: function(data,args){
				if(data instanceof Error){
							console.log("Request FAILED: ", data);
						}else{
							dijit.byId('edit').setContent(data);
					var _utab = dijit.byId(G_SELECTED_TAB);
					
					_utab.setHref('fulllist.php?view='+ G_SELECTED_TAB + '&disable_toogle=' + DISABLE_TOOGLE+ '&current_cat=' + CURRENT_CATOGORY+ '&page=' + CURRENT_PAGE);
						}

				}
		});
}


function AddlistformSubmit(e) {

// prevent the form from actually submitting
	e.preventDefault(); 
	// submit the form in the background

	dojo.xhrPost({
		url: "add_list_submit.php",
		form: "addlistForm",
		handleAs: "text",
		handle: function(data,args){
			if(typeof data == "error"){
				console.warn("error!",args);
			}else{
				// show our response 
				var result = data;
				console.log(result);
				
					dijit.byId('loadingDialog').destroy();
					window.location='index.php?action=alist&list=' + result ;
			
			}
		}
	});
}



 function getHtml(gurl,id) {
            dojo.xhrGet ({
                // Location of the HTML content we want to grab
                url: gurl ,
       			handleAs: "text",
                // Called when the page loaded successfully
                load: function (data) {
					var result = data;
					
				if (result == 1) {
					var _utab = dijit.byId(G_SELECTED_TAB);
					
					_utab.setHref('fulllist.php?view='+ G_SELECTED_TAB + '&disable_toogle=' + DISABLE_TOOGLE+ '&current_cat=' + CURRENT_CATOGORY + '&page=' + CURRENT_PAGE);

				}
                
				},
       
                // Called if there was an error (such as a 404 response)
                error: function (data) {
                    console.error('Error: ', data);
                }
            });
        }
  
  function getHtml_Avail(gurl,id) {
	 
            dojo.xhrGet ({
                // Location of the HTML content we want to grab
                url: gurl ,
       			handleAs: "text",
                // Called when the page loaded successfully
                load: function (data) {
					var result = data;
					console.log(result);
				if (result == 1) {
					
			
					dijit.byId(G_SELECTED_TAB).setHref('availList.php?view='+ G_SELECTED_TAB + '&list=' + LIST + '&current_cat=' + CURRENT_CATOGORY + '&page=' + CURRENT_PAGE);
					
				}
                
				},
       
                // Called if there was an error (such as a 404 response)
                error: function (data) {
                    console.error('Error: ', data);
                }
            });
        }
		
 
 function getDialog(gurl,width,height) {
	      dojo.xhrGet ({
                // Location of the HTML content we want to grab
                url: gurl ,
       
                // Called when the page loaded successfully
                load: function (data) {
					var result = data;
				

if (dijit.byId('loadingDialog')) {
dijit.byId('loadingDialog').destroy();
}
var loadingDialog = new dijit.Dialog({ title:"", id:"loadingDialog",style:"width:" + width + "; height:" + height + "; overflow:auto" });
dijit.byId('loadingDialog').show();
loadingDialog.setContent(result);

	},
       
                // Called if there was an error (such as a 404 response)
                error: function (data) {
                    console.error('Error: ', data);
                }
            });
 }
 
 
 
 function editItem(gurl,page){

 dijit.byId('edit').setHref(gurl);
var eForm = dojo.byId("editForm");
// another dojo.connect syntax: call a function directly	
dojo.connect(eForm,"onsubmit",EditformSubmit);

        }

function addItem(gurl) {
 dijit.byId('edit').setHref(gurl);
	 var aForm = dojo.byId("addForm");
// another dojo.connect syntax: call a function directly	
	dojo.connect(aForm,"onsubmit",AddformSubmit);
	
}

function updatetab(tab) {
	
	var gurl = 'fulllist.php?view='+ tab + '&disable_toogle=' + DISABLE_TOOGLE + '&current_cat=' + CURRENT_CATOGORY;

dijit.byId(tab).setHref(gurl);		
                
	
}
function updatetabSearch() {
	var query = '' + dojo.byId('query').value
	var gurl = 'fulllist.php?view='+ G_SELECTED_TAB + '&disable_toogle=' + DISABLE_TOOGLE + '&current_cat=' + CURRENT_CATOGORY + '&search=' + query;

dijit.byId(G_SELECTED_TAB).setHref(gurl);		
                	
}

function updatetabAvail(tab) {
	var gurl = 'availList.php?view='+ tab + '&list=' + LIST + '&current_cat=' + CURRENT_CATOGORY + '&page=' + CURRENT_PAGE;

dijit.byId(tab).setHref(gurl);		
                
	
}


function hide_disabled(value) {

DISABLE_TOOGLE = value;
updatetab(G_SELECTED_TAB);

}

function show_catogory(value) {

CURRENT_CATOGORY = value;
updatetab(G_SELECTED_TAB);

}

var doSearch1=function()
		{
		
			
		soc_id = null;
		var delay = delay || 1400;
		window.clearTimeout( soc_id );
  		soc_id = window.setTimeout( 'updatetabSearch()', 'delay' );
		}
		
		
var doSearch=function()
		{		
			var tbl = dojo.byId(G_SELECTED_TAB + '_grid_table');
			var query = '' + dojo.byId('query').value;			
			
			
			var rx = new RegExp(query, 'i');
			
			dojo.forEach(tbl.rows,
			function(obj, index, array)
			{
				if (index > 0)
				{  	
					var m = rx.exec(obj.cells[1].innerHTML);
				
					if (m) obj.style.display='';
					else obj.style.display='none';
				}
			});
		
		};

function toggleSizePrice(){
	
	
	
		var TSP = dojo.byId('Ex_Size_Price');
		var ASP = dojo.byId('act_size_price');
	
		if (TSP.style.display == 'none') {
    		openPrompt(TSP);
			var activateHTML = "<a id=\"act_link\" href=\"#\" onclick=\"toggleSizePrice();return false;\">Hide more prices</a>";
			ASP.innerHTML = activateHTML;
		} else {
			closePrompt(TSP);
			var activateHTML = "<a id=\"act_link\" href=\"#\" onclick=\"toggleSizePrice();return false;\">Show more prices</a>";
			ASP.innerHTML = activateHTML;
		}
	}
	
	function openPrompt(el)
	{	
		//dojo.addClass(iTl,'cnt');
		//dojo.addClass(iT,'cnt');
		//dojo.removeClass(iT,'trHidden');
		
		dojo.fx.wipeIn({
			node: el,				
			duration: 500,
			onEnd: function() { el.isOpen=true; }
		}).play();		
	}
	 
	function closePrompt(el)
	{			
	if (el.style.display != 'none')	 {
		dojo.fx.wipeOut({
			node: el,				
			duration: 500,
			onEnd: function() { el.isOpen=false; }
		}).play();	
		}	
	}
