/*
+----------------------------------------------------------------------+
| Advanced Filter Mod                                                  |
+----------------------------------------------------------------------+
| Copyright (c) 2009 BCSE LLC. dba BCS Engineering                     |
+----------------------------------------------------------------------+
|                                                                      |
| Advanced Filter Mod is subject for version 2.0 of the BCSE           |
| proprietary license. That license file can be found bundled with     |
| this package in the file BCSE_LICENSE. A copy of this license can    |
| also be found at                                                     |
| http://www.bcsengineering.com/license/BCSE_LICENSE_2.0.txt           |
|                                                                      |
+----------------------------------------------------------------------+
*/

function apply_filter(){
	if(!document.getElementById('customer_filter'))
		return;

	var wrapper = document.getElementById('customer_filter');

	var selectObjs = wrapper.getElementsByTagName('select');
	if (selectObjs.length < 1)
		selectObjs = wrapper.getElementsByTagName('input');
	if(selectObjs.length < 1)
		return;

	var filter_query = '';
	for(var x = 0; x < selectObjs.length; x++){
		var ffield = encodeURIComponent(selectObjs[x].getAttribute('name'));
		var fvalue = encodeURIComponent(selectObjs[x].value);
		if(target_content_id && document.getElementById(target_content_id)){
			ffield = escape(ffield);
			fvalue = escape(fvalue);
		}
		filter_query += (filter_query != '' ? '&' : '') + ffield + '=' + fvalue;
	}

	if((!target_content_id || !document.getElementById(target_content_id)) && document.forms['filter_form']){
		document.forms['filter_form'].elements['filter_query'].value = filter_query;
		document.forms['filter_form'].elements['mode'].value = 'filter_update';

		document.forms['filter_form'].submit();
	}else{
		ajax_filter_refresh(post_action,filter_query);
	}
}

function ajax_filter_refresh(post_url,query){

	if(!target_content_id || !document.getElementById(target_content_id))
		return;

	contentObj = document.getElementById(target_content_id);

	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return;
	}

	set_ajax_loading(contentObj);

	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4){
			contentObj.style.height = '';
			contentObj.innerHTML = xmlHttp.responseText;
		}
	}

	post_str = 'filter_query=' + encodeURIComponent(decodeURIComponent(query)) + '&mode=filter_update&from_ajax=Y';

	xmlHttp.open("POST",post_url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", post_str.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(post_str);
}

function set_ajax_loading(obj){

	var offset_height = obj.offsetHeight - (+obj.style.paddingTop.replace('px','') + +obj.style.paddingBottom.replace('px','') + +obj.style.marginTop.replace('px','') + +obj.style.marginBottom.replace('px',''));
	var offset_width = obj.offsetWidth - (+obj.style.paddingLeft.replace('px','') + +obj.style.paddingRight.replace('px','') + +obj.style.marginLeft.replace('px','') + +obj.style.marginRight.replace('px',''));

	var ajaxImage = document.createElement('div');
	ajaxImage.style.position = 'absolute';
	ajaxImage.style.zIndex = '500';
	ajaxImage.innerHTML = '<img src="'+ajax_image.src+'" />';
	ajaxImage.style.marginLeft = ((offset_width - ajax_image.width) / 2) + 'px';

	var newDiv = document.createElement('div');
	newDiv.setAttribute(class_attr,'ajax_overlay');
	newDiv.style.width = (offset_width - 10) + 'px';

	newDiv.style.height = (offset_height - 10) + 'px';

	if(obj.childNodes && obj.childNodes.length > 0)
		obj.insertBefore(newDiv,obj.childNodes[0]);
	else
		obj.appendChild(newDiv);
	obj.insertBefore(ajaxImage,newDiv);

	if(+offset_height < +ajax_image.height){
		ajaxImage.style.height = (ajax_image.height + 25) + 'px';
		newDiv.style.height = (ajax_image.height + 25) + 'px';
		obj.style.height = (ajax_image.height + 25) + 'px';
	}

	obj.style.overflow = 'hidden';
}

function create_body_id(){
	if(!document.getElementsByTagName)
		return;

	contentObjs = document.getElementsByTagName(target_tag);

	if(contentObjs.length < 1)
		return;

	for(var x=0;x < contentObjs.length; x++)
		if(contentObjs[x].className.toLowerCase() == target_class){
			contentObjs[x].setAttribute('id',target_content_id);
			break;
		}
}

function fix_filter_width(){
	if(!document.getElementById('customer_filter') || !document.getElementById('customer_filter').childNodes)
		return;

	var filter_width = 0;
	for(var x = 0; x < document.getElementById('customer_filter').childNodes.length; x++)
		filter_width = filter_width + document.getElementById('customer_filter').childNodes[x].offsetWidth;

	document.getElementById('customer_filter').style.width = filter_width + 'px';
}

function GetXmlHttpObject(){
	var xmlHttp
	try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
		try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function addEventToElement(target, eventType, func, useCapture){
	var result = false;

	if (target.addEventListener){
		target.addEventListener(eventType, func, useCapture);
		result = true;
	}else if (target.attachEvent){
		result = target.attachEvent("on" + eventType, func);
	}else{
		alert("Handler could not be attached");
	}

	return result;
}


// BCSE - 0933 -
function bcse_filter_add(type, val) {
	//alert('type='+type+"\nval="+val+"\nfindit="+('filter_choose:'+type.replace(/ /g,'_')));
	var el = document.getElementById('filter_choose:'+type.replace(/ /g,'_'));
	if (el) el.style.display = 'none';

	el = document.getElementById('filter_display:'+type.replace(/ /g,'_'));
	//alert(el);
	if (el) {
		var sp = el.getElementsByTagName('SPAN');
		for (var i=0; i<sp.length; ++i) {
			if (sp[i].className.match(/\bfilter_display\b/)) sp[i].innerHTML = val;
		}
		var ip = el.getElementsByTagName('INPUT');
		for (var i=0; i<ip.length; ++i) {
			if (ip[i].className.match(/\bfilter_input_data\b/)) ip[i].value = val;
		}
		el.style.display = 'block';
	}

	filter_type_shown[type] = true;

	var el = document.getElementById('filter_displays');
	if (el) el.style.display = 'block';

	apply_filter();

	return true;
}

function bcse_filter_remove(type, norefresh) {
	var el = document.getElementById('filter_choose:'+type.replace(/ /g,'_'));
	if (el) el.style.display = '';

	el = document.getElementById('filter_display:'+type.replace(/ /g,'_'));
	//alert(el);
	if (el) {
		/*var sp = el.getElementsByTagName('SPAN');
		for (var i=0; i<sp.length; ++i) {
			if (sp[i].className.match(/\bfilter_display\b/)) sp[i].innerHTML = val;
		}*/
		var ip = el.getElementsByTagName('INPUT');
		for (var i=0; i<ip.length; ++i) {
			if (ip[i].className.match(/\bfilter_input_data\b/)) ip[i].value = '';
		}
		el.style.display = 'none';
	}

	var el = document.getElementById('filter_displays');
	if (el) el.style.display = 'block';

	filter_type_shown[type] = false;
	var show = false;
	for (var k in filter_type_shown) {
		if (filter_type_shown[k]) {
			show = true;
			break;
		}
	}
	if (!show) {
		var el = document.getElementById('filter_displays');
		if (el) el.style.display = 'none';
	}

	if (!norefresh) apply_filter();

	return true;
}

function bcse_filter_remove_all() {
	if (!filter_type_shown) return false;
	for (var k in filter_type_shown) {
		if (filter_type_shown[k]) bcse_filter_remove(k, true);
	}
	apply_filter();
	return true;
}
// /BCSE


var isIE/*@cc_on=1@*/;
var class_attr = 'class' + (isIE ? 'Name' : '');

//var target_tag = 'td';
//var target_class = 'dialogbox';
var target_tag = 'div';
//var target_class = 'dialogbox';
var target_class = 'filter_main_products';
//var target_content_id = 'filter_main_content';
var target_content_id = 'filter_main_content_products';

if(filter_ajax == 'Y'){
	ajax_image = new Image();
	ajax_image.src = skin_dir + '/modules/Advanced_Filter/ajax-loader.gif';

	addEventToElement(window,'load',create_body_id,false);
}

if(isIE)
	addEventToElement(window,'load',fix_filter_width,false);

