/******************************************************************************
Copyright � 2008, Argo Computing Services
All rights reserved.
http://www.argocomputing.com.au
Redistribution and use in any form with or without modification,
are prohibited unless explicit permission is granted by
the Argo Computing Services Pty Ltd (Australia, ACN: 084560109).
******************************************************************************/


var argo = {};

var gOldForm0Target;
function submitInNewWindow()
{
    var aForm = document.forms[0];
    gOldForm0Target = aForm.target;
    aForm.target="_blank";

    setTimeout(
        function(){
            document.forms[0].target = gOldForm0Target;
        },
        200);

   return true;
}

function confirmSubmitInNewWindow(message)
{
    var r = confirm(message);

    if (r) {
        return submitInNewWindow();
    } else {
        return false;
    }
}

function GetSelectedCheckboxesCount(cbControl)
{
    var col = cbControl.all;
    if (col != null)
    {
        var selectedCount = 0;
        for (i = 0; i < col.length; i++) {
            if (col.item(i).tagName == "INPUT") {
                if ( col.item(i).checked ) {
                    selectedCount++;
                }
            }
        }
        return selectedCount;
    }
    return 0;
}


function CBL_Verify(val)
{
    var cbl = null;
    if (document.all)
        cbl = document.all[val.controltovalidate];
    else
        cbl = document.getElementById(val.controltovalidate);
    var count = GetSelectedCheckboxesCount(cbl);
    return count > 0;
}


function ToggleControlVisibility(id, doEnable)
{
   var el = document.getElementById(id);
    if (!el) {
        alert('Failed to toggle control visibility. Control not found: ' + id);
        return;
    }

   if (doEnable) {
      el.style.display = 'block';
   } else {
      el.style.display = 'none';
   }
}

function ToggleDiv(senderObject, id)
{
   var el = document.getElementById(id);
    if (!el) {
        alert('Failed to toggle control visibility. Control not found: ' + id);
        return;
    }

   if (el.style.display == 'none') {
      el.style.display = 'block';
        // Append Expanded to end
        if (!senderObject.className && (senderObject.className.indexOf('Expanded') != -1))
            senderObject.className += 'Expanded';
        //senderObject.className = 'toggleLinkExpanded';
   } else {
      el.style.display = 'none';
        // Remove Expanded from end
        senderObject.className = senderObject.className.replace('Expanded', '')
        //senderObject.className = 'toggleLink';
   }
}

function clientRedirect(url)
{
    window.location.href = url;
}


function PrepareTooltip(obj, accessible)
{
    //obj.T_OPACITY=90;
    if (accessible)
    {
        obj.T_STICKY=true;
        obj.T_CLICKCLOSE=true;
    }
    Tip();
}

function ResourceToTip(key)
{
    return Tip(RM.GetString(key));
}

String.format = function()
{
    if( arguments.length == 0 )
        return null;

    var str = arguments[0];
    for(var i=1;i<arguments.length;i++)
    {
        var re = new RegExp('\\{' + (i-1) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

/* Client-side access to querystring name=value pairs
   Version 1.2.3
   22 Jun 2005
   Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
   this.params = new Object()
   this.get=Querystring_get
   
   if (qs == null)
      qs=location.search.substring(1,location.search.length)

   if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
   qs = qs.replace(/\+/g, ' ')
   var args = qs.split('&') // parse out name/value pairs separated via &
   
// split out each name=value pair
   for (var i=0;i<args.length;i++) {
      var value;
      var pair = args[i].split('=')
      var name = unescape(pair[0])

      if (pair.length == 2)
         value = unescape(pair[1])
      else
         value = name;
      
      this.params[name] = value
   }
}
function Querystring_get(key, default_) {
   // This silly looking line changes UNDEFINED to NULL
   if (default_ == null) default_ = null;
   
   var value=this.params[key]
   if (value==null) value=default_;
   
   return value
}

function ReloadWindow(browserWindow)
{
    // oWnd.BrowserWindow.location.reload();
    if (!browserWindow)
        browserWindow = window;
    browserWindow.location = browserWindow.location;
}

function GetRadWindow()
{
   var oWindow = null;
   if (window.radWindow)
      oWindow = window.radWindow;
   else if (window.frameElement &&window.frameElement.radWindow)
      oWindow = window.frameElement.radWindow;
   return oWindow;
}
function CloseRadWindow(refreshParent)
{
    var oWnd = GetRadWindow();
    if (oWnd) {
        if (refreshParent)
            RefreshRadWindowParentPage();
        oWnd.Close();
    }
}

function RefreshRadWindowParentPage()
{
    var oWnd = GetRadWindow();
    if (oWnd) {
        ReloadWindow(oWnd.BrowserWindow);
    }
}

function disableElement(elem)
{
    elem.disabled=true;
}

function resize_iframe(id)
{
    // Src: http://guymal.com/mycode/100_percent_iframe/
	var height=window.innerWidth;//Firefox
	if (document.body.clientHeight)
	{
		height=document.body.clientHeight;//IE
	}
	//resize the iframe according to the size of the
	//window (all these should be on the same line)
    var fr = document.getElementById(id);
	fr.style.height=parseInt(height-
	fr.offsetTop-8)+"px";
}

function Attachment_ValidateMandatory(src, args, extParam)
{
    // extParam here is { ControlID: ID-XXX, FileID: ID-YYY}
    // Use GetRadUpload to get it by ID.
    if (extParam && extParam.ControlID) {
        var radUpload = GetRadUpload(extParam.ControlID);
        var currFile = document.getElementById(extParam.FileID);
        var fileName = radUpload.GetFileInputs();
        if (fileName.length == 1)
            fileName = fileName[0].value;
        else
            fileName = null;
        args.IsValid = (fileName && fileName.length > 0) || (currFile && currFile.innerHTML.length > 0);
    }
}

function Attachment_ValidateRadUpload(src, args, extParam)
{
    // Use GetRadUpload to get it by ID.
    if (extParam) {
        args.IsValid = GetRadUpload(extParam).ValidateExtensions();
    }
}

// CVH=CustomValidatorHelper
var CVH={
    createFunction: function(validationFunction, extParamData) {
        // Extend the default validation function by passing
        // 2 more parameters to it:
        // Default: (src, args) is goinf to become:
        //    (src, args, extParam)
        // So CustomValidator can be used this way:
        //
        // <ASP:CustomValidator clientvalidationfunction="MyValidatorX"></ASP:CustomValidator>
        //
        //   function VerifySomething(src, args, extParams) { ... }
        //   var MyValidatorX=CVH.createFunction(VerifySomething, 'SomeControlID');
        //
        // Or simpler:
        //   <ASP:CustomValidator clientvalidationfunction="CVH.createFunction(VerifySomething, 'SomeControlID')"></ASP:CustomValidator>
        // !!!!!NOTE!!!!!: clientvalidationfunction should be a string that DOESN'T ens with ';'.
        //                    This is just because of ugly ASP.NET concats string for eval:
        //                        eval(val.clientvalidationfunction + "(val, args) ;");

        var originalFunction = validationFunction;
        var extParam = extParamData;
        
        return function(src, args) {
            // Proxy the call...
            return originalFunction(src, args, extParam);
        }
    }
}
var CustomValidatorHelper=CVH;
argo.validation={
    checkMaxLength: function(el) {
        if (!el) return;
        var maxLen = el.getAttribute("maxlength");
        if (!maxLen) return;
        var curLen = el.value.length;
        if (curLen > maxLen)
            el.value = el.value.substring(0, maxLen);
    }
}

argo.filterElements=function(options){
	var doc = options.document || document;
	var els = [];
	var attrs = options.attributes || {}; // {at1: val1, at2: val2, et3: etc4}
	var checkAttributes=function(e) {
		for (var key in attrs) {
			var filterVal = attrs[key];
			if (filterVal != e.getAttribute(key))
				return false;			
		};
		return true;
	};
	
	for (var i=0; i<options.tags.length; i++){
		var tagElements = doc.getElementsByTagName(options.tags[i]);
		// Filter on attributes
		for (var j=0; j<tagElements.length; j++){
			if (checkAttributes(tagElements[j])) {
				els.push(tagElements[j]);
			}
		}		
	}
	return els;
};

argo.getElementsByTagAndName=function(tag, name) {
    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for(i = 0, iarr = 0; i < elem.length; i++) {
        att = elem[i].getAttribute("name");
        if(att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}
argo.isIE = window.ActiveXObject ? true : false;

argo.getElementsByName=function(name, cfg){
	// IE problem:
	// http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2020889&SiteID=1
	// http://tinymce.moxiecode.com/punbb/viewtopic.php?pid=45462
	// http://webbugtrack.blogspot.com/2007/08/bug-411-getelementsbyname-doesnt-work.html
	if (!cfg) {
		var cfg = {};
	};
	var doc = cfg.doc || document;
	if (argo.isIE){
	    var temp = doc.all;		
	    var matches = [];
		if (!temp)
			return matches;
		if (temp.length == undefined)
			temp = [temp]; // One element returned, convert it to the array
	    for(var i=0;i<temp.length;i++){
	      if(temp[i].name == name){
	        matches.push(temp[i]);
	      }
	    }
	    return matches;		
	} else 
		return doc.getElementsByName(name);
}