// Divavote 2007 JavaScript Document

var sitename = "www.wwe.com";
var siteimages = "http://" + sitename + "/content/media/images/Superstars/5192572/";

var cookieDomain = "; domain=" + sitename;
var coppaAge = 13;
var coppaURL = "http://divavote.wwe.com/?coppa=1";
var flag = 0;
var coppaCookie = "COPPA13";

var dsContMid = new Array();
dsContMid[0] = new Image(93,116);
dsContMid[0].src = siteimages + "c_1-elim.jpg";
dsContMid[1] = new Image(93,116);
dsContMid[1].src = siteimages + "c_2-mid.jpg";
dsContMid[2] = new Image(93,116);
dsContMid[2].src = siteimages + "c_3-elim.jpg";
dsContMid[3] = new Image(93,116);
dsContMid[3].src = siteimages + "c_4-elim.jpg";
dsContMid[4] = new Image(93,116);
dsContMid[4].src = siteimages + "c_5-elim.jpg";
dsContMid[5] = new Image(93,116);
dsContMid[5].src = siteimages + "c_6-elim.jpg";
dsContMid[6] = new Image(93,116);
dsContMid[6].src = siteimages + "c_7-mid.jpg";
dsContMid[7] = new Image(93,116);
dsContMid[7].src = siteimages + "c_8-elim.jpg";

var dsContHi = new Array();
dsContHi[0] = new Image(93,116);
dsContHi[0].src = siteimages + "c_1-elim.jpg";
dsContHi[1] = new Image(93,116);
dsContHi[1].src = siteimages + "c_2-hi.jpg";
dsContHi[2] = new Image(93,116);
dsContHi[2].src = siteimages + "c_3-elim.jpg";
dsContHi[3] = new Image(93,116);
dsContHi[3].src = siteimages + "c_4-elim.jpg";
dsContHi[4] = new Image(93,116);
dsContHi[4].src = siteimages + "c_5-elim.jpg";
dsContHi[5] = new Image(93,116);
dsContHi[5].src = siteimages + "c_6-elim.jpg";
dsContHi[6] = new Image(93,116);
dsContHi[6].src = siteimages + "c_7-hi.jpg";
dsContHi[7] = new Image(93,116);
dsContHi[7].src = siteimages + "c_8-elim.jpg";

var dsContLo = new Array();
dsContLo[0] = new Image(93,116);
dsContLo[0].src = siteimages + "c_1-elim.jpg";
dsContLo[1] = new Image(93,116);
dsContLo[1].src = siteimages + "c_2-lo.jpg";
dsContLo[2] = new Image(93,116);
dsContLo[2].src = siteimages + "c_3-elim.jpg";
dsContLo[3] = new Image(93,116);
dsContLo[3].src = siteimages + "c_4-elim.jpg";
dsContLo[4] = new Image(93,116);
dsContLo[4].src = siteimages + "c_5-elim.jpg";
dsContLo[5] = new Image(93,116);
dsContLo[5].src = siteimages + "c_6-elim.jpg";
dsContLo[6] = new Image(93,116);
dsContLo[6].src = siteimages + "c_7-lo.jpg";
dsContLo[7] = new Image(93,116);
dsContLo[7].src = siteimages + "c_8-elim.jpg";


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getCookie(name) {
	var c = document.cookie;
	var start = c.indexOf(name + "=");
	if (start == -1) return false;

	start += name.length + 1;
	var end = c.indexOf(';', start);
	if (end == -1) { end = document.cookie.length; }
	var cookieval = c.substring(start, end);

	return cookieval;
}

function setCoppaCookie() {
	var expires = new Date((new Date()).getTime() + 24 * 3600000);
	var cookie = coppaCookie + "=";
	cookie += coppaAge;
	cookie += "; expires=" + expires.toGMTString();
	cookie += "; path=/";
  cookie += cookieDomain;
  document.cookie = cookie;
}

function checkCOPPACookie() {
	var cCheck = getCookie(coppaCookie);
	if (cCheck == "") {
		return true;
	} else if (cCheck <= coppaAge) {
		setCoppaCookie();
		setTimeout(function () { location.replace(coppaURL); }, 0);
		return false;
	}
}

function checkCOPPABirthday(bMonth,bDay,bYear) {
	var sDate = new Date();
	var sYear = sDate.getFullYear();
	var sMonth = sDate.getMonth() + 1;
	var sDay = sDate.getDate();

	if((sYear - bYear) < coppaAge) { flag++; }
	else if ((sYear - bYear) == coppaAge) {
		if(sMonth < bMonth) { flag++; }
		else if (sMonth == bMonth) {
			if(sDay < bDay) { flag++; }
		}
	}

	if (flag) {
		setCoppaCookie();
		setTimeout(function () {location.replace(coppaURL); }, 0);
		return false;
	}

	return true;
}

function validateBirthday(bMonth,bDay,bYear) {
	if ((bDay == "") || (bMonth == "") || (bYear == "")) { return false; }
	if ((bDay < 1) || (bDay > 31)) { return false; }
	if ((bMonth < 1) || (bMonth > 12)) { return false; }
	if ((bMonth == 4) || (bMonth == 6) || (bMonth == 9) || (bMonth == 11)) {
		if(bDay > 30) { return false; }
	}
	if(bMonth == 2) {
		if (bDay > 29) { return false; }
		if( ((bYear+0)%4 > 0) && (bDay > 28) ) { return false; }
	}
	return true;
}

function clearText(o) {
	if (o.defaultValue == o.value) {
		o.value = "";
		o.maxLength = 4;
	}
}

function validateEmail(e, re, max) {
	if (! re.test(e) || e.length > max) { return false; }
	return true;
}

function validateTextField(t, re) {
	if (! re.test(t)) { return false; }
	return true;
}

function notQuebec(s) {
	if (s.length == 0) {
		return false;
	}

	var c = s.substring(0,1).toUpperCase();

	if ("G" == c) {
		return true;
	} else if ("H" == c) {
		return true;
	} else if ("J" == c) {
		return true;
	}
	return false;
}

function validateDiva(vote) {
	var divaSelected = false;

	if (vote >= 1 && vote <= 8) {
		divaSelected = true;
	}
	return divaSelected;
}

function isDigit (d) {
	return ((d >= "0") && (d <= "9"))
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isInteger(s) {
	var i;

	if (isEmpty(s))
	if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}

	return true;
}

function validateSC(sc) {
	var validSC = false;

	if (isInteger(sc) && (sc >= 100000) && (sc <= 999999)) {
		validSC = true;
	}

	return validSC;
}

function submitForm(f) {
	var msg = '';
	var _vote = f.vote.value;
	var _tb_id = f.tb_id.value;

	if (checkCOPPACookie() == false) { return false; }

	if (!document.getElementById('welcome')) {

		var _month = f.month.value;
		var _day = f.day.value;
		var _year = f.year.value;

		if (!validateEmail(f.email.value, /^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i, 100)) { msg = msg + "Valid email address required\n"; }
		if ((_day != "") && (_month != "") && (_year != "" && isInteger(_year))) {
			if (checkCOPPABirthday(_month,_day,_year) == false) { return false; }
			if (validateBirthday(_month,_day,_year) == false || _year <= 1900) { msg = msg + "You must specify a valid Birthday to continue\n"; }
		} else {
			msg = msg + "Date of birth required\n";
		}
	}

	if (!validateDiva(_vote)) { msg = msg + "Please select a Diva\n"; }

	if (!validateSC(_tb_id)) { msg = msg + "Security code required\n"; }

	if (flag == 0 && msg != "" ) {
		msg = "The following errors were found:\n" + msg;
		alert(msg);
		return false;
	}
	return true;
}

function puffRemove(num) {
	$('puffContestant'+num).remove();
}

function dsVote(num) {
	//remove puff if any
	if($('puffContestant'+num)) puffRemove(num);

	//set form field
	$('vote').value = num;

	//clear previous selections
	for(var i=1;i<9;i++) {
		$('contestant'+i).src = dsContLo[i-1].src;
		$('cName'+i).style.color = '#333333';
	}

	//set selected contestant image and name
	$('contestant'+num).src = dsContHi[num-1].src;
	$('cName'+num).style.color = '#FF0099';

	//get styles and dimensions
	var pos = findPos($('contestant'+num));
	//make puff image
	var puffContestant = $('contestant'+num).cloneNode(false);
	puffContestant.id = 'puffContestant'+num;
	var cTop = '0px';
	var cLeft = pos[0]-9+'px';
	var cHeight = $('contestant'+num).getHeight() + 'px';
	var cWidth = $('contestant'+num).getWidth() + 'px';

	//append puff image
	$('voteForm').appendChild(puffContestant);

	//set styles on puff image
	puffContestant.setStyle({position:'absolute',margin:'0',padding:'0',left:cLeft,top:cTop,height:cHeight,width:cWidth,zIndex:'20'});

	//do puff
	Effect.Puff('puffContestant'+num, {duration:1.0});
}


function dsHi(num) {
	$('contestant'+num).src = dsContHi[num-1].src;
}

function dsMid(num) {
	if($('vote').value == '') {
		$('contestant'+num).src = dsContMid[num-1].src;
	} else if($('vote').value != num) {
		dsLo(num);
	}
}
function dsLo(num) {
	$('contestant'+num).src = dsContLo[num-1].src;
}
