/**
 *	Digital Copy: optin page script
 *  	Warner Brothers
 *  	Author: Ramil Jugao
*/

var nextPageURL = "index2.html";
var coppaMsg = "We're sorry.  We cannot offer the newsletter feature to you at this time, but you may continue with your Digital Copy redemption process.";

var today = new Date();	
var thisMonth = today.getMonth() + 1;
var thisDate = today.getDate();
var thisYear = today.getFullYear();
	
var requestTimeout = 30000;
	
var monthList = $A($R(1, 12));
var monthNamesList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var dayList = $A($R(1, 31));
var yearList = $A($R(thisYear - 80, thisYear)).reverse();


Event.observe(document, 'dom:loaded', function () {

	// populate month list
	var mlen = monthNamesList.length;
	for (var m = 0; m < mlen; m++) {
		$('month').insert( new Element('option', {value: m}).update(monthNamesList[m]) );	
	}

	// populate day list
	dayList.each(function(item) {
		$('day').insert( new Element('option', {value: item}).update(item) );
	});

	// populate year list
	yearList.each(function(item) {
		$('year').insert( new Element('option', {value: item}).update(item) );
	});
	
	
}); 	



/**
* 	Checks if the string is a valid email
*/
function isEmailValid(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
			return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			return false;
		 }

		if (str.substring(ldot+1,lstr).length == 0){
			return false;
		 }
		 
		 if (str.indexOf(" ")!=-1){
			return false;
		 }

		 return true;
}

/**
* 	Proceed to the next page
*/
function gotoNextPage() {
	document.location.replace(nextPageURL);
}

/**
* 	Checks if the entered birthdate is under 13 years
*/
function isUnder13() {
	var myMonth = $F('month');
	var myDate = $F('day');
	var myYear = $F('year');

	if (myMonth && myDate && myYear) {
		if (Number(thisYear) - Number(myYear)  > 13) 
			return false;
		else if  (Number(thisYear) - Number(myYear) == 13) 
			if (Number(myMonth) < Number(thisMonth)) 
				return false;
			 else if (Number(myMonth) == Number(thisMonth)) 
				if (Number(myDate) <= Number(thisDate)) return false;
				
		return true;
	} else {
		return false;
	}
	
}

/**
* 	Check for invalid entries in the form
*/
function isValid() {
	
	// check if required info is filled up
//	if ($F('fname') || $F('email') || $F('month') || $F('day') || $F('year') || $F('reel_news') || $F('wb_hd_release')) {
	if ($F('reel_news')) {
		// check if first name is missing
		if (!$F('fname')) {
			$('error-message').update('Please enter your first name');
			return false;

		// check if the email is missing	
		} else if (!$F('email')) {
			$('error-message').update('Please enter your email address');
			return false;
			
		// check if the email is valid	
		} else if (!isEmailValid($F('email')) ) {
			$('error-message').update('The email address you entered is not valid');
			return false;
			
		// check if the birth month is missing			
		} else if (!$F('month')) {
			$('error-message').update('Please enter the month of your birthday');
			return false;
			
		// check if the birth day is missing
		} else if (!$F('day')) {
			$('error-message').update('Please enter the day of your birthday');
			return false;
			
		// check if the birth month is missing
		} else if (!$F('year')) {
			$('error-message').update('Please enter the year of your birthday');
			return false;
			
		// check if the options are missing
		} else if (!$F('reel_news')) {
			$('error-message').update('Please select a signup option');
			return false;

		}
	}
	
	return true;
}

/**
*	Validate the form before submitting
*/
function validateForm() {

	// disable next button and clear the error message
	disableNextButton();
	$('error-message').update('');

	if (isValid()) {
		
		if (isUnder13()) {
			// display message 
			alert(coppaMsg);
			gotoNextPage();
			// enable to fix issue in safari
			enableNextButton();
			$('optinForm').reset();
		} else {
			// submit the form
			$('optinForm').submit();
			enableNextButton();
		}
		
		
	} else {
		enableNextButton();
	
	}

}

/**
*	Disable the 'Next' button
*/
function disableNextButton() {
	if ($('next')) {
		$('next').value = "Please wait...";
		$('next').disable();
	}
}

/**
*	Enable the 'Next' button
*/
function enableNextButton() {
	if ($('next')) {
		$('next').value = "Next";
		$('next').enable();
	}
}













/**
*	Ajax timeout
*/
function callInProgress (xmlhttp) {
	switch (xmlhttp.readyState) {
		case 1: case 2: case 3:
			return true;
			break;
		// Case 4 and 0
		default:
			return false;
			break;
	}
}
function showFailureMessage() {
	alert('There was no response from the server request. It has been timed out after 30 seconds');
	
	enableNextButton();
	
	gotoNextPage();
}
// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
	onCreate: function(request) {
		request['timeoutId'] = window.setTimeout(
		function() {
			// If we have hit the timeout and the AJAX request is active, abort it and let the user know
			if (callInProgress(request.transport)) {
				request.transport.abort();
				showFailureMessage();
				// Run the onFailure method if we set one up when creating the AJAX object
					if (request.options['onFailure']) {
						request.options['onFailure'](request.transport, request.json);
					}
				}
			},
			requestTimeout 
		);
	},
	onComplete: function(request) {
		// Clear the timeout, the request completed ok
		window.clearTimeout(request['timeoutId']);
	}
});