
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

var aOnloadFunctions = [];

function load()
{
    aOnloadFunctions.each(function(onloadFunction){
        onloadFunction.call();
    });

}

function getMessage(id,title,text,message){
    var sHtml = message.replace('{text}',text);
    sHtml = sHtml.replace('{title}',title);
    sHtml = sHtml.replace('{id}',id);

    return sHtml;
}

var mouseX;
var mouseY;

function getMousePos(e) {

    if (document.layers||document.getElementById&&!document.all) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    //IE
    else if (document.all) {
        mouseX = window.event.clientX + document.documentElement.scrollLeft;
        mouseY = window.event.clientY + document.documentElement.scrollTop;
    }
}

function startMouseListening(){
    Event.observe(document,'mousemove',getMousePos);
}

function showLogin(sMessage,userName){
    
    new Ajax.Request('/ajax/login.ajax.php',{
        method:'get',
        parameters:{url:window.location,message:sMessage,user_name:userName},
        onSuccess:function(transport){
   
            PopupWindow.show('login',{
                html:transport.responseText,
                overlay:true,
                overlayEffect:'fade',
                onFinish:function(){
                    $('user_name').focus();
                }
            });
      
        },
        onFailure:function(transport){
            alert('Login failed to load');
        }
    });
}

function validateLogin(oForm){
    var message = '';
    var separator = '';

    if (oForm.user_name.value == ''){

        setInputError(oForm.user_name,message);
        message += separator + 'Please enter an email address';
        separator = '<br />';
        
    }
    else resetInputError(oForm.user_name);

    if (oForm.password.value == ''){

        setInputError(oForm.password,message);
        message += separator + 'Please enter a password';
        
    }
    else resetInputError(oForm.password);

    if (message != ''){
        $('loginMessage').update(message);
        $('loginMessage').show();
        return false;
    }
    return true;
}

function showForgotYourPassword(sMessage,emailAddress){

    new Ajax.Request('/ajax/forgot_your_password.ajax.php',{
        method:'get',
        parameters:{url:window.location,message:sMessage,user_name:emailAddress},
        onSuccess:function(transport){

            PopupWindow.show('forgot',{
                html:transport.responseText,
                overlay:true,
                overlayEffect:'fade',
                onFinish:function(){
                    $('email_address').focus();
                }
            });

        },
        onFailure:function(transport){
            alert('Forgot your password failed to load');
        }
    });
}

function validateForgotYourPassword(oForm){
    var message = '';
    var separator = '';

    if (oForm.email_address.value == ''){

        setInputError(oForm.email_address,message);
        message += separator + 'Please enter an email address';
        separator = '<br />';
        
    }
    else resetInputError(oForm.email_address);

    if (message != ''){
        $('forgotMessage').update(message);
        $('forgotMessage').show();
        return false;
    }
    return true;
}
/*
 * Set text input to nothing if it is
 * still set to default text
 */
function blankInputText(field,value){
    if (field.value == value){
        field.value = '';
    }
}

/*
 * Set text input to default value if it
 * is empty
 */
function resetInputText(field,value){
    if (field.value == ''){
        field.value = value;
    }
}

var aFieldsToBlank = [];
/*
 * Set fields with default values
 * to empty before the form is submited
 */
function blankDefaultValues(){
    
    aFieldsToBlank.each(function(field){
       if (field.input.value == field.defaultValue){
        field.input.value = '';
       }
    });
}

function resetDefaultValues(){
    aFieldsToBlank.each(function(field){
       if (field.input.value == ''){
        field.input.value = field.defaultValue;
       }
    });
}

/*
 * Sets the class on an input
 * when validation error
 */
function setInputError(input,message){
    if (input.className){
        input.className += ' error';
        
    }
    else {
        input.className = ' error';
    }

    if (message.length == 0){
        input.focus();
        input.select();
    }
}

function resetInputError(input){
    if (input.className){
        input.className = input.className.replace(' error','');
    }
}

function transparentCaptions(cssClass, opacity){
	
	$$('.' + cssClass).each(function(caption){
		var text = caption.innerHTML;
		caption.className = 'bg';
		
		var inner = new Element('div', {
		    className : cssClass	
		});
		
		inner.className = cssClass;
		
		inner.update(text);
		caption.setOpacity(opacity);
		caption.style.background = '#657542';
		caption.update(inner);
	});
}

function calculateScrollableHeight(title, titleHeight, content, contentHeight){
	
	var currentTitleHeight = ($(title).innerHTML != '') ? $(title).getHeight() : 0;
	
	if (currentTitleHeight > titleHeight){
		$(content).style.height = (contentHeight - currentTitleHeight + titleHeight) + 'px';
	}
	
}

function fixIEForm(formID){
	
	if (Prototype.Browser.IE6 || Prototype.Browser.IE7){
		
		var table = '<table cellpadding="0" cellspacing="0" border="0">';
		
		$$('#' + formID + ' div').each(function(div){
			
			if (div.className == 'label'){
				table += '<tr><td class="label">' + div.innerHTML + '</td>';
			}
			
			if (div.className == 'input'){
				table += '<td>' + div.innerHTML + '</td></tr>';
			}
		});
		
		table += '</table>';
		
		$(formID).update(table);
	}		
}
