﻿
var _MSXML_PROGIDS = new Array('Msxml2.XMLHTTP.6.0',
                               'Msxml2.XMLHTTP.4.0',
                               'Msxml2.XMLHTTP.3.0',
                               'Msxml2.XMLHTTP',
                               'Microsoft.XMLHTTP');

function getXmlhttpObject() {
    var oXHR = null;
    if (typeof XMLHttpRequest != "undefined")
        oXHR = new XMLHttpRequest();
    else {
        for (var i = 0; i < _MSXML_PROGIDS.length; i++) {
            try {
                oXHR = new ActiveXObject(_MSXML_PROGIDS[i]);
                _MSXML_PROGIDS = [_MSXML_PROGIDS[i]];
                break;
            }
            catch (e) {
                oXHR = null;
            }
        }
    }
    return oXHR;
}

function BaseXmlHttp(destSubUrl) {
    this.oXHR = null;
    this.destUrl = destSubUrl;
    var self = this;
    this.callUrlDefault = function()  {this.callUrlWithCallback("", "", this.onCallAnswerd); }
    this.callUrlNoCallback = function(ajaxCommand, callData) {this.callUrlWithCallback(ajaxCommand, callData, this.onCallAnswerd); }
    this.callUrlWithCallback = function(ajaxCommand, callData, methodToCall) {
		var NoCache = new Date().getTime();				
		var url =	this.getUrlPreffix() + 
					this.destUrl + 
					"?NC=" + NoCache;
		if (ajaxCommand && ajaxCommand != "") url += "&AC=" + ajaxCommand;
		if (callData && callData != "") url += "&" + callData;
        this.oXHR = getXmlhttpObject();
        if (this.oXHR == null)  return;
        try {
			if (methodToCall == null)
				methodToCall = this.onCallAnswerd// alert('no calback');
			
			this.oXHR.open("GET", url, true);
			this.oXHR.onreadystatechange = methodToCall;
			this.oXHR.send(null);
        }
        catch (e) { }
    }
    this.callUrlWithFunc = function(ajaxCommand, callData, methodToCall) {
		var NoCache = new Date().getTime();				
		var url =	this.getUrlPreffix() + 
					this.destUrl + "?NC=" + NoCache;
		if (ajaxCommand && ajaxCommand != "") url += "&AC=" + ajaxCommand;
		if (callData && callData != "") url += "&" + callData;
		//var url = curUrl + "Services/SalesList.aspx?AC=" + ajaxCommand + "&" + callData;
        this.oXHR = getXmlhttpObject();
        if (this.oXHR == null) return;
        try {
			if (methodToCall == null)
				methodToCall = this.ajaxCallBack ;// alert('no calback');
				
			this.oXHR.open("GET", url, true);
			this.oXHR.onreadystatechange = methodToCall;
			this.oXHR.send(null);
        }
        catch (e) { }
    }
    this.getUrlPreffix = function() {
		var curUrl = window.location.href.toLowerCase();
		var endIndex = curUrl.indexOf("templates")
		curUrl = curUrl.substring(0, endIndex);
		return curUrl;
    }
    this.ajaxCallBack = function(){
		if (self.oXHR.readyState == 4){
			if (self.oXHR.status == 200) {
				if(self.oXHR.responseText=="")
					return false;
				else{
					//alert(xmlHttp.responseText);
					eval(self.oXHR.responseText);
				}
			}
		}
    }
    this.onCallAnswerd = function() { }
    this.cleanUp = function() { self.oXHR = null; }
}

function GetUserName(){	
	var http = new BaseXmlHttp("Services/GetData.aspx");
	http.callUrlWithFunc("UN","");
}
function ClearFilter(){
	if (window.location.href.toLowerCase().indexOf("/cn-") == -1 && 
		window.location.href.toLowerCase().indexOf("/p-") == -1 &&
		window.location.href.toLowerCase().indexOf("/pr-") == -1 &&
		window.location.href.toLowerCase().indexOf("/pb-") == -1){
		var http = new BaseXmlHttp("Services/SalesList.aspx")
		http.callUrlWithFunc("CC","");
	}
}


function SetUserName(loc, userName)
{
	if (userName != null && userName.length > 0)
	{
		while (userName.indexOf("&#39;") != -1)
			userName = userName.replace("&#39;", "'");
		while (userName.indexOf("&#34;") != -1)
			userName = userName.replace("&#34;", "\"");
		$("SiteHeader1_lblFullName").innerText = userName;
		$("SiteHeader1_lblDefault").innerText = '';
		$("SiteHeader1_lblBull1").innerHtml = '&bull;';
		$("SiteHeader1_lbMyAccount").innerText = 'החשבון שלי';
		$("SiteHeader1_lblBull2").innerHtml = '&bull';
		$("SiteHeader1_lbDisconnect").innerText = 'התנתק';
	}
	else
	{
		$("SiteHeader1_lblDefault").innerHTML = 'אורח';
		$("SiteHeader1_lblFullName").innerHTML = '';
		$("SiteHeader1_lblBull1").innerHTML = '';
		$("SiteHeader1_lbMyAccount").innerHTML = '';
		$("SiteHeader1_lblBull2").innerHTML = '';
		$("SiteHeader1_lbDisconnect").innerHTML = '';
	}
}
function OpenPass(){
	$('divPass').className = 'PassOn';
	$('CheckOutLogin1_txtPassUserId').value = "";
	$('CheckOutLogin1_lblStatus').innerHTML = "";
	if ($('CheckOutLogin1_txtLoginUserId'))
		$('CheckOutLogin1_txtPassUserId').value = $('CheckOutLogin1_txtLoginUserId').value;
	$('CheckOutLogin1_txtPassUserId').focus();
}

function ClosePass(){
	window.scrollTo(0,800); 
	$('divPass').className = 'PassOff';
	$('CheckOutUser1_txtFirstName').focus();
}
function GetUserData(txtId, statusId){
	var thisObj = this;
	this.statusId = statusId;
	this.http = new BaseXmlHttp("Services/GetData.aspx");
	if ($(txtId)){
		$(txtId).value = Trim($(txtId).value);
		if ($(txtId).value == ""){
			$(statusId).innerHTML = "אנא הזן/הזיני תעודת זהות תקינה";
			return;
		}
		if ($(txtId).value.length < 9){
			$(statusId).innerHTML = "הנתונים שהזנת אינם תקינים ,אנא הזן תעודת זהות תקינה (9 ספרות כולל ספרת ביקורת)";
			return;
		}
		var url = "UID=" +$(txtId).value;	
		thisObj.http.callUrlWithCallback("UP", url, 
									function(){
										if (thisObj.http.oXHR){
											if (thisObj.http.oXHR.readyState == 4){	
												if (thisObj.http.oXHR.status == 200) {
													if(thisObj.http.oXHR.responseText=="")
														return false;
													else{
														if($(thisObj.statusId))
															$(thisObj.statusId).innerHTML = thisObj.http.oXHR.responseText;
													}
												}
												//else{ alert('error'); }
											}
										}
									}
		);
	}
}

function GetCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) 
				c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}

function AddCookie(sName,sValus)
{
	date = new Date();
	date.setYear(date.getYear()+ 1)
	document.cookie = sName + "=" + escape(sValus) + "; expires=" + date.toGMTString();
}
function DeleteCookie(sName)
{
	document.cookie = sName + "=" + escape("") + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function Find_ClearCategory(catName)
{
	ClearDDL("SiteSearch1_ddlSubCat",null);		
	ClearDDL("SiteSearch1_ddlBrands",null);
	catName = Trim(catName);
	Find_CallData(catName, false);
}
function Find_SaveSubCategory(catName)
{
	var subCat = $("SiteSearch1_hidSubValue");
	subCat.value = catName;
	
	ClearDDL("SiteSearch1_ddlBrands",null);
	Find_CallData(catName, true);
}
function Find_CallData(catName, isBarandOnly)
{
	var http = new BaseXmlHttp("Services/GetData.aspx");
	var url = "catName=" +catName;
	if (isBarandOnly)
		url = url + "&isBarandOnly=1"
	else url = url + "&isBarandOnly=0"
	http.callUrlWithFunc("FCD",url);
}

function Find_SetData(loc, data, isBrand)
{
	var newSelect = $("SiteSearch1_ddlSubCat");
	if (isBrand)
		newSelect = $("SiteSearch1_ddlBrands");

	if (isBrand)
		optionItem = new Option( "בחר מותג", "", false, false);
	else optionItem = new Option( "בחר מחלקה", "", false, false);
		
	newSelect.options[newSelect.length] = optionItem;
	//alert(categories);
	if (data != null && data.length > 0)
	{
		while (data.indexOf("&#39;") != -1)
			data = data.replace("&#39;", "'");
		while (data.indexOf("&#34;") != -1)
			data = data.replace("&#34;", "\"");
		
		var list = data.split(';');
		var item;
		for (var i = 0; i < list.length -1; i++)
		{
   			item = list[i].split('#');
			optionItem = new Option( item[0], item[1], false, false);
			newSelect.options[newSelect.length] = optionItem;
		}
	}
}

function ClearCategory(catName)
{
	ClearDDL("nextLevel", null);
	ClearDDL("ddlBrands", null);
			
	$("hidTatCatValue").value  = catName;
	levelIndex = 0;
	
	CallData(catName, false);
	var chLen = $("ddlSubCat").children.length -1
	for (i = chLen ; i > 0; i--)
	{
		child = $("ddlSubCat").children[i];
		
		if (child) 	
		{
			$("ddlSubCat").removeChild(child);
		}
	}
}

function GetCategoryData(objId,catName, isSubCat)
{
	var nextLevel = $("nextLevel");
	var index = objId.indexOf('level');
	if (index == -1)
	{
		ClearDDL("nextLevel", null);
		ClearDDL("ddlBrands", null);
		levelIndex = 1;
	}
	else
	{
		var num = objId.substr(index + 5, objId.length);
		var child ;
		var chLen = nextLevel.children.length -1
		for (i = chLen ; i>= num *2; i--)
		{
			child = nextLevel.children[i];
			
			if (child) 	
			{
				nextLevel.removeChild(child);
			}
		}
		num ++;
		levelIndex = num ;
		ClearDDL("ddlBrands", null);
	}
	
	catName = Trim(catName);
	var categoryForBrands = $("hidSubCatValue").value;
	if (isSubCat == 1)
		$("hidSubCatValue").value  = catName;
	if (catName.length > 0) {
		$("hidTatCatValue").value  = catName;
		CallData(catName,false);
	}
	else {
		if (isSubCat == 1)		
			CallData(categoryForBrands , true);
		else
			CallData($("hidTatCatValue").value, true);
	}
}


function CallData(catName,isBarandOnly)
{
	var http = new BaseXmlHttp("Services/GetData.aspx");
	var url = "catName=" +catName;
	if (isBarandOnly)
		url = url + "&isBarandOnly=1"
	else url = url + "&isBarandOnly=0"
	http.callUrlWithFunc("CD",url);
}

function SetData(loc, data, isBrand)
{
	if (data != null && data.length > 0)
	{
		while (data.indexOf("&#39;") != -1)
			data = data.replace("&#39;", "'");
		while (data.indexOf("&#34;") != -1)
			data = data.replace("&#34;", "\"");
	}
	var list = data.split(';');
	var item;
	if (!isBrand && levelIndex == 0)
	{			
		for (var i = 0; i < list.length -1; i++)
		{
   			item = list[i].split('#');
			optionItem = new Option( item[0], item[1], false, false);
			$("ddlSubCat").options[$("ddlSubCat").length] = optionItem;
		}
	}
	else
	{ 
		var optionItem = null;
		var nextLevel = $("nextLevel");	
		var newSelect = document.createElement("select");
		newSelect.options.length = 0;
		if (isBrand) {
			optionItem = new Option( "בחר מותג", "", false, false);
			newSelect = $("ddlBrands");	
		}
		else if (data.length > 0){	
			var br = document.createElement("br");
		
			newSelect.name = "level" + levelIndex;
			newSelect.id = newSelect.name;
			newSelect.style.width = "160px";
			
			br.id = "br" + levelIndex;
			levelIndex++;
			
			newSelect.onchange = GetCategoryData;
			newSelect.onchange = function() {GetCategoryData(this.id,this.options[this.selectedIndex].value)};						
		
			optionItem = new Option( "בחר מחלקה", "", false, false);
			nextLevel.appendChild(newSelect);
			nextLevel.appendChild(br);
		}
		if (optionItem != null){
			newSelect.options[newSelect.length] = optionItem;
		
			for (var i = 0; i < list.length -1; i++)
			{
   				item = list[i].split('#');
				optionItem = new Option( item[0], item[1], false, false);
				newSelect.options[newSelect.length] = optionItem;
			}
		}
	}
}

function GetWinerTable(prodId)
{
	var http = new BaseXmlHttp("Services/GetData.aspx");
	http.callUrlWithFunc("WT","PID="+prodId);
}

function SetWinTable(table)
{
	if (table != null && table.length > 0)
	{
		while (table.indexOf("&#39;") != -1)
			table = table.replace("&#39;", "'");
		while (table.indexOf("&#34;") != -1)
			table = table.replace("&#34;", "\"");
		
		if($("winList"))
			$("winList").outerHTML = table;
	}
}
function SetBrandsData(loc, data)
{
	SetData(loc, data, true)
}
function Find_SetBrandsData(loc, data)
{
	Find_SetData(loc, data, true)
}
function ClearDDL(ddlId, ddlObj)
{
	var ddl = $(ddlId);
	if (ddlObj )
		ddl = ddlObj;
	while (ddl.firstChild)
		{ddl.removeChild(ddl.firstChild);};
}

function MarkPageLinks(){
	var links = document.getElementsByTagName('a');
	if (links.length == 0) alert(links.length);
	if (window.attachEvent) 
        b=1;
    if (window.addEventListener) 
        b=2;
    
	/*for (var i = 0; i < links.length; i++) {
		 switch (b) {
			case 1: links[i].attachEvent("onclick",setGlobal);
                break;
			case 2: links[i].setAttribute("onclick","return setGlobal()")
				break;
			default: //do nothing
		}
		//links[i].onclick = setGlobal;
	}*/
}
function setGlobal() { window.linkClicked = true;}
window.onunload = function() {
	if (typeof window.linkClicked == 'undefined' || !window.linkClicked) {
		// they are leaving some other way than by clicking a link
		/*alert('browser');
		try{Products.ClearFilter();}
		catch (e) {};*/
	}
	else alert('link');
}


