var page_loaded = false;

//Swap Image and Text
function resizeImage() {
        var picdiv = document.getElementById("userpic");
        picdiv.innerHTML = '<img src="' + this.name + '">';

        if (this.width > '450')
                picdiv.firstChild.width = '450';
        return true;
}
function loadFailure() {
        var picdiv = document.getElementById("userpic");
        picdiv.innerHTML = '<strong>' + this.name + ' failed to load</strong>';
        return true;
}

function changeImg(url, desc){
        var picdesc = document.getElementById("picdesc");

        var myImage = new Image();
        myImage.name = url;
        myImage.onload = resizeImage;
        myImage.onerror = loadFailure;
        myImage.src = url;

        picdesc.innerHTML = desc;
}//End swap image and text

function storeCaret(textEl){
        if(textEl.createTextRange)
                textEl.caretPos = document.selection.createRange().duplicate();
}

function insertAtCaret(textEl, text){
        if(textEl.createTextRange && textEl.caretPos) {
                var caretPos = textEl.caretPos;
                caretPos.text =  caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?    text + ' ' : text;
        }else
                textEl.value  = text;
}

function smilie(text){
        text = " "+text+" ";
        if (document.message_form.message.createTextRange && document.message_form.message.caretPos) {
                var caretPos = document.message_form.message.caretPos;
                caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
                document.message_form.message.focus();
        } else {
                document.message_form.message.value  += text;
                document.message_form.message.focus();
        }
}

function txt_insert(text){
        text = " "+text+" ";
        if (document.message_form.message.createTextRange && document.message_form.message.caretPos) {
                var caretPos = document.message_form.message.caretPos;
                caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
                document.message_form.message.focus();
        } else {
                document.message_form.message.value  += text;
                document.message_form.message.focus();
        }
}

//Main Check Country
function checkCountry(form){
    if(form.country.value != 'US'){
        form.state.disabled = true;
        form.zipcode.disabled = true;
        form.zipcode.value = '(NA)';
        form.city.focus();
    }else{
        form.state.disabled = false;
        form.zipcode.disabled = false;
        form.zipcode.value = '';
    }
}

//Focus on password or email
function login(){
        form = document.login;
        if (form.email.value == ''){
                form.email.focus();
        }
        else
                form.passwd.focus();
}

//Check country for join
function checkCountry2(form){
        if(form.country.value != 'US'){
                form.zipcode.disabled = true;
                form.zipcode.value = '(NA)';
        }
        else{
                form.zipcode.disabled = false;
                form.zipcode.value = '';
        }
}

function breakOut(page){
    if (self != top)
        window.open(page ,"_top","");
}

function LTrim(str){
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1) {

            var j=0, i = s.length;
            while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
                j++;
            s = s.substring(j, i);
        }

        return s;
}

function RTrim(str){
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            var i = s.length - 1;
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;

            s = s.substring(0, i+1);
        }

        return s;
}

function Trim(str){
        return RTrim(LTrim(str));
}

//Single Field Check
function check_single(field, message){
        if(Trim(field.value) == ""){
                alert(message);
                field.focus();
                field.value = '';
                return false;
        }
        return true;
}

//Form Check
function check_form(){
        //tinyMCE.triggerSave(false,true);
        if(Trim(document.message_form.subject.value) == ""){
                alert("Please enter a Subject.");
                document.message_form.subject.focus();
                document.message_form.subject.value = '';
        return false;
        }
        else if(Trim(document.message_form.message.value) == ""){
                alert("Please enter a Message.");
                document.message_form.message.focus();
        return false;
        }
        return true;
}

//Disable Submit Button
function disableButton(theButton, value){
        theButton.value = value;
        theButton.disabled = true;
        return true;
}

//Upload form certify, then progress bar
function check(form, theButton, value){
        var form_tbl; //Table that contains form
        var progress_tbl; //Table that contains progress
        
        if(form.certify.checked == false){
                alert("You must certify you have rights to distribute the photos.");
                return false;
        }
        disableButton(theButton, value);
        
        form_tbl = document.getElementById("form_tbl");
        progress_tbl = document.getElementById("progress_tbl");
        
        form_tbl.style.display = "none"; //Hide Form
        progress_tbl.style.display = ""; //Show progress
        
        //Needed for IE annimation bug
        var t = setTimeout("document.getElementById('progress_img').src=document.getElementById('progress_img').src", 200); //Restart the img
}

//Make them confirm
function rusure(link, desc){
        question = confirm(desc)
        if (question !="0"){
                top.location = link
        }
}

function colorRow(E) {
        if(E.checked)
                (E.parentNode).parentNode.style.backgroundColor ="red";
        else
                (E.parentNode).parentNode.style.backgroundColor ="";
}

function togglechecked(form){
        for (var i = 0; i < form.elements.length; i++) {
                var E = form.elements[i];
                if ((E.disabled == false) && (E.name != 'cbxSelectAll') && (E.type == 'checkbox')) {
                        E.checked = form.cbxSelectAll.checked;
                }
                colorRow(E);
        }
}

function switchtabs(theCell, theSection){
        var theRow = theCell.parentNode; //The table row that contains the tabs
        var theSection = document.getElementById(theSection); //Table to show

        var tabs_container = document.getElementById('tabs_container'); //Container that has all the tables
        var theTables = tabs_container.getElementsByTagName('table');

        //Set current tab
        for(i=0 ; i< theRow.cells.length; i++){
                if (theRow.cells[i].className == "td4"){
                        theRow.cells[i].innerHTML = "<b>" + theRow.cells[i].title + "</b>";
                }
                theRow.cells[i].className = '';
        }

        //Turn off tables
        for (i=0; i < theTables.length; i++){
                theTables[i].style.display = "none";
        }

        theCell.innerHTML = "<strong>" + theCell.title + "</strong>";
        theCell.className = "td4";
        theSection.style.display = "";
}

function showhide(theShow, theHide){
        if(theShow){
                var theShow = document.getElementById(theShow); //Section to show
                theShow.style.display = "";
        }
        if(theHide){
                var theHide = document.getElementById(theHide); //Section to show
                theHide.style.display = "none";
        }
}

function inputTip(){
	var inputItems = document.getElementsByTagName('input');
	var blurClass = 'grey';
	
	for(var i=0; i < inputItems.length; i++){
     	if (inputItems[i].type == 'text' && inputItems[i].title != ''){ 
     		
     		if (inputItems[i].value == ''){
     			inputItems[i].className = blurClass;
				inputItems[i].value = inputItems[i].title;
			}
					
     		inputItems[i].onfocus = function (){
				if (this.value == this.title){
					this.value = '';
					this.className = '';
				}
			}//End onfocus
			
			inputItems[i].onblur = function (){
				if (this.value == ''){
					this.className = blurClass;
					this.value = this.title;
				}	
			}//End onblur
		}//End if
	}//End for
}//End function

function textLimit(box, limit){
	var limitBox = document.getElementById(box.name + "-limit");
	
	if (box.value.length > limit){
		box.value = box.value.substr(0,limit);
	}

	limitBox.innerHTML = (limit - box.value.length) + " characters left";	
}

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())
}//End Set Cookie

function getCookie(c_name){
        if (document.cookie.length>0){
                c_start=document.cookie.indexOf(c_name + "=")
                if (c_start!=-1){
                        c_start=c_start + c_name.length+1
                        c_end=document.cookie.indexOf(";",c_start)
                        if (c_end==-1)
                                c_end=document.cookie.length
                        return unescape(document.cookie.substring(c_start,c_end))
                }
        }
        return ""
}//End Get cookie

//Stuff to run once page is loaded.
function pageLoader() {
        page_loaded = true;
}

//Syntax
//addEvent(current[j],'mouseover',this.tipOver);
//addEvent(window, 'load', pageLoader);
function addEvent( obj, type, fn ) {
        if (obj.addEventListener) {
                obj.addEventListener( type, fn, false );
                EventCache.add(obj, type, fn);
        }
        else if (obj.attachEvent) {
                obj["e"+type+fn] = fn;
                obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
                obj.attachEvent( "on"+type, obj[type+fn] );
                EventCache.add(obj, type, fn);
        }
        else {
                obj["on"+type] = obj["e"+type+fn];
        }
}
var EventCache = function(){
        var listEvents = [];
        return {
                listEvents : listEvents,
                add : function(node, sEventName, fHandler){
                        listEvents.push(arguments);
                },
                flush : function(){
                        var i, item;
                        for(i = listEvents.length - 1; i >= 0; i = i - 1){
                                item = listEvents[i];
                                if(item[0].removeEventListener){
                                        item[0].removeEventListener(item[1], item[2], item[3]);
                                };
                                if(item[1].substring(0, 2) != "on"){
                                        item[1] = "on" + item[1];
                                };
                                if(item[0].detachEvent){
                                        item[0].detachEvent(item[1], item[2]);
                                };
                                item[0][item[1]] = null;
                        };
                }
        };
}();
addEvent(window, 'load', pageLoader);

