﻿function Delegate() {}
Delegate.prototype.func = null;
Delegate.create = function(obj, func)
{
	var f = function()
	{
		var target = arguments.callee.target;
		var func = arguments.callee.func;

		return func.apply(target, arguments);
	};

	f.target = obj;
	f.func = func;

	return f;
}

function Utils() {}
Utils.getForm = function()
{
    if (document.forms.length > 0)
    {
        return document.forms[0];
    }
    return null;
}

Utils.createSubmitHidden = function(form, controlName, submitValue)
{
    //MZ - Remove any existing hidden fields with the same name to make sure correct event handler is called when the form is submitted
    if (document.getElementById(controlName))
    {
        var obj = document.getElementById(controlName);
        obj.parentNode.removeChild(obj);
    }
    
    var hidden = document.createElement("input");
    
    hidden.type = "hidden";
    hidden.id = controlName;
    hidden.name = controlName;     
    hidden.value = submitValue;
    
    form.appendChild(hidden);
}

Utils.getNode = function(obj, nodeName)
{
   var result = null;

   for (var x = 0; x < obj.attributes.length; x++)
   {
        if (obj.attributes[x].nodeName == nodeName)
        {
            result = obj.attributes[x].nodeValue;
            break;
        }
   }
   
   return result;
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+ expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString() + "; path=/");
}

// all we want to know is if the cookie exists - not interested in the value
function readCookie(c_name) 
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == c_name )
		{
			b_cookie_found = true;
			
			// all we want to know
			break;
		}
		
		a_temp_cookie = null;
		cookie_name = '';
	}
	
	return b_cookie_found;
}


