﻿function Dbg(str)
{
    var dbgEl = el('dbgdiv');
    if (dbgEl == null)
    {
        var dbgEl = document.createElement('DIV');
        dbgEl.id = 'dbgdiv';
        dbgEl.style.position = 'fixed';
        dbgEl.style.width = '600px';
        dbgEl.style.height = 'auto';
        dbgEl.style.top = '20px';
        dbgEl.style.left = '20px';
        dbgEl.style.filter = 'alpha(opacity=50)';
        dbgEl.style.opacity = '0.50';
        dbgEl.style.border = '1px solid black';
        dbgEl.style.backgroundColor = '#DDDDDD';

        document.body.appendChild(dbgEl);
        //document.write('<div id=\'dbgdiv\' style=\'position:absolute;width: 300px; height: auto;top: 20px; right: 20px; filter:alpha(opacity=50);opacity=0.5;border: 1px solid black; background-color: #DDDDDD;\'></div>');
        //dbgEl = el('dbgdiv');
    }
    
    var newInfo = document.createElement('DIV');
    newInfo.style.position = 'relative';
    newInfo.style.clear = 'both';
    newInfo.innerHTML = str;
    dbgEl.appendChild(newInfo);
    dbgEl.appendChild(document.createElement('HR'));
}

function LinkChecker() { }
LinkChecker.RefreshTimeoutPeriod = 1000;
LinkChecker.Start = function(type)
{
    el('linkchecker_results').LoadComplete = null;
    XibAjax.LoadHtml('linkchecker_results', null, 'linkchecker_ajax.aspx?method=Start&type=' + type + '&t=' + new Date().getTime());
    activateblock(el('button_LinkChecker_ResultsOuter'), true);
}

LinkChecker.Refresh = function(waitForIt)
{
    //Dbg('Refresh');
    if (waitForIt)
    {
        LinkChecker.RefreshTimeout = window.setTimeout('LinkChecker.Refresh(false);', LinkChecker.RefreshTimeoutPeriod);
    }
    else
    {
        el('linkchecker_results').LoadComplete = null;
        XibAjax.LoadHtml('linkchecker_results', null, 'linkchecker_ajax.aspx?method=Refresh' + '&t=' + new Date().getTime());
    }
}

AjaxTable.HandleCheckboxClick = function(tableId, idClicked, description, newValue, postMethod){

	var oTable = AjaxTable.GetTable(tableId);
	oTable.SelectedIdList.AddOrRemoveItem(idClicked, newValue, description);
	
	var checked = true;
	
	if(oTable.SelectedIdList.PickerType == "Single"){
		//Un-check the other checkboxes.
		var ArrCheckboxes = oTable.getElementsByTagName("input");
		var chkPrefix = tableId + "_chkbox_";
		for(var i=0; i<ArrCheckboxes.length; i++){
			var oItem = ArrCheckboxes[i];
			if(oItem.id.indexOf(chkPrefix) == 0){
				var id = oItem.id.substring(chkPrefix.length);
				if(id != idClicked + "") {
				    oItem.checked = false;
				} else {
				    checked = oItem.checked;
				}
			}
		}
		XibAjax.CloseControl(XibAjax.LastControlOpenned);
	}	
	if (postMethod)
	    postMethod(tableId, idClicked, checked);
}

AjaxTable.CreateAutoCheckbox = function(oTable, jsonCode, id, description, postMethod){
	var onchange=" window.top.document.getElementById('" + oTable.id + "').SelectedIdList.AddOrRemoveItem(" + id + ",this.checked,'" + XibAjax.JavaScriptEncode(description) + "'); ";
	var onchange="AjaxTable.HandleCheckboxClick('" + oTable.id + "'," + id + ",'" + XibAjax.JavaScriptEncode(description) + "',this.checked" + (postMethod?"," + postMethod:"") + "); ";
	
	
	return("<input type=checkbox onclick=\"" + onchange + "\" id='" + oTable.id + "_chkbox_" + id + "' onfocus='this.parentNode.parentNode.style.background=\"#D1D9E7\"' onblur='this.parentNode.parentNode.style.background=\"\"'/>");
}

function SearchAffiliate_UpdateTotal(uid)
{
    //Let's try and refresh the total at the bottom of the page...
    var oTable = AjaxTable.GetTable(uid);
    var qs = AjaxTable.CreateQueryString(oTable);
    
    var totalEl = el('searchAffiliateTotal_' + uid);
    totalEl.LoadComplete = null;
    
    XibAjax.LoadHtml('searchAffiliateTotal_' + uid, null, '/admin/AJAX/AffiliateSearchTotal/GetTotal.aspx?' + qs, true);

}

AjaxTable.UpdatePagingInfo = function(oTable){
	
	var PageNavigationDiv = window.top.document.getElementById(oTable.id + "_RecordNavigation");
	var DisplayPageNavigationDiv = AjaxTable.NoOfPages(oTable) > 1;
	if((oTable.DisplayPageNavigationDiv == null)||(DisplayPageNavigationDiv)) oTable.DisplayPageNavigationDiv = DisplayPageNavigationDiv;
	
	//PageNavigationDiv.style.display = oTable.DisplayPageNavigationDiv ? "block" : "none";
	
	var NoRecordsFoundDiv = window.top.document.getElementById(oTable.id + "_NoRecordsFound");
	NoRecordsFoundDiv.style.display = (oTable.NumberOfRecords == 0) ? "block" : "none";
	
	var pageString = "";
	
	var start = 0;
	var end = 0;
	
	var sections = 7;
	var secSmall = Math.floor(sections/2);
	var secLarge = secSmall+1;
	
	if (AjaxTable.NoOfPages(oTable)<sections+1) {
		start = 0;
		end = AjaxTable.NoOfPages(oTable);
	} else {
		start = (oTable.CurrentPage<secLarge) ? 0 : ( (oTable.CurrentPage + secLarge>AjaxTable.NoOfPages(oTable)) ? AjaxTable.NoOfPages(oTable) - sections : (oTable.CurrentPage - secSmall) );
		end = (oTable.CurrentPage + secLarge>AjaxTable.NoOfPages(oTable)) ? AjaxTable.NoOfPages(oTable) : (oTable.CurrentPage<secLarge ? sections : oTable.CurrentPage + secLarge);
	}
	for (var i=start;i<end;i++) {
		if (i==oTable.CurrentPage) {
			pageString+='<strong>' + (i+1) + '</strong> ';
		} else {
			pageString+='<a href="Javascript:AjaxTable.SetPage(\'' + oTable.id + '\',' + i + ')">' + (i+1) + '</a> ';
		}
	}
	
	XibAjax.DisplayInfo(oTable.id + "_PageLinkSpan", pageString);
	
	XibAjax.DisplayInfo(oTable.id + "_NumberOfRecordsDisplay", oTable.NumberOfRecords);
	XibAjax.DisplayInfo(oTable.id + "_CurrentPageDisplay", oTable.CurrentPage + 1);
	XibAjax.DisplayInfo(oTable.id + "_TotalPages", AjaxTable.NoOfPages(oTable));
	XibAjax.DisplayInfo(oTable.id + "_ShowingRecordFrom", AjaxTable.ShowingRecordFrom(oTable));
	XibAjax.DisplayInfo(oTable.id + "_ShowingRecordTo", AjaxTable.ShowingRecordTo(oTable));
	
	var doSearch = true;
	var index = 2;
	while (doSearch)
	{
	    var elementFound = false;
	    
	    if (el(oTable.id + "_NumberOfRecordsDisplay" + index) != null)
	    {
	        XibAjax.DisplayInfo(oTable.id + "_NumberOfRecordsDisplay" + index, oTable.NumberOfRecords);
	        elementFound = true;
	    }

	    if (el(oTable.id + "_CurrentPageDisplay" + index) != null)
	    {
	        XibAjax.DisplayInfo(oTable.id + "_CurrentPageDisplay" + index, oTable.CurrentPage + 1);
	        elementFound = true;
	    }
	    
	    if (el(oTable.id + "_TotalPages" + index) != null)
	    {
	        XibAjax.DisplayInfo(oTable.id + "_TotalPages" + index, AjaxTable.NoOfPages(oTable));
	        elementFound = true;
	    }
	    
	    if (el(oTable.id + "_ShowingRecordFrom" + index) != null)
	    {
	        XibAjax.DisplayInfo(oTable.id + "_ShowingRecordFrom" + index, AjaxTable.ShowingRecordFrom(oTable));
	        elementFound = true;
	    }
	    
	    if (el(oTable.id + "_ShowingRecordTo" + index) != null)
	    {
	        XibAjax.DisplayInfo(oTable.id + "_ShowingRecordTo" + index, AjaxTable.ShowingRecordTo(oTable));
	        elementFound = true;
	    }

	    if (el(oTable.id + "_PageLinkSpan" + index) != null)
	    {	    
	        XibAjax.DisplayInfo(oTable.id + "_PageLinkSpan" + index, pageString);
	        elementFound = true;
	    }
	    
	    ++index;
	    
	    if (!elementFound)
	    {
	        doSearch = false;
	    }
	}
}

XibAjax.GetValue = function(url, func, Params)
{
	var oAjaxRequest = new AjaxRequest();
	oAjaxRequest.XmlRequest.open("GET", XibAjax.CleanUrl(url), true);
	oAjaxRequest.Function = func;
    oAjaxRequest.PassedParameters = Params;
	oAjaxRequest.oncomplete = "XibAjax.GetValue_finalise";
	oAjaxRequest.XmlRequest.send(null);
	IncrementProcessCounter();
}

XibAjax.GetValue_finalise = function(oAjaxRequest){
	var val = eval(oAjaxRequest.XmlRequest.responseText);
	
	if (typeof(oAjaxRequest.Function) == 'object' ||
	    typeof(oAjaxRequest.Function) == 'function')
	{
	    oAjaxRequest.Function(val, oAjaxRequest.PassedParameters);
	}
	
	DecrementProcessCounter();
}


/*
	This handles the onclick event of the checkboxes in 
	an object picker.
*/
AjaxTable.HandleCheckboxClick = function(tableId, idClicked, description, newValue, postMethod){
	var oTable = AjaxTable.GetTable(tableId);
	oTable.SelectedIdList.AddOrRemoveItem(idClicked, newValue, description);
	
	var checked = true;
	
	if (typeof(oTable.OnSelectedValueChanged) == 'function' ||
		    typeof(oTable.OnSelectedValueChanged) == 'object')
	{
	    oTable.OnSelectedValueChanged();
	}
	
	if(oTable.SelectedIdList.PickerType == "Single"){
		//Un-check the other checkboxes.
		var ArrCheckboxes = oTable.getElementsByTagName("input");
		var chkPrefix = tableId + "_chkbox_";
		for(var i=0; i<ArrCheckboxes.length; i++){
			var oItem = ArrCheckboxes[i];
			if(oItem.id.indexOf(chkPrefix) == 0){
				var id = oItem.id.substring(chkPrefix.length);
				if(id != idClicked + "") oItem.checked = false;
			}
		}
		XibAjax.CloseControl(XibAjax.LastControlOpenned);
	}	
	if (postMethod)
	    postMethod(tableId, idClicked, newValue);
}


AjaxTable.GetSelectedId = function(parentTableId)
{
    var selectedId = 0;
    var oParentTable = AjaxTable.GetTable(parentTableId);
    if (oParentTable != null)
    {
        selectedId = oParentTable.SelectedIdList.Selected();
    }
    //Dbg('Selected Id:' + selectedId);
    return selectedId;
}

IdList.prototype.Selected = function()
{
    if(this.PickerType == "Single")
    {
        if (this.ItemsToAdd.length == 0)
        {
            return 0;
        }
        else
        {
            return this.ItemsToAdd[0].id;
        }
    }
    else
    {
        return this.ItemsToAdd;
    }
}