function popupWin (name, url, title, width, height, closemsg) {
	var popupwin = dhtmlwindow.open(name, "iframe", url, title, "width="+width+"px,height="+height+"px,resize=1,scrolling=1,center=1", "recal")
	
	var winname = eval('window.'+name);
	winname = popupwin;
	
	popupwin.onclose=function(){return window;}
}


//=AJR======================================================
// Used in #searchBox  on Browse pages to reveal Value boxes
// from Drop-down filters
//
// id = element ID to hide/reveal
// value = value checked from <option> tag
// trigger = specific value to reveal on, if many options.  Default = null
//
//==========================================================
function reveal(id, value, trigger) {
	
	if (value == '0' || value == '' || (value != trigger && trigger != null)) {
		document.getElementById(id).style.display = 'none';
		
	} else if (value == trigger || trigger == null) {		
		document.getElementById(id).style.display = '';
	}
}// END function


//=AJR======================================================
// 
//  Check Terms and Conditions has been checked
//
//==========================================================
function checkTerms() {
	if (!$('agreeTCs')) return true;
	
	if (!$('agreeTCs').checked) alert("Please agree to our Terms and Conditions before proceeding to pay.");
	
	return $('agreeTCs').checked;
	
}// END function




//=WW=======================================================
// CheckEmail(obj, classname)
// Basic email validation check, uses timer so it can be 'live'
// Usage: onkeyup=CheckEmail('id_of_inputbox', 'bademail')
//
// inputid		- inputbox element ID
// classname	- classname to add to inputbox if email invalid
//==========================================================
function CheckEmail(inputid, classname) {
	if(checktimer > 0) window.clearTimeout(checktimer);
	checktimer = window.setTimeout('CheckEmail_Callback("'+inputid+'", "'+classname+'")', 1000);
}
function CheckEmail_Callback(inputid, classname) {
	obj = document.getElementById(inputid);
	if(obj.value.length == 0) {
		if(typeof classname !== undefined)
   			obj.removeClass(classname);
   		return true;
	}
	var reg = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(obj.value) == false) {
		if(typeof classname !== undefined)
			obj.addClass(classname);
		return false;
	} else {
		if(typeof classname !== undefined)
   			obj.removeClass(classname);
   		return true;
	}
}

//=WW=======================================================
// CreateElement(data)
// Creates a DOM element by passing an object with all 
// the information you want for the element in it.
// To avoid memory leaks in IE, pass one field as parent with a
// string identifying the ID of the parent DOM element to add to
//
// e.g. to create a tr with a class of 'header'
//		tr = CreateElement({tagtype: 'tr', className: 'header'});
//
// or to create a div with text inside
//		div = CreateElement({tagtype: 'div', text: 'Some text'});
//
// or to create a div containing two images
//		div = CreateElement(tagtype: 'div', children: 
//			[{tagtype: 'img', src: 'image1.jpg'}, {tagtype: 'img', src: 'image2.jpg'}]);
//
// Function returns the element to be used in an append
// e.g. table.appendChild(tr);
//==========================================================
function CreateElement(data)
{
	var e = document.createElement(data.tagtype);
	if(data.tagtype == 'input') e.type = (data.type == undefined ? 'text' : data.type);
	if(typeof data.parent == 'string') data.parent = document.getElementById(data.parent);
	if(data.parent != undefined) data.parent.appendChild(e);
	for (var key in data) 
		if (key == 'style') for (var s in data.style) eval('e.style.' + s + ' = data.style.' + s);
		else if (key == 'text') e.appendChild(document.createTextNode(data.text));
		else if (key == 'children') for (var i = 0; i < data.children.length; i++) {data.children[i].parent = e;CreateElement(data.children[i]);} 
		else if (key.substr(0, 2) == 'on') $(e).addEvent(key.replace('on', ''), data.bind != undefined ? function(event){eval(eval('data.on'+event.type));}.bind(data.bind) : function(event){eval(eval('data.on'+event.type));});
		else if (key != 'parent' && key != 'bind') eval('e.' + key + ' = data.' + key);
	return e;
}

//=WW=======================================================
// IsValidDate(date, direction)
// Attempts to validate a date. Depending on the date, can 
// take into account dd/mm or mm/dd. Will also accept 2 or 4
// year dates and the use of - or / as the separator.
// Can also check if the date is before/after today.
//
// date			- string to validate
// direction	- optional. if specified will check if the date
//					is before or after today. Specify -1 for
//					before today, 1 for after today.
//
// Returns false if invalid or yyyy-mm-dd if valid.
//==========================================================
function IsValidDate(date, direction) {
	while(date.indexOf('-') > -1) date = date.replace('-', '/');
	while(date.indexOf(' ') > -1) date = date.replace(' ', '');
	date = date.split('/');
	
	if(date.length != 3) return false;
		
	if(isNaN(date[0]) || isNaN(date[1]) || isNaN(date[2])) return false;
		
	date[0] = parseInt(date[0]); date[1] = parseInt(date[1]); date[2] = parseInt(date[2]);
	
	if(date[0] < 1 || date[1] < 1 || date[2] < 1) return false;
	
	if(date[2].toString().length <= 2 && date[2] < parseInt(new Date("yy"))) date[2] = 2000 + date[2];
	else if (date[2].toString().length <= 2) date[2] = 1900 + date[2];
	
	if(date[1] > 12 && date[0] <= 12) {
		t = date[1];
		date[1] = date[0];
		date[0] = t;	
	}
	
	daysinmonths = new Array(31,(((date[2] % 4 == 0) && ( (!(date[2] % 100 == 0)) || (date[2] % 400 == 0))) ? 29 : 28 ),31,30,31,30,31,31,30,31,30,31);
	if(date[1] > 12 || date[0] > daysinmonths[date[1]-1]) return false;
	
	if(date[0].toString().length == 1) date[0] = '0' + date[0].toString();
	if(date[1].toString().length == 1) date[1] = '0' + date[1].toString();
		
	date = date[2] + '-' + date[1] + '-' + date[0];
	
	real_date = new Date(date.replace('-', '/').replace('-', '/'));
	now = new Date();
		
	if(direction == -1) if(real_date > now) return false;
	else if (direction == 1) if(real_date < now) return false;
	
	return date;
}

var swfo;
function playProductVideo(url, width, height){
	if($('videoWin')){
		try{
		swfo.remote('load', url, true);
		}catch(ex){}
		return false;
	}

	var holder = new Element('div', {
		'id': 'videoWin',
		'styles': {
			'display': 'none',
			'font-size': '20px',
			'color': '#c16dc5',
			'width': width,
			'height': height+24,
			'position': 'absolute',
			'background': '#000'
		}
	}).inject(document.body);

	holder.showModal(true);

	var createFlash = function(){
		swfo = new Swiff(rooturl+'webapp/shared/javascript/resources/movieplayer.swf', {
			'container': holder,
			'width': width,
			'height': height,
			'params': {
				'wmode': 'transparent',
				'allowFullScreen': 'true'
			},
			'vars': {
				'url': url,
				'statusbarcolor': '444444',
				'backgroundcolor': '000000',
				'autostart': true
			}
		});


		new Element('div', {
			'styles': {
				'width': 16,
				'height': 16,
				'float': 'right',
				'cursor': 'pointer',
				'background': 'url('+rooturl+'webapp/shared/icon/?cancel)'
			},
			'events': {
				'click': function(){
					hideModal(holder);
					var deleteme = function(){
						holder.destroy();
					};
					deleteme.delay(800);
				}
			}
		}).inject(holder, 'top');
	};
	
	createFlash.delay(800,this);

	return false;
}

function setupTabPods(){
	var etabPods = $$('.tabPod');
	$each(etabPods, function(pod){
		var eLIs = pod.getElement('ul').getElements('li');
		var ePages = pod.getElement('.podBody').getElements('.podPage');
		
		$each(eLIs, function(li, i){
			li.setStyle('cursor', 'pointer');
			li.getElement('a').addEvent('click', function(e){e.preventDefault()});
			li.addEvent('click', function(e){
				$each(eLIs, function(listitem, j){
					if(i == j) listitem.addClass('selected');
					else listitem.removeClass('selected');
				});
				
				$each(ePages, function(page, j){
					page.setStyle('display', i == j ? 'block' : 'none');
				});
			});
		});
		
		eLIs[0].fireEvent('click');
	});	
}

function setupRotatingBackgrounds(){
	var eImages = $('rotatingBackgrounds');
	var eLinks = $('topBar_Products_SelectionBox');
	
	if(!eImages || !eLinks) return;
	
	eImages = eImages.getElements('img');
	eLinks = eLinks.getElements('a');
	
	$each(eImages, function(img){
		img.setStyle('opacity', 0);
		img.get('morph').addEvents({
			'onStart': function(e){
				if(e.getStyle('opacity') == 0) e.setStyle('display', 'block');
			},
			'onComplete': function(e){
				if(e.getStyle('opacity') == 0) e.setStyle('display', 'none');
			}
		});
	});
	
	$each(eLinks, function(eLink, i){
		eLink.addEvent('click', function(e){
			e.preventDefault();
			curRotation = i;
			$each(eLinks, function(eLink2, k){
				if(i == k) eLink2.addClass('currentSelection');
				else eLink2.removeClass('currentSelection');
			});
			$each(eImages, function(img, j){
				img.morph({'opacity': i == j ? 1 : 0});
			});
		});
	});
		
	eLinks[0].fireEvent('click', {'preventDefault': function(){}});
	
	curRotation = 1;
	
	if(eImages.length == 1) return;
	
	autoRotate.periodical(5000); //time between image switches in milliseconds
}

function autoRotate(){
	var eLinks = $('topBar_Products_SelectionBox').getElements('a');
	
	eLinks[curRotation].fireEvent('click', {'preventDefault': function(){}});
	
	curRotation++;
	if(curRotation > eLinks.length - 1) curRotation = 0;
}

var curRotation = 0;

window.addEvent('domready', function(){
	setupRotatingBackgrounds();
	setupTabPods();
	if($('loginholder'))
		$('loginholder').showModal(true);
});

