﻿var rows=0;
	    
//The core function to get the xml http request object
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0;
var ie = false;
var nav = false;
    if (navigator.appName.indexOf("Netscape") != -1) {
    	nav = true; 
    }
    if (navigator.appName.indexOf("Microsoft") != -1) {
	    ie = true;
    }
    if (navigator.appName.indexOf("Opera") != -1) {
	    ie = true;
    }

function GetXmlHttpObject(){ 
	var objXMLHttp=null
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject) {
	    var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
		objXMLHttp=new ActiveXObject(strObjName)
	}
	return objXMLHttp
}

var db = (document.body) ? 1 : 0;
var scroll = (window.scrollTo) ? 1 : 0;
	
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
	begin = dc.indexOf(prefix);
	if (begin != 0) return null;
} else {
	begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}	

function saveScroll() {

    if (!scroll) return;
    var now = new Date();
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    var x = (db) ? document.body.scrollLeft : pageXOffset;
    var y = (db) ? document.body.scrollTop : pageYOffset;
    setCookie("y",  y, now);
}
function setCookie(name, value, expires, path, domain, secure) {

var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
document.cookie = curCookie;
}

iMyTimer = null; //so the search not go to fast
var url;
function update_count(field,counter,btn,maxchars,internet,tabindex,tabid,marketid,languageid) {

	if(iMyTimer != null){
		clearTimeout(iMyTimer);
		iMyTimer = null;
	}
    var fName=document.forms[0];
    var sValue = fName.elements[field].value;
	tot_len = sValue.length;

	if (tot_len > 1) {
	    tbl = document.getElementById("tblSearchResult")
	    if (tbl.tagName == "TABLE") {
	        //clear all rows first
	        while (tbl.rows.length > 0)
	            tbl.deleteRow(0);
	    }
	    var mainContainer = document.getElementById("divSearch");
	    mainContainer.style.display = 'none';
	    mainContainer.style.visibility = 'hidden'; 
	     
	    //display div
	    mainContainer.style.display = '';	
	    mainContainer.style.visibility = 'visible';
	
	    //Get the Go Button
	    var btnObj = document.getElementById(btn);
        var btnPosLeft = DSgetLeft(btnObj);
        var btnPosBottom = DSgetTop(btnObj) + btnObj.offsetHeight;  
        
        
       // alert(mainContainer.style.left);
        

        if (nav) {
	        mainContainer.style.left = btnPosLeft - 151;
	        mainContainer.style.top = btnPosBottom+1;  
        }
        if (ie) {
	        mainContainer.style.posLeft = btnPosLeft - fName.elements[field].style.pixelWidth;
	        mainContainer.style.posTop =  btnPosBottom+3;            
        }
	    
	    //show the processing image to the user
	    btnObj.src = myApl+"/images/loadingV2.gif";
       

	    //get the xml http object
	    xmlHttp=GetXmlHttpObject()
	    if (xmlHttp==null){
		    alert ("Browser does not support HTTP Request")
		    return
	    } 
	
	    //build the processing page and it's query string
	    //invoke a request to the server using javascript async call
	    xmlHttp.onreadystatechange=stateChangedEx2
	    url="?tabindex="+tabindex+"&tabid="+tabid+"&L="+languageid+"&M="+marketid;
	   
	    xmlHttp.open("GET",myApl+"/DesktopBannerSearch.aspx"+url +"&i="+internet+"&s="+escape(replaceCharacters(sValue)),true)
	    iMyTimer = setTimeout(function () {xmlHttp.send(null);}, 500);
	    
	    setTimeout(function () {btnObj.src = myApl+"/images/goButtonV2.gif";}, 500);
	}
	
}
//The extented method to check the http request state has changed
//Then the dynamic div tag will be filled with the response text
function stateChangedEx2() { 
//alert(divObj);
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		var myArray;
        myArray = xmlHttp.responseText.split(";");
         //build my table
         tbl=document.getElementById("tblSearchResult");
         if (tbl.tagName == "TABLE") {
           
            
		    //alert("TABLE");
		    //clear all rows first
		    //for(var i = tbl.rows.length - 1; i > 0; i--){
		       // tbl.deleteRow(i);
		    //}
		    //scroll to top
		    var myClass="DimSketchTable";
		    rows=0;
		    startrow=0;
		    if (navigator.appName.indexOf("Microsoft") == -1) {
	            //add close button to opera,netscape - onmouseleave don't working
                var r1 = tbl.insertRow(0);
                var c1 = r1.appendChild( document.createElement("TD") );
                c1.innerHTML="<img src='"+myApl+"/images/CalendarSmallDelete.gif' height='12' width='12' style='cursor:pointer;' alt='close' onclick='HideSearchPanel();' />"
                c1.align='right';
                startrow=1;
            }

		    //add new ones
		    for (i=0;i<myArray.length;i++){
		    
		        var displaytext = myArray[i].split("|")[0];
		        if (displaytext!=""){
		        
		            var searchtext = myArray[i].split("|")[1];
		            var searchkey = myArray[i].split("|")[2]; //itemgroup or userid
		            var splitcode = myArray[i].split("|")[3];
		            var type = myArray[i].split("|")[4];
		            		            
		            rows++
		        
		            var r = tbl.insertRow(i+startrow);
		        
		            if (myClass != "DimSketchTable"){
		                myClass = "DimSketchTable"
		            }else{
		                myClass = "DimSketchTableAlt"
		            }
		            r.className = myClass;
		        
		            var c = r.appendChild( document.createElement("TD") );

		            switch (type) {
		                case "FamilyText":
		                    c.innerHTML = "<a href='" + myApl + "/eCat/DataSheet.aspx" + url + "&T=" + splitcode + "&itemgroup=" + searchkey + "' class='CommandButton' style='text-decoration:none;' onMouseOver='Sel(" + String.fromCharCode(34) + searchtext + String.fromCharCode(34) + ")'>" + displaytext + "</a>";
		                    break;
        			    case "Contacts":
        			        c.innerHTML = "<a href='"+myApl+"/DesktopDefault.aspx"+url+"&userid="+searchkey+"' class='CommandButton' style='text-decoration:none;' onMouseOver='Sel("+String.fromCharCode(34)+searchtext+String.fromCharCode(34)+")'>"+displaytext+"</a>";
        			        break;
        			    case "Found":
        			        c.innerHTML = displaytext
        			       
        			        break;
        			    default:
		                    c.innerHTML = myArray[i];
		                    // alert(type)
        			        break;
                    }		        
		        
                    c.className = "eCatNormal";
                
		        }
		        
             }
		 }
		 
	     if (rows==0){
	        var mainContainer = document.getElementById("divSearch");
    	    mainContainer.style.display = 'none';	
    	    mainContainer.style.visibility = 'hidden';
        }
        /*
        var r = tbl.insertRow(rows);
        r.className = myClass;
		var c = r.appendChild( document.createElement("TD") );
        c.innerHTML = rows+" found!";
        c.className = "eCatNormalBold";
        */
	} 
} 
	function replaceCharacters(html) {
		// Special characters and their HTML equivelent
		var set = [
			["€","‘","’","’","“","”","–","—","¡","¢","£","£","¤","¥","¦","§","¨","©","ª","«","¬","­","®","¯","°","±","²","³","´","µ","¶","·","¸","¹","º","»","¼","½","¾","¿","À","Á","Â","Ã","Ä","Å","Æ","Ç","È","É","Ê","Ë","Ì","Í","Î","Ï","Ð","Ñ","Ò","Ó","Ô","Õ","Ö","×","Ø","Ù","Ú","Û","Ü","Ý","Þ","ß","à","á","â","ã","ä","å","æ","ç","è","é","ê","ë","ì","í","î","ï","ð","ñ","ò","ó","ô","õ","ö","÷","ø","ù","ú","û","ü","ü","ý","þ","ÿ"],
			["&euro;","&lsquo;","&rsquo;","&rsquo;","&ldquo;","&rdquo;","&ndash;","&mdash;","&iexcl;","&cent;","&pound;","&pound;","&curren;","&yen;","&brvbar;","&sect;","&uml;","&copy;","&ordf;","&laquo;","&not;","­","&reg;","&macr;","&deg;","&plusmn;","&sup2;","&sup3;","&acute;","&micro;","&para;","&middot;","&cedil;","&sup1;","&ordm;","&raquo;","&frac14;","&frac12;","&frac34;","&iquest;","&Agrave;","&Aacute;","&Acirc;","&Atilde;","&Auml;","&Aring;","&AElig;","&Ccedil;","&Egrave;","&Eacute;","&Ecirc;","&Euml;","&Igrave;","&Iacute;","&Icirc;","&Iuml;","&ETH;","&Ntilde;","&Ograve;","&Oacute;","&Ocirc;","&Otilde;","&Ouml;","&times;","&Oslash;","&Ugrave;","&Uacute;","&Ucirc;","&Uuml;","&Yacute;","&THORN;","&szlig;","&agrave;","&aacute;","&acirc;","&atilde;","&auml;","&aring;","&aelig;","&ccedil;","&egrave;","&eacute;","&ecirc;","&euml;","&igrave;","&iacute;","&icirc;","&iuml;","&eth;","&ntilde;","&ograve;","&oacute;","&ocirc;","&otilde;","&ouml;","&divide;","&oslash;","&ugrave;","&uacute;","&ucirc;","&uuml;","&uuml;","&yacute;","&thorn;","&yuml;"]
		];
		// Replace each instance of one of the above special characters with it's HTML equivelent
		if (html) {
			for(var j = 0; j < set[0].length; j++){
				html = html.replace(eval("/"+set[0][j]+"/g"),set[1][j]);
			}
		}
		// Return the HTML or an empty string if no HTML was supplied
		return html;
	}
function Sel(familytext){
    var fName=document.forms[0];
    fName.elements[myControl+"_txtSearch1"].value=familytext;
}
function HideSearchPanel(){
    var mainContainer = document.getElementById("divSearch");
        mainContainer.style.visibility = 'hidden';
}

function DSgetTop(Elem) 
{
 var yPos = Elem.offsetTop;
 var tempEl = Elem.offsetParent;
 while (tempEl != null) 
 {
  yPos += tempEl.offsetTop;
  tempEl = tempEl.offsetParent;
 }
 return yPos;
}

function DSgetLeft(Elem) 
{
 xPos = Elem.offsetLeft;
 tempEl = Elem.offsetParent;
 while (tempEl != null) 
 {
  xPos += tempEl.offsetLeft;
  tempEl = tempEl.offsetParent;
 }
  return xPos;
}