// JavaScript Document
var dateChanged = true;
var popup_dragging = false;
var popup_target;
var popup_mouseX;
var popup_mouseY;
var popup_mouseposX;
var popup_mouseposY;
var popup_oldfunction;

if (document.images)
{
  pic1= new Image(100,25); 
  pic1.src="../Images/Layout%20Images/navbarwhite.png";
  pic2= new Image(100,25); 
  pic2.src="../Images/Layout%20Images/navbarwhite.jpg"; 
}


function navChange(dir,id,type){	
	var path = document.location.href;
	var counter =1;
	var display = '';
	path = path.substr(path.indexOf('/',7)+1);
	while (path.indexOf('/')!=-1){
		path = path.substr(path.indexOf('/')+1);
		counter = counter+1;
	}
	var exten = '';
	while (counter>1){
		exten = exten + '../';
		counter = counter-1;
	}
	if(dir){
		document.getElementById(id).style.backgroundImage = "url("+exten+"Images/Layout%20Images/navbarwhite."+type+")";
		display = "block";
	} else {
		document.getElementById(id).style.backgroundImage = "url("+exten+"Images/Layout%20Images/navbarblack."+type+")";
		display = "none";
	}  
	if(type=="jpg"){
		id = id + "ul";
		document.getElementById(id).style.display = display;
	}
}

function ImgAdjust()
{
	var viewportwidth=0;
	if (typeof window.innerWidth != 'undefined') viewportwidth = window.innerWidth; 
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) viewportwidth = document.documentElement.clientWidth;
	else viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	
	if(document.getElementById('ImgLeft') && document.getElementById('ImgRight')){
		var imgLeft = document.getElementById('ImgLeft');
		var imgRight = document.getElementById('ImgRight');
		if(viewportwidth>905){
			imgLeft.style.height= 20 + 'px';
			imgRight.style.height= 20 + 'px';
			imgLeft.style.width=Math.round((viewportwidth-905)/2)-10 + 'px';
			imgRight.style.width=Math.round((viewportwidth-905)/2)-10 + 'px';
		}else{
			imgLeft.style.width=0+ 'px';
			imgLeft.style.height=0 + 'px';
			imgRight.style.width=0+ 'px';
		}
	}
	return
}

function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("navbar");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
} 



function HeadImgChange(image,address,vid){	
	if (vid=="vid") {
		code='<object width="300" height="250"><param name="movie" value="'+ image +'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+ image +'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="300" height="250"></embed></object>';
		//code='<object width="300" height="250" ><param name="movie" value="' + image + '"><param name="allowFullScreen" value="true"><param name="allowscriptaccess" value="always"><embed wmode="transparent" src="' + image + '" width="300" height="250" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" movie="' + image + '"> </embed></object>';
		document.getElementById('AHeadLineLink').removeAttribute('href');
	} else {
		code = '<img src=' + image + ' alt="Head Line Image" name="ImgHeadLine" width="300" height="250" id="ImgHeadLine">';
		document.getElementById('AHeadLineLink').href= address;
	}
	document.getElementById('AHeadLineLink').innerHTML = code;
	
}

function StartFunctions(id){
	setFocus(id);
	if(id == 'submit') {
		TimeFill('submit');
		DateCheck('submit');
	} else if (id=='newslettercreator'){
		DateCheck('newsletter');
	} else if (id=='createpress') {
		DateCheck('createpress');
	}
	if(id=='delpress'){
		displayReleases();
	}
	if(id=='lname' || id=='code' || id=='candidateName' || id=='mcode'){
		setFocus(id);
	}
	ImgAdjust();
	startList();
}

function setFocus(id){
	switch(id){
		case 'lname':
		var element = document.frmproxynocard.lname;
		break;
		case 'code':
		var element = document.frmwithcard.code;
		break;
		case 'candidateName':
		var element = document.addCandFrm.candidateName;
		break;
		case 'mcode':
		var element = document.votingFrm.mcode;
		break;
	}
	if(element!=undefined){
		element.value = "";
		element.focus();
	}
}

function DateCheck(id) {
	var d= new Date();
	switch(id){
		case 'submit':
		var daySelect = document.SubmitEvent.EventDay;
		var yearSelect=document.SubmitEvent.EventYear;
		var monthSelect=document.SubmitEvent.EventMonth;
		break;
		case 'createpress':
		var daySelect = document.PressRelease.EventDay;
		var yearSelect=document.PressRelease.EventYear;
		var monthSelect=document.PressRelease.EventMonth;		
		break;
		case 'caledit':
		var daySelect = document.caledit.EventDay;
		var yearSelect=document.caledit.EventYear;
		var monthSelect=document.caledit.EventMonth;			
		break;
		case 'SCMinfo':
		var daySelect = document.basicInfo.day;
		var yearSelect=document.basicInfo.year;
		var monthSelect=document.basicInfo.month;	
		break;
		case 'intern':
		var daySelect = document.frmInternship.day;
		var yearSelect=document.frmInternship.year;
		var monthSelect=document.frmInternship.month;	
		break;
	}
	
	var days=0;
	if(!dateChanged){
		for (i=0;i<11;i++) {
			var newyear=i+d.getFullYear();
			yearSelect.options[yearSelect.options.length]= new Option(newyear,newyear,"false","false");
		}
	}
	switch(monthSelect.value) {
		case "January":
		case "March":
		case "May":
		case "July":
		case "August":
		case "October":
		case "December":
		case "01":
		case "03":
		case "05":
		case "07":
		case "08":
		case "10":
		case "12":
		days=31;
		break;
		case "April":
		case "June":
		case "September":
		case "November":
		case "04":
		case "06":
		case "09":
		case "11":
		days=30;
		break;
		case "February":
		case "02":
		days=28;
		if (isLeapYear(yearSelect.value)) days++;
		break;
	}
	daySelect.options.length=0;
	for(i=1;i<=days;i++) {
		daySelect.options[daySelect.options.length]= new Option(i,i,"false","false");
	}
	if (!dateChanged){
	daySelect.selectedIndex=d.getDate()-1;
	yearSelect.selectedIndex=0;
	monthSelect.selectedIndex=d.getMonth();
	dateChanged= true;
	} else daySelect.selectedIndex=d.getDate()-1;
}

function TimeFill(){
	var d= new Date();
	var hours=d.getHours();
	var minutes=d.getMinutes();
	var startHour=document.SubmitEvent.StartHours;
	var startMin=document.SubmitEvent.StartMinutes;
	var endHour=document.SubmitEvent.EndHours;
	var endMin=document.SubmitEvent.EndMinutes;
	var startAbbr=document.SubmitEvent.StartAbbr;
	var endAbbr=document.SubmitEvent.EndAbbr;
	startHour.options.length = 0;
	startMin.options.length = 0;
	endHour.options.length = 0;
	endMin.options.length = 0;
	
	for (i=0;i<=59;i++) {
		var time;
		if (i<10) time = "0"+String(i);
		else time=String(i);
		if (i<13&&i>0) {
			startHour.options[startHour.options.length]=new Option(i,i,"false","false");
			endHour.options[endHour.options.length]=new Option(i,i,"false","false");
		}
		startMin.options[startMin.options.length]=new Option(time,time,"false","false");
		endMin.options[endMin.options.length]=new Option(time,time,"false","false");
	}
	startMin.options[startMin.options.length]=new Option('TBD','TBD',"false","false");
	endMin.options[endMin.options.length]=new Option('TBD','TBD',"false","false");
	startHour.options[startHour.options.length]=new Option('TBD','TBD',"false","false");
	endHour.options[endHour.options.length]=new Option('TBD','TBD',"false","false");
	if(hours>=12&&minutes>0) {
		if(hours==12) startHour.selectedIndex=hours-1;
		else startHour.selectedIndex=hours-13;
		
		endHour.selectedIndex=hours-12;
	} else {
		if (hours==0) {
			startHour.selectedIndex=11;
			endHour.selectedIndex=10;				 
		}
		startHour.selectedIndex=hours-1;
		endHour.selectedIndex=hours;
	}
	
	if(hours>11){
		startAbbr.selectedIndex=1;
		endAbbr.selectedIndex=1;
	} else {
		startAbbr.selectedIndex=0;
		if (hours+1>11)	endAbbr.selectedIndex=1;
		else endAbbr.selectedIndex=0;
	}
	startMin.selectedIndex=minutes;
	endMin.selectedIndex=minutes; 
	
}

function isLeapYear(yyyy) {
  if ((yyyy%400==0) || ((yyyy%4 == 0) && (yyyy%100 != 0))) return true;
  else return false;
}

function checkSubmit(Allow){ 
	if(Allow){
		return true;
	} else {		
		document.getElementById("vcode").focus();
		return false;
	}
}

function writeEmail(name,ele){
	if(ele==null){
		ele="";
	}
	host = "padems";
	switch(name){
		case "Jim":
		email = "jrb";
		break;
		case "Fadia":
		email = "fadia";
		break;
		case "Shannon":
		email = "shannon";
		break;
		case "Justin":
		email = "justin";
		break;
		case "Vivian":
		email = "vivian";
		break;
		case "Penny":
		email = "penny";
		break;
		case "Sabrina":
		email = "sabrina";
		break;
		case "Kevin":
		email = "washo";
		break;
	}
	email = email + "&#64;" + host + "&#46;com";
	if(ele==""){
		document.getElementById(name).innerHTML = "<a href=" + "mail" + "to:" + email + ">" + email + "</a>";
	} else {
		document.getElementById(ele).innerHTML = "<a href=" + "mail" + "to:" + email + ">" + email + "</a>";
	}
}

function popup_mousedown(e){
  var ie = navigator.appName == "Microsoft Internet Explorer";
  popup_mouseposX = ie ? window.event.clientX : e.clientX;
  popup_mouseposY = ie ? window.event.clientY : e.clientY;
}

function popup_mousedown_window(e){
  var ie = navigator.appName == "Microsoft Internet Explorer";

  if (ie && window.event.button != 1) return;
  if (!ie && e.button!= 0) return;

  popup_dragging = true;
  popup_target = this['target'];
  popup_mouseX = ie ? window.event.clientX : e.clientX;
  popup_mouseY = ie ? window.event.clientY : e.clientY;

  if (ie)
       popup_oldfunction = document.onselectstart;
  else popup_oldfunction = document.onmousedown;

  if (ie)
       document.onselectstart = new Function("return false;");
  else document.onmousedown = new Function("return false;");
}

function popup_mousemove(e){
  var ie = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);
  var mouseX  = ie ? window.event.clientX : e.clientX;
  var mouseY  = ie ? window.event.clientY : e.clientY;

  if (!popup_dragging) return;

  element.style.left = (element.offsetLeft+mouseX-popup_mouseX)+'px';
  element.style.top = (element.offsetTop +mouseY-popup_mouseY)+'px';

  popup_mouseX = ie ? window.event.clientX : e.clientX;
  popup_mouseY = ie ? window.event.clientY : e.clientY;
}

function popup_mouseup(e){
  var ie = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  if (!popup_dragging) return;

  popup_dragging = false;

  if (ie)
       document.onselectstart = popup_oldfunction;
  else document.onmousedown = popup_oldfunction;
}

function popup_exit(e){
  var ie = navigator.appName == "Microsoft Internet Explorer";
  var element = document.getElementById(popup_target);

  popup_mouseup(e);
  element.style.display = 'none';
}

function popup_show(id, drag_id, exit_id, position, x, y, position_id){
  var element = document.getElementById(id);
  var drag_element = document.getElementById(drag_id);
  var exit_element = document.getElementById(exit_id);

  var width = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
  var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

  element.style.position = "absolute";
  element.style.display = "block";

  if (position == "element" || position == "element-right" || position == "element-bottom"){
    var position_element = document.getElementById(position_id);
    for (var p = position_element; p; p = p.offsetParent)
      if (p.style.position != 'absolute'){
        x += p.offsetLeft;
        y += p.offsetTop;
      }
    if (position == "element-right") x += position_element.clientWidth;
    if (position == "element-bottom") y += position_element.clientHeight;
    element.style.left = x+'px';
    element.style.top = y+'px';
  }

  if (position == "mouse")  {
    element.style.left = (document.documentElement.scrollLeft+popup_mouseposX+x)+'px';
    element.style.top = (document.documentElement.scrollTop +popup_mouseposY+y)+'px';
  }
  if (position == "screen-top-left"){
    element.style.left = (document.documentElement.scrollLeft+x)+'px';
    element.style.top = (document.documentElement.scrollTop +y)+'px';
  }
  if (position == "screen-center"){
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )/2+x)+'px';
    element.style.top = (document.documentElement.scrollTop +(height-element.clientHeight)/2+y)+'px';
  }
  if (position == "screen-bottom-right"){
    element.style.left = (document.documentElement.scrollLeft+(width -element.clientWidth )+x)+'px';
    element.style.top = (document.documentElement.scrollTop +(height-element.clientHeight)+y)+'px';
  }
  drag_element['target'] = id;
  drag_element.onmousedown = popup_mousedown_window;
  exit_element.onclick = popup_exit;
}

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent('onmousedown', popup_mousedown);
else document.addEventListener('mousedown', popup_mousedown, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent('onmousemove', popup_mousemove);
else document.addEventListener('mousemove', popup_mousemove, false);

if (navigator.appName == "Microsoft Internet Explorer")
     document.attachEvent('onmouseup', popup_mouseup);
else document.addEventListener('mouseup', popup_mouseup, false);

function countChars(ele){
	var text = document.getElementById("counting");
	var textbox = ele.value;
	var count = 140-textbox.length;
	if(count<0){
		text.innerHTML = "Characters Left: <span class='required'>"+count+"</span>";
	} else {
		text.innerHTML = "Characters Left: "+count;
	}
}