function $RequestQueryString(qName, url) {

    if (url == undefined || url == null || url == '')
        url = window.location.search.substring(1);

    var arrParams = url.split("?");
    if (arrParams.length > 1) {

        arrParams = arrParams[1].split("&");

        //    var arrParams = url.split("&");
        for (i = 0; i < arrParams.length; i++) {
            var arrNamesValues = arrParams[i].split("=");
            if (arrNamesValues[0] == qName) {
                if (arrNamesValues.length > 1)
                    return arrNamesValues[1];
            }
        }
    }
    return "";
}

function Browser() {
    var ua, s, i;
    this.isIE = false;
    this.isNS = false;
    this.isFF = false;
    this.isCH = false;
    this.version = null;
    ua = navigator.userAgent;

    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }    
    s = "Firefox";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isFF= true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = "Chrome";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isCH = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}
var browser = new Browser();
//******************************
var divOpened = new Array();
var Products;
function PageProducts (mode, tbl, ddl, pageId, furl, filter, page, pids){
	var thisProd = this;
	this.XmlHttpObj = new BaseXmlHttp("Services/SalesList.aspx");	
	this.params = "pk=" + thisProd.tblKey;
	this.productMode = mode;
	this.tblKey = tbl; 
	this.furlKey = furl; 
	this.ddlProdId = ddl;
	this.pageEmpty = true;
	this.pager = 0;
	this.filterKey = filter; 
	this.page = page;
	this.pageHidId = pageId;
	this.pageHidHTMLId = 'PageNum';
	this.pageIds = pids;
	this.command = '';
    this.SaveProduct = function(){
		var params = "prod=" + thisProd;		
        thisProd.XmlHttpObj.callUrl("SP", params);
	}
    this.BindProducts = function(){
		if (thisProd.pageEmpty){
			window.setTimeout('Products.FirsLoad();',1000); 
			thisProd.pageEmpty = false;
		}
		else window.setTimeout('Products.FirsLoad('+thisProd.pager+');',1000); 
	}
	this.FirsLoad = function(){
		var params = "pk=" + thisProd.tblKey;
		params += "&mode=" + thisProd.productMode;
		params += "&furl=" + thisProd.furlKey; 
				
		//thisProd.ShowLoading();
		thisProd.pager = $(thisProd.pageHidHTMLId).value;
		//alert(thisProd.pager);
		params += "&Page=" + thisProd.pager; 
		thisProd.command = 'FL';
		thisProd.XmlHttpObj.callUrlWithCallback("FL", params, this.InsertHtml);
	}
	this.ClearFilter = function(){	
		if($('tblGroup')){			
			var list = new Array();
			var nodes = document.getElementsByTagName('*')
			for (i = 0; i<nodes.length; i++){
				if (nodes[i].className == 'selected' ){
					nodes[i].className = '';
				}
			}
		}
		thisProd.CloseOpenedGroups();
		var params = "pk=" + thisProd.tblKey;
		params += "&mode=" + thisProd.productMode;
		params += "&furl=" + thisProd.furlKey;
				
		thisProd.ShowLoading();
		thisProd.command = 'CF';
		thisProd.XmlHttpObj.callUrlWithCallback("CF", params, this.InsertHtml);
	}
    this.ChangeMode = function() { 
		var params = "pk=" + thisProd.tblKey;		
		params += "&mode=" + thisProd.productMode;
		params += "&furl=" + thisProd.furlKey;
		
		params += "&CProd=" + thisProd.GetSelectedValue();
		params += "&Page=" + thisProd.pager;  
		thisProd.command = 'CPM';
		thisProd.ShowLoading();
        thisProd.XmlHttpObj.callUrlWithCallback("CPM", params, this.InsertHtml);
    }
    this.SetFilterLI = function(objLI){
		try{
			if(objLI.childNodes[0].tagName == 'a' && objLI.childNodes[0].id.indexOf('aProp') > -1){
				//alert("li click");
				if(browser.isIE){
					objLI.childNodes[0].click();
				}
				/*else if(browser.isCH){
					objLI.children[0].onclick();
					alert("CH");
				}*/
				else if(!browser.isCH){
					var fname = objLI.children[0].getAttribute("onclick").replace("this", "'" + objLI.children[0].id + "'");
					eval(fname);
				}
			}
		}
		catch(ex){alert(ex);};
    }
    this.SetFilter = function(obj, id, key, divId) {
		if (typeof(obj) == "string"){
			if ($(obj))
				obj = $(obj);
			else return;
		}
		try{
		obj.blur();                 // most browsers 
		obj.hideFocus = false;      // internet explorer    
		obj.style.outline = null;   // mozilla 
		}
		catch (e){};
		var params = "pk=" + thisProd.tblKey;		
		params += "&mode=" + thisProd.productMode;
		params += "&furl=" + thisProd.furlKey;
		
		params += "&CProd=" + thisProd.GetSelectedValue();
		params += "&Page=" + thisProd.pager;
		params += "&gid=" + id;
		params += "&pid=" + key;
		params += "&obj=" + obj.id;
		thisProd.ShowLoading();
//    debugger; 
		if (obj.className == 'selected'){
			obj.className = '';
			thisProd.command = 'RF';
		}
		else{
			obj.className = 'selected'; 
			thisProd.command = 'AF';
		}
		
		thisProd.CloseOtherGroups(divId);
		thisProd.XmlHttpObj.callUrlWithCallback(thisProd.command, params, this.InsertHtml);
    }
	this.OpenOtherGroups = function(divId, divW){
		if (divId && $(divId)){
			if ($(divId).className == "OtherGroupsOn")
				thisProd.CloseOtherGroups(divId);
			else{
				$(divId).className ="OtherGroupsOn";
				$(divId).style.width = divW +'px';
				divOpened.push(divId);			
			}
		}
		thisProd.CloseOpenedGroups(divId);
	}
	this.CloseOpenedGroups = function(divId){
		for(i = 0 ; i < divOpened.length; i++ ){
			if (divId == null || divOpened[i] != divId) {
				thisProd.CloseOtherGroups(divOpened[i]);
				divOpened.slice(i,1);
			}
		}
	}
	this.CloseOtherGroups = function(divId){ if (divId && $(divId)) $(divId).className = "OtherGroupsOff"; }
    this.GetProducts = function(prodCountId, pageN){
		var params = "pk=" + thisProd.tblKey;
		params += "&mode=" + thisProd.productMode;
		params += "&furl=" + thisProd.furlKey;
		if (prodCountId)
			thisProd.ddlProdId = prodCountId;
			
		params += "&CProd=" + thisProd.GetSelectedValue();
		
		if (pageN == null || pageN == '' || isNaN(pageN))			
			thisProd.pager = $RequestQueryString('Page')
		else thisProd.pager = pageN;
		
		$(thisProd.pageHidHTMLId).value = thisProd.pager;
		//alert(thisProd.pager);
		
		params += "&Page=" + thisProd.pager;
		var scrollY;
		if (typeof(window.pageYOffset) == 'number'){
			scrollY = window.pageYOffset;
		}
		else if(document.body && document.body.scrollTop){
			scrollY = document.body.scrollTop;
		}
		else if (document.documentElement && document.documentElement.scrollTop){
			scrollY = document.documentElement.scrollTop;
		}
		if (scrollY > 200) window.scrollTo(0,0);
		
		thisProd.ShowLoading();
		thisProd.command = 'RPC';
		thisProd.XmlHttpObj.callUrlWithCallback("RPC", params, this.InsertHtml);
	}
    this.InsertHtml = function() {
		if (thisProd.XmlHttpObj.oXHR){
			if (thisProd.XmlHttpObj.oXHR.readyState == 4){	
				thisProd.HideLoading();
				if (thisProd.XmlHttpObj.oXHR.status == 200) {
					if(thisProd.XmlHttpObj.oXHR.responseText=="")
						return false;
					else{
						var htmltext = thisProd.XmlHttpObj.oXHR.responseText;
						if (htmltext.substr(0,8) == "Error on"){
							htmltext = htmltext.substr(0, htmltext.indexOf('END##'));
							alert(htmltext);
						}
						else{								
							if (htmltext.substr(0,5) == "fun##"){
								var fun = htmltext.substr(5, htmltext.indexOf('##fun')-5);
								htmltext = htmltext.substr(htmltext.indexOf('##fun') + 5);
								eval(fun);
							}
							var mess = '';
							if (htmltext.substr(0,5) == "mes##"){
								mess = htmltext.substr(5, htmltext.indexOf('##mes')-5);
								htmltext = htmltext.substr(htmltext.indexOf('##mes') + 5);
							}
							//alert(htmltext.length)
							$("divProds").innerHTML = htmltext;
							if (mess && mess != '')
								$("mess").innerText = mess;
							ReplacePageNumbers();
							MarkPageLinks();
							if (thisProd.command == 'CPM'){
								if (thisProd.productMode == 'line')
									thisProd.productMode = 'cube';
								else thisProd.productMode = 'line';
							}
						}
					}
				}
				else {
					alert('error on page');	
					//alert(thisProd.XmlHttpObj.oXHR.responseText);					
				}
			}
		}
    }
    this.SetSelected = function (objList){
		if (objList && objList.length > 0){
			var objects = objList.split(',');
			for (i = 0; i< objects.length; i++ ){
				if (objects[i] && $(objects[i])){
					$(objects[i]).className = 'selected'; //$(objects[i]).parentNode.className = 'selected'; 
				}
			}
		}
    }
    this.GetSelectedValue = function(){   
		var ddlProducts = $(thisProd.ddlProdId);
		if (ddlProducts)
			return ddlProducts[ddlProducts.selectedIndex].value;
		return '';
    }
    this.ShowLoading = function(){ if ($("load"))  $("load").style.display = 'block'; }
    this.HideLoading = function(){ if ($("load"))  $("load").style.display = 'none'; }
}
