if (typeof SORT_ASC == 'undefined') { var SORT_ASC = 4; }
if (typeof SORT_DESC == 'undefined') { var SORT_DESC = 3; }

function isset() {
	var a=arguments; var k=a.length; var i=0;

	if (k==0) {
		throw new Error('Empty isset');
	}

	while (i!=k) {
		if (typeof(a[i])=='undefined' || a[i]===null) {
			return false;
		}
		i++;
	}

	return true;
}

function trim(str, charlist) {
	var whitespace, l = 0;

	whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	if (isset(charlist)) {
		whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
	}

	l = str.length;
	for (var i = 0; i < l; i++) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(i);
			break;
		}
	}

	l = str.length;
	for (i = l - 1; i >= 0; i--) {
		if (whitespace.indexOf(str.charAt(i)) === -1) {
			str = str.substring(0, i + 1);
			break;
		}
	}

	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function include(filename) {
	var script = document.createElement('script');
	script.setAttribute('type', 'text/javascript');
	script.setAttribute('src', filename);
	script.setAttribute('defer', 'defer');
	document.getElementsByTagName('HEAD')[0].appendChild(script);

	// save include state for reference by include_once
	if (!window.php_js) {window.php_js = {};}
	if (!window.php_js.includes) {window.php_js.includes = {};}
	if (!window.php_js.includes[filename]) {
		window.php_js.includes[filename] = 1;
	}
	else {
		window.php_js.includes[filename]++;
	}

	return window.php_js.includes[filename];
}


function include_once(filename) {
	if (!window.php_js) {window.php_js = {};}
	if (!window.php_js.includes) {window.php_js.includes = {};}
	if (!window.php_js.includes[filename]) {
		return include(filename);
	}
	else {
		return window.php_js.includes[filename];
	}
}

function set_product_attributes(sender) {
	var input = document.createElement('INPUT');
	input.type = "hidden";
	input.name = "args[leading_attribute]";
	var matches = sender.name.match(/args\[attributes\]\[(\d+)\]/i);
	if (!empty(matches)) {
		input.value = matches[1];
		sender.form.appendChild(input);				
		sender.form.submit();		
	}
}

function set_product_attributes_nosubmit(sender) {
	var input = document.createElement('INPUT');
	input.type = "hidden";
	input.name = "args[leading_attribute]";
	var matches = sender.name.match(/args\[attributes\]\[(\d+)\]/i);
	if (!empty(matches)) {
		input.value = matches[1];
		alert(input.value);
		sender.form.appendChild(input);	
	}
}

function remember_quantity(sender, from_id) {
	var input = document.createElement('INPUT');
	var dropdownIndex = document.getElementById(from_id).selectedIndex;
	var dropdownValue = document.getElementById(from_id)[dropdownIndex].value;
	input.type = "hidden";
	input.name = "args[quantity]";
	input.value = dropdownValue;
	sender.form.appendChild(input);
}

function empty_on_edit(sender) {
	if(!isset(sender.initValue))sender.initValue=sender.defaultValue;
	if (sender.value == sender.initValue)sender.value = '';
	sender.onblur = function() {if(this.value == '')this.value = this.initValue;}
	sender.ondblclick = function() {this.value = this.initValue;}
}

function set_sort(form, field){
	var sort_by = document.getElementById(form).elements.namedItem('args[sort_by]');
	var sort_order = document.getElementById(form).elements.namedItem('args[sort_order]');
	sort_order.value = (sort_by.value == field && sort_order.value == SORT_ASC) ? SORT_DESC : SORT_ASC;
	sort_by.value = field;
	document.getElementById(form).submit();
}

function set_page(form, page_nr){
	var page_no = document.getElementById(form).elements.namedItem('args[page_no]').value = page_nr;
	document.getElementById(form).submit();
}

function set_page_custom(form, page_nr, var_name){
	var page_no = document.getElementById(form).elements.namedItem('args['+var_name+']').value = page_nr;
	document.getElementById(form).submit();
}

function set_items_per_page(form, item_count) {
	document.getElementById(form).elements.namedItem('args[items_per_page]').value = item_count;
	document.getElementById(form).submit();
}

function empty(mixed_var) {
	if (mixed_var === "" || mixed_var === 0 || mixed_var === "0"
	|| mixed_var === null || mixed_var === false || mixed_var === undefined
	|| ((typeof mixed_var == 'array' || typeof mixed_var == 'object') && mixed_var.length === 0) ){
		return true;
	}

	return false;
}

/** Ajax request functions **/
var aPlaceholders =[];
function process_submit(sender, sPlaceholder, sTemplate, sLoaderdiv, callBackFunction){
	var placeholder = null, xmlHttp;	

	if( isset(sPlaceholder) ){
		if (!isset(aPlaceholders[sPlaceholder]) ) { // dont allow multiple calls on same placeholder	
			placeholder=document.getElementById(sPlaceholder);
			xmlHttp = new clXmlHttp();
			xmlHttp.onReady = process_result;
			xmlHttp.callBackFunction = callBackFunction;
			xmlHttp.tag = sPlaceholder;
			aPlaceholders[sPlaceholder] = xmlHttp;						
			//placeholder.disabled=true;					
			if( document.getElementById(sLoaderdiv) != null )document.getElementById(sLoaderdiv).style.display = 'block';                                                                                                                                                            
				
			// get all data elements and put them in request string	
			var oForm = (sender.tagName.toUpperCase() == "FORM") ? sender : sender.form;
			var oElement;
			var aPostVariables = Array();
			
			for( i=0; i<oForm.elements.length; i++ ){
				oElement=oForm.elements[i];
				switch(oElement.type){
					case 'button':
					case 'submit':
						//oElement.disabled=true;	
				  	break;
					case 'checkbox':
				 		if(oElement.checked)aPostVariables[oElement.name]=oElement.name + '=' + oElement.value;
					break;
					case 'radio':
						if(oElement.checked)aPostVariables[oElement.name]=oElement.name + '=' + oElement.value;
				  	break;
					default: aPostVariables[oElement.name]=oElement.name + '=' + oElement.value;
				}				
			}	
			//always add as last, overwriting false values 
			aPostVariables['request']='request=page';
			aPostVariables['args[page]']='args[page]='+sTemplate;
			var params ='';
			for(var aVar in aPostVariables) {if( typeof aPostVariables[aVar] =="string" )params +='&'+aPostVariables[aVar];}
			params = trim(params, '&');
			xmlHttp.makePOSTRequest(ajax_url, true, params);
		}	
	}		
	return false;
}

function process_result(sender){	
	var oPlaceholder;	
	try{
		if( isset(sender) ){ 
			if( isset(sender.tag) ){
				aPlaceholders[sender.tag] = null;
			 	oPlaceholder =document.getElementById(sender.tag);
				var oResult =  this.responseText();		
				//oPlaceholder.disabled=false;
				oPlaceholder.innerHTML =oResult;
				if(sender.callBackFunction) sender.callBackFunction();
			}
		}
	}catch(e){alert(e.message);}
}

function show_details(form_id, input_id, value) {
	var process_form = document.getElementById(form_id);
	var process_input = document.getElementById(input_id);
	process_input.value = value;
	process_form.submit();
}
