/* globals */
var win = null;
var filter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,5}|\d+)$/;

function NewWindow(mypage,myname,w,h,s,r){
	LeftPos = (screen.width) ? (screen.width-w)/2 : 0;
	TopPos = (screen.height) ? (screen.height-h)/2 : 0;
	settings ='height='+h+',width='+w+',top='+TopPos+',left='+LeftPos+',scrollbars='+s+',resizable='+r+',status=yes';
	win = window.open(mypage,myname,settings);
}
/* init valid popup and external windows */
function windowLinks() {
    if(!document.getElementsByTagName) {
         return;
    }
    var anchors = document.getElementsByTagName('a');
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var relIndex = anchor.rel;
		 if (relIndex){
		 var relSplit = relIndex.split('|');
		 /* XHTML compliant target attribute */
		 if (relSplit[0] == 'external') {
            anchor.target = '_blank';
			anchor.className = 'external';
			anchor.title = 'Load in new window: '+ anchor.href;
			/* XHTML compliant popup attribute */
   		} else if (relSplit[0] == 'popup') {
			if (anchor.className == '' && anchor.title == '') {
			  anchor.className = 'popup';
			  anchor.title = 'Link loads in Popup Window';
			}
			anchor.popupWidth = relSplit[1];
			anchor.popupHeight = relSplit[2];
			/* added the ablitity to have resize or scroll */
			anchor.popupScroll = relSplit[3];
			anchor.popupResize = relSplit[4];
	        anchor.onclick = function() {
				NewWindow(this.href,'',this.popupWidth,this.popupHeight,this.popupScroll,this.popupResize);return false;
			   };
			}
		}
	}
}

/* toggles visibility */
function expandCollapse() {
	for (var i=0; i<expandCollapse.arguments.length; i++) {
		var element = document.getElementById(expandCollapse.arguments[i]);
		element.style.display = (element.style.display == 'none' || element.style.display == '') ? 'block' : 'none';
	}
}
/* check that a user wants to log out */
function logOut() {
	ht = document.getElementsByTagName('html')[0];
	ht.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)';
	if (confirm('Are you sure you want to log out?')) {
		return true;
	} else {
		ht.style.filter = '';
		return false;
	}
}

/* validates the newsletter subscription */
function validateSubscription() {
	if(!gebi('newsletterform')) {return;}
	/* get the newsletter form wait for it to submit */
	var nf = gebi('newsletterform');
	nf.onsubmit = function() {
		/* if either the name of email are not correct halt the submission */
		if(nf.elements['NewsletterName'].value == 'Name') {
			alert('Please enter your name');
			nf.elements['NewsletterName'].focus();
			return false;			
		}
		if(!filter.test(nf.elements['NewsletterEmail'].value)) {
			alert('Please enter a valid email address');
			nf.elements['NewsletterEmail'].focus();
			return false;
		}
		return true;
	};
}

function selectionBox() {
	if(!gebi('process')) {return;}
	var processBtn = gebi('process');
	var printBox = gebi('print_selections');
	var emailBox = gebi('email_selections');
	var enquireBox = gebi('enquire_selections');
	processBtn.onclick = function() {
		if (!printBox.checked && !emailBox.checked && !enquireBox.checked) {
			alert('Please check at least one option');	
			return false;
		}
		if (emailBox.checked || enquireBox.checked) {
			if (emailBox.checked && !filter.test(gebi('selection_email').value)) {
				alert('Please enter the email you want to send your selections to');
				return false;	
			}			
			if (gebi('enquire_name').value == '') {
				alert('Please enter your name');
				return false;	
			}
			if (!filter.test(gebi('enquire_email').value)) {
				alert('Please enter your email');
				return false;	
			}			
		}
		if (printBox.checked) {
			NewWindow('cfincludes/selections.cfm?CFID='+readCookie('CFID')+'&CFTOKEN='+readCookie('CFTOKEN'),'',600,500,'yes','yes');	
			win.focus();
		}		
	};
}

addLoadListener(windowLinks);
addLoadListener(selectionBox);
addLoadListener(validateSubscription);
