//This javascript file contains functions commonly used throughout the site

// sendOpenerTo()
// safe open page in parent, checks if opener exits
function sendOpenerTo(mypage) {
    if (typeof(opener)=="object") {
        if (opener.closed) {
            var winparent = window.open(mypage,'parentwindow','');
   	    } else {
            //opener.location.href = mypage;
            //break out of frames
            opener.top.location.href = mypage;
        }
    } else {
        var winparent = window.open(mypage,'parentwindow','');
    }
    window.close();
}

function sendTopTo(myURL) {
    top.location.href = myURL;
}

// Match drop down box to selected value
// setSelect(this,value)
function setSelect(theObject,a) {
	for (i=0;i<theObject.options.length;i++) {
		if (theObject.options[i].value==a) {
			theObject.selectedIndex=i;
			break;
		}
	}
}

// toggle Checkbox on/off by clicking adjacent text
// useage toggleCheckbox( formname a , elementname b )
function toggleCheckbox(a,b) {
    if (document.forms[a].elements[b].checked==false) {
        document.forms[a].elements[b].checked = true;
    } else {
        document.forms[a].elements[b].checked = false;
    }
}

// toggle to radio button by clicking adjacent text
// usage toggleRadio( formname a, elementname b, button_order c)
function toggleRadio(a,b,c) {
    //alert(document.forms[a].elements[b][c].checked);
    if (document.forms[a].elements[b][c].checked==false) {
        document.forms[a].elements[b][c].checked=true;
    }
}

function checkforRadio(theForm,q) {
    for (i=0;i<theForm.elements[q].length;i++) {
        if (theForm.elements[q][i].checked==true) {
            //alert(theForm.elements[q][i].value);
            return true;
            break;
        }
    }
    return false;
}

function checkMaxSelect (theForm,theBox,boxOrder,maxCks) {
    //alert(theForm.elements[theBox].length);
    tcnt=0;
    
    for (i=0; i<theForm.elements[theBox.name].length; i++) { 
        if (theForm.elements[theBox.name][i].checked==true) {
            tcnt++;
        }
        if (tcnt > maxCks) {
            theForm.elements[theBox.name][boxOrder].checked=false;
            alert("Please select only " + maxCks + " items.");
            break;
        }
    }
}

// check for valid email
function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return true;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}


// appendOnload(function aFunct);
// preserves existing window.onload handlers if present
// aFunct is function name refernce, or an anonymous function
// usage appendOnload(myNewFunction);  or   appendOnload(function(){dosomething();});
function appendOnload(aFunct) {
    var oldOnload = (typeof(window.onload)=="function") ? window.onload : function() {};
    window.onload = function() {
        oldOnload();
        aFunct(); 
    }
}

// doNothing function to replace <a href="#">
// use <a href="javascript:doNothing();"> instead
function doNothing() {
    void(0);
}

// randomTools 
var randomTools = {
	x : "randomTools",
	randElement : function (elBase, max) {
		var tVal = Math.floor((Math.random() * max) + 1);
		var tEl = document.getElementById(elBase + "_" + tVal);
		if (tEl != null) tEl.style.display = "block";
		for (var i=1;i<=max;i++) {
			var lEl = document.getElementById(elBase + "_" + i);
			if (lEl != null && i !=  tVal) lEl.innerHTML = "";
		}
	}
};