/*
	(c) Copyright Mobile Bridges Ltd 2005. All Rights Reserved.

	Project: Project independent
	Version: 1.0
	Date:    04-03-2006

	Description: Eventhandler for ActionForm, generated by Code Generator
*/

function OnFilter()
{
	OnAction("Filter");
}

function OnSelect()
{
	OnAction("Select");
}

function OnEdit()
{
	OnAction("Load");
}

function OnInsert()
{
	OnAction("Add");
}

function OnRemove(inConfirmation)
{
	var isConfirmed = true;

	if (inConfirmation.length)
		isConfirmed = confirm(inConfirmation);

	if (isConfirmed)
		OnAction("Remove");
}

function OnCancel()
{
	OnAction("Load");
}

function OnSave()
{
	OnAction("Save");
}

function OnAction(inAction)
{
	var theForm = document.getElementById("Form");
	theForm.theAction.value = inAction;
    theForm.submit();
}

function OnBack(inPage, inAction)
{
	var theForm = document.getElementById("Form");

	theForm.Page.value = inPage;
	theForm.theAction.value = inAction;

	theForm.action = "";
	theForm.submit();
}

function OnConfirm(inConfirmation, inPage, inAction)
{
	var isConfirmed = true;

	if (inConfirmation.length)
		isConfirmed = confirm(inConfirmation);

	if (isConfirmed)
	{
		var theForm = document.getElementById("Form");

		theForm.Page.value = inPage;
    	theForm.theAction.value = inAction;

		theForm.action = "";
    	theForm.submit();
	}
}

function RestoreSelectBox(inName, inValue)
{
	var theSelectBox = document.getElementById(inName);
	if (theSelectBox)
	{
    	var theOptions = theSelectBox.options;
		for (i=0;i<theOptions.length;i++)
		{
            if (theOptions[i].value == inValue)
			{
				theOptions[i].selected = true;
				return true;
			}
		}
	}
}

function RestoreCheckBox(inName)
{
	var theCheckBox = document.getElementById(inName);
	if (theCheckBox)
		theCheckBox.checked = !theCheckBox.checked;
}

function RestoreRadioButtonGroup(inName, inValue)
{
	var theChildNodes = document.getElementsByTagName("input");
	for (i=0;i<theChildNodes.length;i++)
	{
		var theChildNode = theChildNodes.item(i);
		if (theChildNode.getAttribute("id") == inName) {
			theChildNode.checked = true;
			return;
		}
	}
}

function serialize( inp ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
 
    var getType = function( inp ) {
        var type = typeof inp, match;
        if(type == 'object' && !inp)
        {
            return 'null';
        }
        if (type == "object") {
            if(!inp.constructor)
            {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
 
    var type = getType(inp);
    var val;
    switch (type) {
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (inp ? "1" : "0");
            break;
        case "number":
            val = (Math.round(inp) == inp ? "i" : "d") + ":" + inp;
            break;
        case "string":
            val = "s:" + inp.length + ":\"" + inp + "\"";
            break;
        case "array":
            val = "a";
        case "object":
            if (type == "object") {
                var objname = inp.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            var count = 0;
            var vals = "";
            var okey;
            for (key in inp) {
            	if (Math.round(key) == key) {
                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(inp[key]);
                count++;
				}
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}

if(typeof(MooTools) != "undefined" ) {

	function hideException() {
		document.id('exception').toggleClass("StyledExceptionHidden");
	}

	window.addEvent( 'domready', function(){
		
		var theMessage = document.getElementById('FormTextAreaElement');
		if(theMessage){
			theMessage.onkeyup = function(e) {
				var lEvent = window.event || e;
				if(theMessage.value.length > 160) {	
					theMessage.value = theMessage.value.substring(0, 160);
					var objControl = theMessage;
					objControl.scrollTop = objControl.scrollHeight;
				}
			}
		}
		
		var theTextareaElement = document.id('Input_Message_Overlay');
		if(theTextareaElement) {
			if (typeof theTextareaElement.onselectstart!="undefined") { 				// IE
				theTextareaElement.onselectstart = function(){return false};
			} else if (typeof theTextareaElement.style.MozUserSelect!="undefined") {	// FF
				theTextareaElement.style.MozUserSelect = "none";
			} else {																	// OTHER
				theTextareaElement.onmousedown=function(){return false}
				theTextareaElement.style.cursor = "default";
			}
		}
		
	} );

	function OnMessageClick(inElement, inOverlay) {
		
		if(document.id(inOverlay).getStyle('display') == "block") {
			document.id(inOverlay).setStyle('display','none');
			document.id(inOverlay).getPrevious('textarea').focus();
		} else {
			if((inElement.value.length < 1) && (inElement.nodeName != "TEXTAREA")) {
				document.id(inOverlay).setStyle('display','block');
			}
		}
	}

	function OnMessageBlur(inElement, inOverlay) {
		if((inElement.value.length < 1)) {
			document.id(inOverlay).setStyle('display','block');
		}
	}

}
