var ns4 = document.layers;
var ns6 = document.getElementById && !document.all || (navigator.userAgent.indexOf('Opera') >= 0);
var ie4 = document.all;

function addView(listViewName)
{
	var wnd = window.open("index_popup.php?md=Affiliate_Merchants_Views_ListViews&action=add&listViewName="+escape(listViewName)+"&<?php echo SID?>","EditView","scrollbars=1, top=100, left=100, width=550, height=500, status=0")
    wnd.focus();
}

function editView(ID, listViewName)
{
	var wnd = window.open("index_popup.php?md=Affiliate_Merchants_Views_ListViews&action=edit&vid="+ID+"&listViewName="+escape(listViewName)+"&<?php echo SID?>","EditView","scrollbars=1, top=100, left=100, width=550, height=500, status=0")
    wnd.focus();
}

function deleteView(ID, listViewName, confirmText)
{
    if(confirm(confirmText))
    {
        var wnd = window.open("index_popup.php?md=Affiliate_Merchants_Views_ListViews&action=delete&vid="+ID+"&listViewName="+escape(listViewName)+"&<?php echo SID?>","EditView","scrollbars=1, top=100, left=100, width=200, height=100, status=0")
        wnd.focus();
    }
}

function submitView()
{
    FilterForm.submit();
}

function performAction(sel)
{
  if(sel.value!='-')
  {
    eval(sel.value);
  }
  sel.selectedIndex = 0;
}

function checkAllItems()
{
  var checks = document.getElementsByName("itemschecked[]");
  if(typeof(checks) == 'undefined') return;

  checkedAllItems = !checkedAllItems;

  if(checks.length > 0)
  {
    for (var b = 0; b < checks.length; b++)
    {
      if(checkedAllItems == true)
        checks[b].checked = true;
      else
        checks[b].checked = false;
    }
  }
  else
  {
    if(checkedAllItems == true)
      checks.checked = true;
    else
      checks.checked = false;
  }

  var buttons = document.getElementById("checkItemsButton");
  if(typeof(buttons) == 'undefined') return;
  if(buttons.length > 0)
  {
    for (var b = 0; b < buttons.length; b++)
    {
      if(checkedAllItems == true)
        buttons[b].value = '[  ]';
      else
        buttons[b].value = '[X]';
    }
  }
  else
  {
    if(checkedAllItems == true)
      buttons.value = '[  ]';
    else
      buttons.value = '[X]';
  }
}

function checkAll(name, check)
{
  var checks = document.getElementsByName(name);
  if(typeof(checks) == 'undefined') return;

  if(checks.length > 0)
  {
    for (var b = 0; b < checks.length; b++)
    {
        checks[b].checked = check;
    }
  }
  else
  {
      checks.checked = check;
  }
}

function addAffiliate()
{
	var wnd = window.open("index_popup.php?md=AffiliateManager&action=add"+"&<?php echo SID?>","AddAffiliate","scrollbars=1, top=100, left=100, width=500, height=560, status=0");
    wnd.focus();
}

var treeopened = null;
function openMenuItems(myId)
{
    if (document.getElementById)
    {
        var elem = document.getElementById (myId);
        if (elem && elem.className)
        {
            elem.className = (elem.className == 'leftMenuTableOpened') ? 'leftMenuTableClosed' : 'leftMenuTableOpened';
        }
    }

    return false;
}

function openItem(myId)
{
    if (document.getElementById)
    {
        var elem = document.getElementById (myId);
        if (elem && elem.className)
        {
            elem.className = (elem.className == 'tableOpened') ? 'tableClosed' : 'tableOpened';
        }
    }

    return false;
}

function showListOptions()
{
	if(document.getElementById)
	{
			if(document.getElementById("view_av_options").style.display=="block")
			{
				document.getElementById("view_av_options").style.display="none";
			}
			else
			{
				document.getElementById("view_av_options").style.display="block";
			}
	}
}

function showAdvancedFilter(preffix, filterShowedField)
{
    if(filterShowedField.value=="1")
			{
				document.getElementById(preffix+'advanced_filter').style.display="none";
				document.getElementById(preffix+'standard_filter').style.display="block";
				document.getElementById(preffix+'advanced_filter_caption').style.display="none";
				document.getElementById(preffix+'standard_filter_caption').style.display="block";

				filterShowedField.value="0";
			}
			else
			{
                document.getElementById(preffix+'standard_filter').style.display="none";
				document.getElementById(preffix+'advanced_filter').style.display="block";
				document.getElementById(preffix+'standard_filter_caption').style.display="none";
				document.getElementById(preffix+'advanced_filter_caption').style.display="block";

				filterShowedField.value="1";
			}


    return;

}

function showInfoRow(caption_id, name, text, moretext, lesstext)
{
    var row = document.getElementById(name);
    var captions = document.getElementsByName(name+'_caption');
    var id = 0;
    if(captions.length > 0) {
        for (var i = 0; i < captions.length; i++) {
            if(captions[i].id == caption_id) {
                id = i;
            }
        }
        if(captions[id].title == "more")
        {
            for (var i = 0; i < captions.length; i++) {
                captions[i].innerHTML = moretext;
            }
            captions[id].title = 'less';
            captions[id].innerHTML = lesstext;
            document.getElementById(name+'_td').innerHTML = text;
            if(ns4 || ns6) {
                row.style.display = "table-row";
            } else {
                row.style.display = "inline";
            }
        }
        else
        {
            for (var i = 0; i < captions.length; i++) {
                captions[i].title = 'more';
                captions[i].innerHTML = moretext;
            }
            row.style.display = "none";
        }
    } else {
        if(row.style.display == "none")
        {
            captions.title = 'less';
            captions.innerHTML = lesstext;
            document.getElementById(name+'_td').innerHTML = text;
            if(ns4 || ns6) {
                row.style.display = "table-row";
            } else {
                row.style.display = "inline";
            }
        }
        else
        {
            captions.title = 'more';
            captions.innerHTML = moretext;
            row.style.display = "none";
        }
    }

    return;
}

function hideTabs(preffix) {
    var divs = document.getElementsByTagName('div');
    var id;
    if(divs.length > 0) {
        for (var b = 0; b < divs.length; b++) {
            if (divs[b].id.indexOf(preffix) != -1) {
                if (divs[b].style.display="block") {
                    divs[b].style.display="none";
                    id = divs[b].id.slice(preffix.length);
                    document.getElementById(preffix+id+'_left1').className = 'tabEdgeContentDisabled';
                    document.getElementById(preffix+id+'_left2').className = 'tabEdgeContentDisabled';
                    document.getElementById(preffix+id+'_content').className = 'tabContentDisabled';
                    document.getElementById(preffix+id+'_right1').className = 'tabEdgeContentDisabled';
                    document.getElementById(preffix+id+'_right2').className = 'tabEdgeContentDisabled';
                }
            }
        }
    } else {
        divs.style.display="none";
    }
}

function showTab(preffix, id) {
    hideTabs(preffix);
    document.getElementById(preffix+id).style.display="block";
    document.getElementById(preffix+id+'_left1').className = 'tabEdgeContent';
    document.getElementById(preffix+id+'_left2').className = 'tabEdgeContent';
    document.getElementById(preffix+id+'_content').className = 'tabContent';
    document.getElementById(preffix+id+'_right1').className = 'tabEdgeContent';
    document.getElementById(preffix+id+'_right2').className = 'tabEdgeContent';
    document.getElementById(preffix+'sheet').value = id;
}

function showPopupHelp(helpID)
{
	var wnd = window.open("index_popup.php?md=QUnit_Help_Help&hid="+helpID+"&<?php echo SID?>","helpwnd","scrollbars=1, top=100, left=100, width=350, height=250, status=0");
    wnd.focus();
}

function navigationSet(html_code) {
   document.getElementById('pam_navigation').innerHTML = html_code;
}

var requester = null;

var offsetX = 0;
var offsetY = 20;
var toolTipSTYLE="";
var toolTipContentSTYLE=""; //just for ie4
var CAPTION='cap';
var FG='fg';
var BG='bg';
var TEXTCOLOR='tc';
var CAPTIONCOLOR='cc';
var WIDTH='tw';
var HEIGHT='th';
var FONT='font';
var POSITIONY='posy';
var hideToolTip=true;
var cap, fg, bg, tc, cc, tw, th, font, posy = 0;

function initToolTips() {
    if(ns4||ns6||ie4) {
        if(ns4) { toolTipSTYLE = document.toolTipLayer; toolTipContentSTYLE = document.all.toolTipLayerContent.style; }
        else if(ns6) { toolTipSTYLE = document.getElementById("toolTipLayer").style; }
        else if(ie4) { toolTipSTYLE = document.all.toolTipLayer.style; }
        if(ns4) { ; } else {
            toolTipSTYLE.visibility = "visible";
            toolTipSTYLE.display = "none";
        }
    }
}

function showLoadedData() {
    var msg = 'Error!';
    if (requester.readyState == 4) {
        if (requester.status == 200) {
            msg = requester.responseText;
        } else {
            msg = "加载信息出错！";
        }
    }
    document.getElementById('toolTipLayerContent').innerHTML = msg;
    return true;
}

function toolTipHide() {
    if(ns4) {
        toolTipSTYLE.visibility = "hidden";
    } else {
        toolTipSTYLE.display = "none";
        var IfrRef = document.getElementById('DivShim');
        IfrRef.style.display = "none";
    }
}

function toolTip() {
    if(arguments.length < 1) { // hide
        if (hideToolTip == false) {
            return;
        }
        if(ns4) {
            toolTipSTYLE.visibility = "hidden";
        } else {
            toolTipSTYLE.display = "none";
            var IfrRef = document.getElementById('DivShim');
            IfrRef.style.display = "none";
        }
    } else { // show
        e = arguments[0];
        var msg = arguments[1];
        var type = arguments[2];
        fg = "#000000";
        bg = "#DDDDDD";
        tc = "#000000";
        cc= "#FFFFFF";
        font = "Verdana,Arial,Helvetica";
        tw = '';
        th = '';
        cap = '';
        posy = 0;
        for(var i = 3; i < arguments.length; i+=2) {
            switch (arguments[i]) {
                case "cap": cap = arguments[i+1]; break;
                case "font": font = arguments[i+1]; break;
                case "fg": fg = arguments[i+1]; break;
                case "bg": bg = arguments[i+1]; break;
                case "tc": tc = arguments[i+1]; break;
                case "cc": cc = arguments[i+1]; break;
                case "tw": tw = arguments[i+1]; break;
                case "th": th = arguments[i+1]; break;
                case "posy": posy = arguments[i+1]; break;
            }
        }
        var imgdir = '../templates/affiliate/default/images/';
        var content = '';
        switch (type) {
            case '2':
                titletext = '信息';
                break;
            case '3':
                titletext = '选择颜色';
                msg = msg.replace(/{SLASH}/g, '\"');
                break;
            case '4':
                titletext = '详细信息';
                try {
                    requester = new XMLHttpRequest();
                }
                catch (error) {
                    try {
                        requester = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (error) {
                        msg = "Error initialising AJAX";
                        break;
                    }
                }
                requester.open("GET", msg, true);
                requester.send(null);
                requester.onreadystatechange = showLoadedData;
                msg = "<center><img src='"+imgdir+"loading.gif' border='0' /></center>";
                break;
            default:
                titletext = '帮助';
                break;
        }

        if(ns4) {
            toolTipContentSTYLE.document.write(msg);
            toolTipContentSTYLE.document.close();
            toolTipSTYLE.visibility = "visible";
        } else if(ns6) {
            moveToMouseLoc(e);
            document.getElementById("toolTipLayerContent").innerHTML = msg;
            toolTipSTYLE.display='block';
        } else if(ie4) {
            moveToMouseLoc();
            document.all("toolTipLayerContent").innerHTML=msg;
            toolTipSTYLE.display='block';
            var IfrRef = document.getElementById('DivShim');
            var DivRef = document.getElementById('toolTipLayer');
            IfrRef.style.width = DivRef.offsetWidth;
            IfrRef.style.height = DivRef.offsetHeight;
            IfrRef.style.top = DivRef.style.top;
            IfrRef.style.left = DivRef.style.left;
            IfrRef.style.zIndex = DivRef.style.zIndex - 1;
            IfrRef.style.display = "block";
        }
    }
}

function moveToMouseLoc(e, tw, th) {
    if(ns4||ns6) {
        x = e.pageX;
        y = e.pageY;
    } else {
        x = event.x + document.body.scrollLeft;
        y = event.y + document.body.scrollTop;
    }

    //if use bottom-right tooltip box
    //ff需要加上单位
    toolTipSTYLE.left = x+'px';
    toolTipSTYLE.top  = (y-10)+'px';

    //if use left-top tooltip box
    //toolTipSTYLE.left = x+6;
    //toolTipSTYLE.top  = y+10;
    return true;
}


//----------------------------------------------------------------------------
// AJAX box window functions
//----------------------------------------------------------------------------

var requesters = new Array();
var requestersUsed = 0;

function loadDataForWindow(url, divID) {
	var requesterID = requestersUsed++;
	try {
    	requesters[requesterID] = new XMLHttpRequest();
    }
    catch (error) {
    	try {
        	requesters[requesterID] = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (error) {

            return;
        }
    }
    requesters[requesterID].open("GET", url, true);
    requesters[requesterID].send(null);
    requesters[requesterID].onreadystatechange = function() {
	    if (requesters[requesterID].readyState == 4) {
	        if (requesters[requesterID].status == 200) {
    	        document.getElementById(divID).innerHTML = requesters[requesterID].responseText;
        	} else {
	        	document.getElementById(divID).innerHTML = "ERROR loading data";
	        }
    	}
	    return true;
    }
}



//----------------------------------------------------------------------------
// end of AJAX box window functions
//----------------------------------------------------------------------------

var pa_lastItem = '';

function pa_obj(id) {
	ret = null;
	if (document.getElementById) {
		ret = document.getElementById(id);
	} else if (document.all) {
		ret = document.all[id];
	}
	return ret;
}

function pa_mover(name) {
	if(pa_obj(name+'_')) {
		pa_obj(name+'_').style.visibility = 'visible';
	}
}

function pa_mout(name) {
	if(pa_obj(name+'_')) {
		pa_obj(name+'_').style.visibility = 'hidden';
	}
}

function pa_mover0(name) {
    if(name.substr(4, 3) == 'mnu' && name.lastIndexOf('_') != name.length-1)
    {
        // display submenu
        if(pa_obj(pa_lastItem+'_')) {
            pa_obj(pa_lastItem+'_').style.visibility = 'hidden';
        }

        // change bg color of chosen menu
        if(pa_obj('top' + pa_lastItem)) {
            pa_obj('top' + pa_lastItem + 'L').className = 'headerTopMenuLeft';
            pa_obj('top' + pa_lastItem).className = 'headerTopMenu';
            pa_obj('top' + pa_lastItem + 'R').className = 'headerTopMenuRight';
        }

        if(pa_obj('top' + name)) {
            pa_obj('top' + name + 'L').className = 'headerTopMenuLeftActive';
            pa_obj('top' + name).className = 'headerTopMenuActive';
            pa_obj('top' + name + 'R').className = 'headerTopMenuRightActive';
        }

        // remember last item
        pa_lastItem = name;

        if(pa_obj(name+'_')) {
            pa_obj(name+'_').style.visibility = 'visible';
        }
    }
}

function pa_mout0(event) {
    if(pa_lastItem != '') {
        _pa_mout0(pa_lastItem, event);
    }
}

function _pa_mout0(name, event) {
    var name2 = name + '_';

    if(!event.toElement) {
       	try{
       		var toElId = event.relatedTarget.id;
       	}catch(e){
       		return;
       	}
    } else {
        var toElId = event.toElement.id;
    }

    if(toElId == name2 || toElId.substr(0, 4) == 'pam_' || toElId.substr(0, 7) == 'toppam_' || toElId == '') {
        return;
    }

	if(pa_obj(name+'_')) {
		pa_obj(name+'_').style.visibility = 'hidden';
	}

    // change bg color of leaven menu
    if(pa_obj('top' + name)) {
        //pa_obj('top' + name).className = 'headerTopMenu';
    }
}

function pa_mover1(event) {
    //alert(event.target.id);
    //return;
    if(!event.toElement) {
        var src = event.target.id;
    } else {
        var src = event.toElement.id;
    }
    //var src = event.target.id;
    //alert(src);
    //var src = event.toelement.id;
    //if(Math.random()<0.5)
    //    src = 'Company2';
    //else
    //    src = 'Company3';
    //if(src != 'Company2' && src != 'Company3')
    //    return;
    //alert(src);
    pa_mover0(src);
    //var anch = document.getElementById(event.toElement.id);
    //anch.attachEvent('onMouseOut', out1);
}

//----------------------------------------------------------------------------
// end top menu functions
//----------------------------------------------------------------------------
