function toggleVisible(id, label)
{
    if(id != null)
    {
        var divel = document.getElementById(id);
        
        if(divel.style.display == "none")
        {
            divel.style.display = "block";        
            if (label != null)
				label.innerHTML = label.innerHTML.replace('+ Show', '- Hide');
        }
        else
        {
            divel.style.display = "none";
            if (label != null)
				label.innerHTML = label.innerHTML.replace('- Hide', '+ Show');
        }
    }
}


function toogleTickAll(id)
{
    if(id != null)
    {
        var FormName ="aspnetForm";
        var FieldName = "chklist"+id;
        var CheckValue = true;
       
        if(!document.forms[FormName])
		return;
		
		var theForm = document.forms[FormName];
		
	    var els = theForm.elements; 

	    for(i=0; i<els.length; i++){ 

	        switch(els[i].type){			
			    case "checkbox":
			       
                    if(els[i].id.substring(0,5) == "chk_"+id || els[i].id.substring(0,6) == "chk_"+id)
                    {
                        if(els[i].checked)
				            els[i].checked = false;
				        else
				            els[i].checked = true;
				    }

				    break;			
		        }
		    }

	    }
}

/*
	This function handles the expand/collapse functionality for the programmes section.
*/
function ShowHideProgrammes(imageElement, contentElement, maxHeight, networkPath)
{
	if (imageElement)
	{
		var img = document.getElementById(imageElement);
		
		if (img)
		{
			if (img.src.indexOf('btn-showprogrammes.gif') > 0)
			{
				img.src = '/images/' + networkPath + '/btn-hideprogrammes.gif';
				AnimateNextFrame(contentElement, -1, -1, 4, maxHeight, 500, 0, 'px', null);
			}
			else
			{
				img.src = '/images/' + networkPath + '/btn-showprogrammes.gif';
				AnimateNextFrame(contentElement, -1, -1, maxHeight, 4, 500, 0, 'px', null);
			}				
		}
	}
}


function show_loading() {
	// Create Loading div
	var div = setDiv();

	// Set initial position				
	set_loading_position();
    
    setInterval(moving_dots, 500);
    
    var t = setTimeout("hide_loading()", 5000);
	// Attach event handler to onScroll to keep in position
	//addEventHandler(window, 'scroll', set_loading_position);
			
				
}


function SetUpdateProgressPosition(controlId)
{
	var div = document.getElementById(controlId);
	
	if (div == null || div == '' || typeof(div) != 'object') 
	{
		return false; 
	}
	
	var scrollTop = f_scrollTop();
    var scrollLeft = f_scrollLeft();
    var clientWidth = f_clientWidth();
    var clientHeight = f_clientHeight();

    // Calculate left position
    var left = (clientWidth - (div.offsetWidth - 35)) / 2 ;
    left = scrollLeft + left;

    // Set position
    div.style.position = 'absolute';
    div.style.left = left + 'px';
    div.style.top = scrollTop + (clientHeight / 2) + 'px';
    
    div.style.zindex = '1000';
}

function set_loading_position() {
        var div = document.getElementById('processingDiv');
if (div == null || div == '' || typeof(div) != 'object') { return false; }

        var scrollTop = f_scrollTop();
        var scrollLeft = f_scrollLeft();
        var clientWidth = f_clientWidth();
        var clientHeight = f_clientHeight();

        // Calculate left position
        var left = (clientWidth - div.offsetWidth) /2 ;
        left = scrollLeft + left;

        // Set position
        div.style.position = 'relative';
        div.style.left = left + 'px';
        div.style.top = (clientHeight/2) + 'px';
        
          div = document.getElementById('processingDivOuter');
          div.style.position = 'absolute';
        div.style.zindex = '1000';
        div.style.left = '0px';
        div.style.top = '0px';
        div.style.width = document.documentElement.clientWidth+'px;';
        div.style.height = document.documentElement.clientHeight+'px;';
}

function f_clientWidth() {
        return document.documentElement.clientWidth;
}

function f_clientHeight() {
        return document.documentElement.clientHeight;
}

function f_viewportWidth() {
	var test1 = document.documentElement.scrollWidth;
	var test2 = document.documentElement.offsetWidth;
	
	if (test1 > test2) // all but Explorer Mac
		return document.documentElement.scrollWidth;
	else 
		return document.documentElement.offsetWidth;
}

function f_viewportHeight() {
	var test1 = document.documentElement.scrollHeight;
	var test2 = document.documentElement.offsetHeight;
	
	if (test1 > test2) // all but Explorer Mac
		return document.documentElement.scrollHeight;
	else 
		return document.documentElement.offsetHeight;
}

function f_scrollLeft() {
        return f_filterResults (
        window.pageXOffset ? window.pageXOffset : 0,
        document.documentElement ? document.documentElement.scrollLeft : 0,
        document.body ? document.body.scrollLeft : 0);
}

function f_scrollTop() {
        return f_filterResults (
        window.pageYOffset ? window.pageYOffset : 0,
        document.documentElement ? document.documentElement.scrollTop : 0,
        document.body ? document.body.scrollTop : 0);
}

function f_filterResults(n_win, n_docel, n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// Used on the Programmes/Commissions.aspx page to auto submit changes to commission fields...
function submitCommissionChange(obj)
{
	if (obj)
	{		
		var hidControl = document.getElementById('hidChangedControl');
		if (hidControl)
		{
			if (obj.id == 'create_new')
			{
				// Do some validation, make sure a name is specified...
				if (document.getElementById('name_new').value.length == 0)
				{
					alert('You must specify a name for this commission.');
					return;
				}
				
				hidControl.value = 'name_new';
			}
			else
				hidControl.value = obj.id;
				
			document.forms[0].submit();
		}
		else
			alert('error finding hidden control');
	}
	else
		alert('object not found');
}


//
// Used on the GetCreatives page to display the overlay with fullsize image and html tags etc...
//
function $(v) { return(document.getElementById(v)); }
function $S(v) { return($(v).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function isset(v) { return((typeof(v)=='undefined' || v.length==0)?false:true); }
function XYwin(v) 
{
	var z = agent('msie') ? 
		Array(document.body.clientHeight,document.body.clientWidth)
		: Array(window.innerHeight,window.innerWidth); 
		
	return(isset(v) ? z[v] : z); 
}

function popupTOG() 
{
	$S('popupContainer').display='none';
	$S('popupBox').display='none';
}


function popupBox(imageUrl, type, tagString, width, height) 
{
	var scrollTop = f_scrollTop();
    var scrollLeft = f_scrollLeft();
    var clientWidth = f_clientWidth();
    var clientHeight = f_clientHeight();
    var viewportWidth = f_viewportWidth();
    var viewportHeight = f_viewportHeight();
    
    // Add extra height and width to accomodate the tag textbox and close button etc...
    var totalWidth = width + 480;
    var totalHeight = height + 140;
    
	// Set the background height and position to cover all of the page
	$S('popupContainer').height= viewportHeight + 'px';
	$S('popupContainer').width= viewportWidth + 'px';
	$S('popupContainer').display = 'block';
	$S('popupContainer').top = '0';
	$S('popupContainer').left = '0';	
	
	if (type == 1)
	{
		$('imagePlaceholder').innerHTML = '<img src="' + imageUrl + '"\/>';
	}
	else if(type == 2)
	{
		$('imagePlaceholder').innerHTML = '<object classid="clsid:D27CDB6E-AE6D--11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" ">';
		$('imagePlaceholder').innerHTML += '<param name="wmode" value="transparent" /><param name="movie" value="' + imageUrl + '" /><param name="quality" value="high" />';
		$('imagePlaceholder').innerHTML += '<embed src="' + imageUrl + '" quality="high" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash"pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>';
	}
	else if(type == 3)
	{
	    $('imagePlaceholder').innerHTML = '<iframe frameborder="0" scrolling="no" width="' + width + '" height="' + height + '" style="float: none;" alt="" src="' + imageUrl + '"></iframe>' ;
	    
	}
	
	
	$('imagePlaceholder').innerHTML += '<br \/>';
	
	var newdiv = document.createElement("div");
	newdiv.setAttribute("id", "newDiv");	
    newdiv.innerHTML =  "<textarea rows='4' id='tagPlaceholder'>" + tagString + "</textarea>";
    var container = document.getElementById("containpop");
    var oldchild = document.getElementById("newDiv");
    if(oldchild!=null)
        container.replaceChild(newdiv,oldchild);	
    else
        container.appendChild(newdiv);	
	
	var top = scrollTop + Math.round((clientHeight-totalHeight)/2);
	if (top < 0)
		top = 0;
	$S('popupBox').top = top + 'px';
	$S('popupBox').left = Math.round((clientWidth-totalWidth)/2)+'px';
	$S('popupBox').width= totalWidth + 'px';
	$S('popupBox').height= totalHeight + 'px';
	$S('popupBox').display='block'; 
}

function SaveToClip(id)
{
    var text = document.getElementById(id).value;
    
    if (document.all) 
        window.clipboardData.setData("Text",text);            
    else            
        document.clipboard.saveEdit(text);            
}

String.prototype.contains = function(t) { return this.indexOf(t) >= 0 ? true : false }

String.prototype.pad = function(l, s, t){
	return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
		+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
		+ this + s.substr(0, l - t) : this;
}


function CheckDateRange(fromid, toid, check, ddl) {

    var from = document.getElementById(fromid);
    var to = document.getElementById(toid);

    var ddlControl = document.getElementById(ddl);
    if (ddlControl)
        ddlControl.options[ddlControl.options.length - 1].selected = true;

    var fromdate = from.value;
    var dateparts = fromdate.split("/");

    var newfromdate = new Date();
    newfromdate.setFullYear(dateparts[2], dateparts[1] - 1, dateparts[0]);

    var todate = to.value;
    dateparts = todate.split("/");

    var newtodate = new Date();
    newtodate.setFullYear(dateparts[2], dateparts[1] - 1, dateparts[0]);

    if (newfromdate > newtodate && check == "from") {
        var month = newfromdate.getMonth() + 1;
        var newmonth = "" + month + "";
        newmonth = newmonth.pad(2, "0", 0);
        var newday = "" + newfromdate.getDate() + "";
        newday = newday.pad(2, "0", 0);
        to.value = newday + '/' + newmonth + '/' + newfromdate.getFullYear();
    }
    else if (newtodate < newfromdate && check == "to") {
        var month = newtodate.getMonth() + 1;
        var newmonth = "" + month + "";
        newmonth = newmonth.pad(2, "0", 0);
        var newday = "" + newtodate.getDate() + "";
        newday = newday.pad(2, "0", 0);
        from.value = newday + '/' + newmonth + '/' + newtodate.getFullYear();
    }

}


function DateSelectorChange(ddl, fromDateId, toDateId) {
    if (ddl != null) {
        var dateFrom = document.getElementById(fromDateId);
        var dateTo = document.getElementById(toDateId);

        var newFromDate = new Date();

        var daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

        switch (ddl.options[ddl.selectedIndex].value) {
            case "today":
                {
                    var month = newFromDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + newFromDate.getDate() + "";
                    newday = newday.pad(2, "0", 0);

                    dateFrom.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();
                    dateTo.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();
                    break;
                }
            case "yesterday":
                {
                    today = new Date();
                    yesterday = new Date(today.valueOf() - 1000 * 60 * 60 * 24);

                    var newmonth = "" + (yesterday.getMonth() + 1) + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + yesterday.getDate() + "";
                    newday = newday.pad(2, "0", 0);

                    dateFrom.value = newday + '/' + newmonth + '/' + yesterday.getFullYear();
                    dateTo.value = newday + '/' + newmonth + '/' + yesterday.getFullYear();
                    break;
                }
            case "thisweek":
                {
                    var month = newFromDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + newFromDate.getDate() + "";
                    newday = newday.pad(2, "0", 0);
                    dateTo.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();

                    // Date From need to back track until we hit a previous monday
                    newFromDate.setDate(newFromDate.getDate() - (newFromDate.getDay() - 1));

                    var month = newFromDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + newFromDate.getDate() + "";
                    newday = newday.pad(2, "0", 0);
                    dateFrom.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();
                    break;
                }
            case "lastweek":
                {
                    newFromDate.setDate(newFromDate.getDate() - 7);
                    newFromDate.setDate(newFromDate.getDate() - (newFromDate.getDay() - 1));

                    var month = newFromDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + newFromDate.getDate() + "";
                    newday = newday.pad(2, "0", 0);
                    dateFrom.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();

                    newFromDate.setDate(newFromDate.getDate() + 6);

                    var month = newFromDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + newFromDate.getDate() + "";
                    newday = newday.pad(2, "0", 0);
                    dateTo.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();
                    break;
                }
            case "thismonth":
                {
                    var month = newFromDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var dd = new Date(newFromDate.getFullYear(), month, 0);

                    var newday = "" + dd.getDate() + "";
                    newday = newday.pad(2, "0", 0);

                    dateFrom.value = '01/' + newmonth + '/' + newFromDate.getFullYear();
                    dateTo.value = newday + '/' + newmonth + '/' + newFromDate.getFullYear();
                    break;
                }
            case "lastmonth":
                {
                    var toDate = new Date();

                    while (toDate.getMonth() == new Date().getMonth()) 
                    {
                        toDate.setDate(toDate.getDate() - 1);
                    }

                    var month = toDate.getMonth() + 1;
                    var newmonth = "" + month + "";
                    newmonth = newmonth.pad(2, "0", 0);

                    var newday = "" + toDate.getDate() + "";
                    newday = newday.pad(2, "0", 0);

                    dateFrom.value = '01/' + newmonth + '/' + toDate.getFullYear();
                    dateTo.value = newday + '/' + newmonth + '/' + toDate.getFullYear();
                    break;
                }
        }
    }
}