// ---------------------------------------------------------------------
//                             onLoad Handler
// ---------------------------------------------------------------------
var old = window.onload; // catch any existing onload calls 1st
window.onload = function () {
  if (old) { // execute existing onloads
    old();
  };
  for (var ii = 0; arguments.callee.actions.length > ii; ii++) {
    arguments.callee.actions[ii]();
  };
};
window.onload.actions = [];

// ---------------------------------------------------------------------
//                             fixLinks
// ---------------------------------------------------------------------
function fixLinks() {
  if (!document.getElementsByTagName) return null;
  var server = document.location.hostname;
  var anchors = document.getElementsByTagName("a");
  var id, href, title;
  for(var i=0; i < anchors.length; i++){
    if(!anchors[i].href) continue;
    href = anchors[i].href;
    title = anchors[i].title;
    id = anchors[i].id;
    if(href.indexOf(server) == -1){ // Href is not a file on my server
      if(href.indexOf("javascript:") == -1){ // Href is not a javascript call
        if(!anchors[i].onclick){ // Href does not have an onclick event
          if(href.indexOf("mailto:") == -1){ // Href is not a mailto:
            if (((href.indexOf("http://") != -1) ||
                 (href.indexOf("https://") != -1)) &&
                 href.indexOf('ctsavesenergy.org') == -1 ){ // Href is not relative (for Safari)
                                                            // or pointing to our server (from Atomz)
              anchors[i].setAttribute("target","_blank");
              anchors[i].setAttribute("title",title + " [This Link Will Open in a New Window]");
            }
          }
        }
      }
    }
  }
  return null;
}
window.onload.actions.push(fixLinks);

function clearEmail(which){
	if (which.value == 'Your Email Address'){
		which.value = '';
	}
	which.onblur = function(){
		if (this.value == ''){
			this.value = 'Your Email Address';
		}
	}
}

function checkVote(whichForm){
	voteSelected = false;
	for (i=0;i<whichForm.rating.length;i++){
		if (whichForm.rating[i].checked) voteSelected = true;
	}
	if (!voteSelected){
		alert('You must rate this clip before voting.');
		return false;
	} else {
		if (whichForm.email.value == '' || whichForm.email.value == 'Your Email Address'){
			alert('You must enter your email address before I can count your vote.');
			return false;
		} else {
			return true;
		}
	}
	return false;
}

var at = /\@/i;
var period = /\./i;

function checkApp(whichForm){
	errorMessage = '';
	if (whichForm.username.value.length < 3) errorMessage += "You need to enter your name.\n";
	if (whichForm.email.value.length < 3) errorMessage += "You need to enter your email address.\n";
	else if ((!at.test(whichForm.email.value)) || (!period.test(whichForm.email.value))) {
		errorMessage += "You must enter a valid email address\n";
	}
	if (errorMessage) {
		alert(errorMessage);
		return false;
	}
	if ((whichForm.address.value.length < 5) || (whichForm.city.value.length < 5)){
		if (confirm('By not inlcuding your address, you will not be eligible to win a prize. Are you sure you want to do this?')){
			return true;
		} else {
			return false;
		}
	}
	return true;
}