// Mootools dependent

// Prevents error if firebug console not available.
// @see http://getfirebug.com/console.html
if(!console){
    var console = new Object();
    console.log = function(){};
    console.info = function(){};
    console.warn = function(){};
    console.error = function(){};
    console.assert = function(){};
    console.dir = function(){};
    console.dirxml = function(){};
    console.trace = function(){};
    console.group = function(){};
    console.groupEnd = function(){};
    console.time = function(){};
    console.timeEnd = function(){};
    console.profile = function(){};
    console.profileEnd = function(){};
    console.count = function(){};
}

/**
* @desc select options jump down jumps
*/
function selectLand(ref, target)
    {
    lowtarget=target.toLowerCase();
    if (lowtarget=="_self") {window.location=loc;}
    else {if (lowtarget=="_top") {top.location=loc;}
    else {if (lowtarget=="_blank") {window.open(loc);}
    else {if (lowtarget=="_parent") {parent.location=loc;}
    else {parent.frames[target].location=loc;};
    }}}
}

/**
 *
 */
function selectJump(menu){
    ref=menu.actions.options[menu.actions.selectedIndex].value;
    splitc=ref.lastIndexOf("*");
    target="";
    if (splitc!=-1)
    {loc=ref.substring(0,splitc);
    target=ref.substring(splitc+1,1000);}
    else {loc=ref; target="_self";};
    if (ref != "") {selectLand(loc,target);}
}

/**
 * 
 */
function replace(string,text,by){
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

/**
 * For when we want to use fields
 */
function addDefaulterField(fieldClassName, defValue){
     $$(fieldClassName).each(function(ie){

        // Store defValue if case need to access again out out this function...
        ie.store('defValue', defValue);

        // Page loads so set up default states:
        var defCol = ie.getStyle('color');
        var col = '#333333';
        startValue = ie.get('value');

        ie.store('isPassword', ie.get('type') == 'password');



        if(startValue == ''){

            ie.set('value', defValue);
            ie.setStyle('color', col);

            //
            var isPass = ie.retrieve('isPassword');
            if(isPass){
                // @todo: Why not applying in IE? I have an answer but its
                // bad practice to swear in code comments, even about IE.
                ie.set('type', 'text');
            }
        }

        //
        ie.addEvent('blur', function(ev){

            // restore copy to default...
            if(ie.get('value') == ''){

                // if password box convert to ordinary text box to display
                // default value.
                var isPass = ie.retrieve('isPassword');
                if(isPass){
                    ie.set('type', 'text');
                }

                //
                ie.set('value', defValue);
                ie.setStyle('color', col);

            }

        });

        // links
        ie.addEvent('focus', function(ev){

            // clear copy...
            if(ie.get('value') == defValue){

                // check if was password box!
                // if so convert back to it.
                var isPass = ie.retrieve('isPassword');
                if(isPass){
                    ie.set('type', 'password');
                }

                //
                ie.set('value', '');
                ie.setStyle('color', defCol);

            }

        });


    });
}

/**
 * Delete confirmation messages.
 * cancelMessage example: "Not deleted." if it was being used as a delete
 * confirmation dialogue.
 */
function confirmIt(message, cancelMessage, acceptURL){
    var confirmed = confirm(message);
    if(!confirmed){
        alert(cancelMessage);
    } else {
        window.location = acceptURL;
    }
    return;
}



/**
* @desc hiddenInputID, hiddenInputValue are optional (used to carry an extra
* value if needed.)
*/
function inputWrite(
    inputID,
    anchorContainer,
    hiddenInputID,
    hiddenInputValue
){
    $(inputID).set('value', anchorContainer.get('html'));
    if((hiddenInputID != null) && (hiddenInputValue != null)){
    	$(hiddenInputID).set('value', hiddenInputValue);
    }
}

/**
 * 
 */
function simpleXMLToHash(rootXMLElem){
	var hash = new Hash();
	for(var i = 0; i < rootXMLElem.length; i++){
		var nodeElem = new Element(rootXMLElem.item(i));
	    hash.set(
            nodeElem.nodeName.toLowerCase(),
            nodeElem.textContent.toString()
        );
	}
	return hash;
}

/**
 * Get url param
 */
function getURLParam(name){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if(results == null) return '';
    return results[1];
}

/**
 * Fix buttons passing contents rather than values in in IE
 */
if(Browser){
    if(Browser.Engine.trident){ // if IE
        window.addEvent('domready', function(){
            $$('form button').each(function(el){
                
                if(el.get('type') == 'submit'){
                    el.addEvent('click', function(e){

                        var form = el.getParent('form');
                        var hElem = new Element('input', {
                            'type': 'hidden',
                            'name': el.get('name'),
                            'value': el.attributes["value"].value
                        });

                        if(form){
                            hElem.inject(form);
                        }
                    });
                }
            });
        });
    }
}

window.addEvent('domready',function() {
	
	
});

