var showBrochureRequestOnLoad = false;
var showLoginOnLoad = false;
var submitLoginForm = false;

function openBasketWindow() {
	var w = window.open( 'blank.html', 'basket', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, width=500, height=400');
	w.focus();
}

function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
				input.value = '';
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}

function correctPNG( imageId ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		var img = document.getElementById( imageId );
		var imgName = img.src.toUpperCase()
		var imgID = (img.id) ? "id='" + img.id + "' " : "";
		var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		var imgStyle = "display:inline-block;" + img.style.cssText 
		if (img.align == "left") imgStyle = "float:left;" + imgStyle
		if (img.align == "right") imgStyle = "float:right;" + imgStyle
		if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		var strNewHTML = "<span " + imgID + imgClass + imgTitle
		+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		img.outerHTML = strNewHTML
	}    
}

function correctPNGBackground( divId, imgURL ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		var div = document.getElementById( divId );
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgURL+"', sizingMethod='scale')";
		div.style.background = '';
	}    
}

function showHideElement( elementId ) {
	if ( document.getElementById( elementId ).style.display == 'none' ) {
		document.getElementById( elementId ).style.display = '';
	} else {
		document.getElementById( elementId ).style.display = 'none';
	}
}

function hideElement( elementId ) {
	document.getElementById( elementId ).style.display = 'none';
}

function PADIeLearning() {
	accessWin = window.open('https://www.padi.com/padi/elearning/default.aspx','','scrollbars=yes,top='+(screen.height/8)+',left='+(screen.width/8)+',width=1000,height=700,resizable=yes');
	window.accessWin.focus();
}

function subscribeUserInline(){
	
	var validateform = new validateForm();
	// check we have name, email
	var emailAddress = document.getElementById('cmsContentSubscribe-emailAddressInline');
	if(( emailAddress.value != '- Email Address -' ) && ( emailAddress.value != '' )){
		// check email address is an email address
		validateform.validateEmailAddress( emailAddress.value );
		
		if(validateform.numberOfErrors() > 0) {
			validateform.displayErrors();
		} else {
			// check for default '- Name -' in the name field
			if( document.getElementById('cmsContentSubscribe-nameInline').value == '- Name -' ){
				// set it to blank
				document.getElementById('cmsContentSubscribe-nameInline').value = '';
			}
			return true;
		}
	} else {
		validateform.addCustomError( 'Please enter your email address.' );
		validateform.displayErrors();
		emailAddress.focus();
		return false;
	}
	
}

function numberFormat(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

var showTipOnLoad = false;


function hideTip() {
	// Show the Panel
	if( typeof tipBox != "undefined" )
		tipBox.hide();
}

function showTip( description ) {
	// clear the password field
	document.getElementById('tipText').innerHTML = description;
	// Show the Panel
	if( typeof tipBox != "undefined" )
		tipBox.show();
	else
		showTipOnLoad = true
}




function hideBrochureRequest() {
	if( typeof brochureBox != "undefined" )
		brochureBox.hide();
}

function showBrochureRequest() {
	if( typeof brochureBox != "undefined" )
		brochureBox.show();
	else
		showBrochureRequestOnLoad = true;
}

function validateBrochureRequest() {
	var validate = new validateForm();
	validate.checkText( 'brochure_firstname', 'Your First Name' );
	validate.checkText( 'brochure_surname', 'Your Surname' );
	validate.validateEmailAddress( 'brochure_email', 'Your Email Address' );
	if ( validate.numberOfErrors() > 0 ) {
		validate.displayErrors();
		return false;
	} else {
		return true;
	}
}



function hideLogin() {
	if( typeof loginBox != "undefined" )
		loginBox.hide();
	submitLoginForm = false;
}

function showLogin() {
	if( typeof loginBox != "undefined" )
		loginBox.show();
	else
		showLoginOnLoad = true;
}

function validateLogin() {
	// if we are submitting to form
	if( submitLoginForm ){
		// ensure we have username and password
		var validate = new validateForm();
		if( document.getElementById('clientLogin_username').value == ' - type here -'){
			validate.addCustomError( 'Email' );
		} else {
			validate.checkText( 'clientLogin_username', 'Email' );	
		}
		validate.checkText( 'clientLogin_password', 'Password' );
		if( validate.numberOfErrors() > 0 ) {
			validate.displayErrors();
			// we have errors
			submitLoginForm = false;
		}
	}
	
	return submitLoginForm;
}

