// JavaScript Document

function closeWindows() {
	document.getElementById('rules').style.display = 'none';
	if (document.getElementById('form'))
		document.getElementById('form').style.display = 'none';
	if (document.getElementById('thanks'))
		document.getElementById('thanks').style.display = 'none';
}

function closeRulesWindow() {
	document.getElementById('rules').style.display = 'none';
}

function openRulesWindow() {
	document.getElementById('rules').style.display = 'block';
}

function openFormWindow() {
	this.text = document.getElementById('caption').value;
	if (this.text != "" && this.text != "Type your caption here...") {
		closeWindows();
		document.getElementById('form').style.display = 'block';
	}
}

function clearTextArea() {
	this.el = document.getElementById('caption');
	if (this.el.value == "Type your caption here...") {
		this.el.value = "";
	}
	this.el.style.color = "#000000";
}

function textAreaMaxLength() {
	this.el = document.getElementById('caption');
	if (this.el.value.length > 200)
		this.el.value = this.el.value.substring(0,200);
}

function formatPhone(el,event) {
	this.lastchar = el.value.substr(el.value.length-1,el.value.length);
	input = stripNonNumeric(el);
	//If deleting
	if(event.keyCode == 8 && (input.length <= 3 || input.length == 6) && (this.lastchar != ' ' && this.lastchar != '-')) {
		if (input.length == 1) {
			el.value = '';
			return;
		}
		else
			input = input.substr(0,input.length-1);
	}
	if (input.length == 0)
		return;
	else if (input.length < 4)
		el.value = '('+input+')';
	else if (input.length < 6)
		el.value = '(' + input.substring(0,3) + ') ' + input.substring(3);
	else
		el.value = '(' + input.substring(0,3) + ') ' + input.substring(3,6) + '-' + input.substring(6);
}

function stripNonNumeric(el) {
	this.out = '';
	this.input = el.value;
	for (this.i = 0; this.i <= this.input.length; this.i++) {
		this.val = this.input.substring(this.i,this.i+1);
		if (this.val in {'0':'','1':'','2':'','3':'','4':'','5':'','6':'','7':'','8':'','9':''})
			this.out += this.val;
	}
	return this.out;
}

function updateTimer() {

	if (days == 0 && hours == 0 && minutes == 0 && seconds == 0) {
		clearInterval();
		setTimeout('window.location = "/submit"',5000);
		return;
	}
		
	if (seconds == 0) {
		seconds = 59;
		if (minutes == 0) {
			minutes = 59;
			if (hours == 0) {
				hours = 23;
				days--;
			}
			else
				hours--;
		}
		else
			minutes--;
	}
	else
		seconds--;
	
	document.getElementById('days').innerHTML = days;
	document.getElementById('hours').innerHTML = hours;
	document.getElementById('minutes').innerHTML = minutes;
	document.getElementById('seconds').innerHTML = seconds;
}

function eventListener(el,action,func) {
    if (el) {
        if (el.addEventListener)
            el.addEventListener(action, func, false);
        else if (el.attachEvent)
            el.attachEvent('on' + action, func);
    }
}

//Run it
eventListener(window,'resize',positionWindow);
if (typeof(days) != undefined) {
	updateTimer();
	setInterval("updateTimer()",1000);
}