// JavaScript Document

/*
* open a popup window
* usage: onclick="popupwindow('mypage.html', {windowname: 'mypage', width:600, height:300});
* options object is optional
*/
function popupwindow(url, options){

	if(! options)
	options = new Object;

	_width = options.width ? options.width:319;
	_height = options.height ? options.height:288;
	_scrollbars = options.scrollbars ? options.scrollbars:0;
	_resizable = options.resizable ? options.resizable:0;
	_toolbar = options.toolbar ? options.toolbar:0;
	_statusbar = options.statusbar ? options.statusbar:0;
	_menubar = options.menubar ? options.menubar:0;
	_location = options.location ? options.location:0;
	_windowname = options.windowname ? options.windowname.replace( /\W+/g,'_' ):'popupwindow'; // replace all non word chars in windowname
	_left = options.left ? options.left:(screen.width - _width) / 2; // else the center of the screen
	_top = options.top ? options.top:(((screen.height - _height ) / 2) - 30); // else the center of the screen

	_mypopup = window.open(url, _windowname,'toolbar='+_toolbar+',scrollbars='+_scrollbars+',location='+_location+',statusbar='+_statusbar+',menubar='+_menubar+',resizable='+_resizable+',width='+_width+',height='+_height+',top='+_top+',left='+_left);
	if(! _mypopup)
	alert("There was an error opening the "+_windowname+" window. If you have a popup-blocker please disable it for this site");
	else
	_mypopup.focus();
	
	return _mypopup;
	
	
}


/* changet the url of a parent window from a popup
* useage: <a href="javascript:void(0);" onclick="parentgoto('http://www.google.com/', true);">

*/
function parentgoto(_url, keepopen){

	if(window.opener){
		window.opener.location = _url;
		//window.opener.focus();
		window.opener.document.form.submit();
		if(! keepopen)
		window.close();
	}
	else
	window.location = _url;
}


function validateform() {
	
	//Array of required variables
	required_fields = new Array('firstname', 'surname', 'email', 'code', 'postcode' );
	var error = 0;
	for ( field in required_fields ) {
		if ( document.form[field].value == "" ) {
			alert( 'You have not filled out all required variables.' );
			error = 1;
			break;
		}
	}
	if ( error == 0 ) {
		//Check any radios
		if ( !document.form.rewards[0].checked && !document.form.rewards[1].checked ) {
			alert( 'Please indicate whether you are a rewards member of not. ');
			error = 1;
		}
		else {
			now = new Date();
			month = now.getMonth();
			year = now.getFullYear();
			code = createCode(month,year);
			if ( document.form.code.value.toUpperCase() != code ) {
				alert( 'The code you entered is incorrect.' );
				error = 1;
			}
		}
	}
	
	if ( error == 0 )
		popupwindow('proceed.php');
		
}


//function to create the monthly code
function createCode(month,year) {
	//Set the month
	var month_part
	switch(month) {
		case 0:
			month_part = 'JAN';
			break;
		case 1:
			month_part = 'FEB';
			break;
		case 2:
			month_part = 'MAR';
			break;
		case 3:
			month_part = 'APR';
			break;
		case 4:
			month_part = 'MAY';
			break;
		case 5:
			month_part = 'JUN';
			break;
		case 6:
			month_part = 'JUL';
			break;
		case 7:
			month_part = 'AUG';
			break;
		case 8:
			month_part = 'SEP';
			break;
		case 9:
			month_part = 'OCT';
			break;
		case 10:
			month_part = 'NOV';
			break;
		case 11:
			month_part = 'DEC';
			break;
		default:
			//We should never get here
			alert('There is a problem with the calendar code, please contact Chemmart');
	}
	
	year_part = '09';
	
	code = month_part + year_part;
	
	if ( month == 11 && year == 2008 )
		code = 'JAN09';
	
	
	return code;


}