function NewWindow(mypage,myname,w,h,s,r){
	var win = null;
	var LeftPos = (screen.width) ? (screen.width-w)/2 : 0;
	var TopPos = (screen.height) ? (screen.height-h)/2 : 0;
	var settings = 'height='+h+',width='+w+',top='+TopPos+',left='+LeftPos+',scrollbars='+s+',resizable='+r+',status=yes';
	win = window.open(mypage,myname,settings);
}

function windowLinks() {
  // init valid popup and external windows
  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;
		};
	  }
	}
  }
}

function bookmarkpage(title, url){
	if (document.all) {
		window.external.AddFavorite(url, title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(title, url, '');
	}
}

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';
	}
}

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;
	}
}

function toggleSections() {
  var sections = getElementsByAttribute('class','barsection');
  var len = sections.length;
  for (var i=0; i<len; i++) {
	var a = document.createElement('a');
	var img = document.createElement('img');
	var h1 = sections[i].parentNode.getElementsByTagName('h1')[0];
	var vis = null;
	img.alt = 'Close Section';
	a.className = 'toggle';
	a.s = sections[i];	
	a.s.id = 'tab' + i + 'Vis';
	a.href = 'javascript:;';
	a.title = h1.firstChild.nodeValue;
	a.onclick = function() {
	  var a = this;
	  setTimeout(function() {
	    var img = a.getElementsByTagName('img')[0];		
	    img.src = img.src.indexOf('minus.gif') != -1 ? 'assets/images/plus.gif' : 'assets/images/minus.gif';
	    img.alt = img.alt.indexOf('Close Section') != -1 ? 'Open Section' : 'Close Section';
	    if (a.s.style.display == '' || a.s.style.display == 'none') {
		  a.s.style.display = 'block';
		  a.s.parentNode.style.padding = '0px 0px 5px 0px';
		} else if (a.s.style.display == 'block') {
		  a.s.style.display = 'none';			
		  a.s.parentNode.style.padding = '0px';		  
		}
		createCookie('tab' + a.s.id + 'Vis', a.s.style.display, 1);	
	  }, 50);
	};
	h1.parentNode.insertBefore(a, h1);
	h1.insertBefore(img, h1.firstChild);
	a.appendChild(h1);
    vis = readCookie('tab' + a.s.id + 'Vis');
	if (/block/.test(a.s.className)) {
	  a.s.parentNode.style.padding = '0px 0px 5px 0px';
	  a.s.style.display = 'block';
	  img.src = 'assets/images/minus.gif';
	  img.alt = 'Close Section';	  
	} else if (!vis || vis == 'none') {
	  a.s.parentNode.style.padding = '0px';		
	  a.s.style.display = 'none';
	  img.src = 'assets/images/plus.gif';
	  img.alt = 'Open Section';
    } else {
	  a.s.parentNode.style.padding = '0px 0px 5px 0px';		
	  a.s.style.display = 'block';		
	  img.src = 'assets/images/minus.gif';
	  img.alt = 'Close Section';	  
	}
  }
}

function initSetSameAddress() {
	// init the function for copying the billing address data to the delivery address fields
	if(!document.getElementById('setsame')) {return;}
	// wait for the button to be clicked
	addEvent(document.getElementById('setsame'), 'click', setSameAddress, false);
	var fields = new Array('con_address','con_suburb','con_postcode','con_state','con_country');
	for (var i=0; i<fields.length; i++) {
		var delField = gebi('con_del_'+fields[i].split('_')[1]);
		var field = gebi(fields[i]);
		if (field.type == 'text' || field.type == 'textarea') {
			delField.value = delField.value.replace(/^\s*$/gm, '');
			field.value = field.value.replace(/^\s*$/gm, '');
		}
	}
	return true;
}
function setSameAddress(e) {
	var target = window.event ? window.event.srcElement : e ? e.target : null;
	// these are all the billing fields that should be copied
	var fields = new Array('con_address','con_suburb','con_postcode','con_state','con_country');
	if (target.value.indexOf('Copy') != -1) {
		for (var i=0; i<fields.length; i++) {
			// loop the fields and get the equivilent delivery fields as well as the delivery error spans 
			// that have been created for validation. We also need the field itself to copy the data from
			var delField = gebi('con_del_'+fields[i].split('_')[1]);
			var errorField = gebi('error_con_'+fields[i].split('_')[1]);
			var field = gebi(fields[i]);
			// the field is a dropdown 
			if(field.type == 'select-one') {	
				field.selectedIndex = delField.selectedIndex;	
				// copy the current selcted and if it is '' then show the error
				if (field.options[field.selectedIndex].value == ''){
					errorField.innerHTML = validationSet[field.name]['error'];
					field.className = 'fielderror';
				} else {
					errorField.innerHTML = '';
					field.className = 'textfield';		
				}	
			// for fields that are input and texteareas
			} else if (field.type == 'text' || field.type == 'textarea') {
				field.value = delField.value;
				// copy in the value and make sure that the regex matches otherwise show the error
				if (!field.value.match(validationSet[field.name]['regexp'])) {
					errorField.innerHTML = validationSet[field.name]['error'];
					field.className = 'fielderror';
				} else {
					errorField.innerHTML = '';
					field.className = 'textfield';
				}					
			}
		}
	}
	return false;
}

function initCheckEmailExists() {
	if(!document.getElementById('checkexists')) {return;}
	var checkExistsButton = document.getElementById('checkexists');
	checkExistsButton.onclick = function() {
		var ajax = new Ajax();
		var email = gebi('con_email').value;
		if (!email.match(/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,5}|\d+)$/)) {
			alert('Please enter a valid email address');
		} else {
			ajax.doGet('modules/ajax/ajax_get.cfm?action=checkemail&email='+email, function(str){alert(str);});
		}
		return false;
	};
	return true;
}

function toggleButton() {
	if (document.getElementById('togglebtn')) {
		var tb = document.getElementById('togglebtn');
		var as = document.getElementById('advancedsearch');
		var vis = readCookie('advancedSearchVis');
		as.style.display = vis == null ? 'none' : vis;
		tb.onclick = function() {
		  expandCollapse('advancedsearch');
		  createCookie('advancedSearchVis', as.style.display, 1);
		  return false;
		};
	}
}

function cancelButton() {
	var cancelButton,url;
	if (gebi('accountcancel')) {
		cancelButton = gebi('accountcancel');
		url = 'my_account.cfm?status=cancelled';
	} else if (gebi('checkoutcancel')) {
		cancelButton = gebi('checkoutcancel');	
		url = 'shopping_cart.cfm?status=cancelled';
	} else if (gebi('newslettercancel')) {
		cancelButton = gebi('newslettercancel');	
		url = 'newsletter.cfm?status=cancelled';
	} else {
		return;
	}
	cancelButton.onclick = function() {
		window.location.href = url;
		return false;
	};
	return true;
}

function validateNL() {
  if (document.getElementById('nlform')) {
	var f = document.getElementById('nlform');
	var ans = f.action.substring(f.action.indexOf('a=')+2, f.action.length);
	var vo = {'Newsletter Name':/[^\s]+/,'Newsletter Email':/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,5}|\d+)$/,'Newsletter State':/ /,'Answer':ans};
	f.onsubmit = function() {
	  var m = 'The fields below contain errors, please fix these and try again:\n';
	  var l = m.length;
	  for (var i in vo) {
	    if (f.elements[i]) {
		  field = f.elements[i];
		  if ((field.type == 'text' && !field.value.match(vo[i])) || (field.type == 'select-one' && field.options[field.selectedIndex].value == '')) {
			m += '\n- ' + i;  
		  }
	    }
	  }
	  if (m.length > l) {
		alert(m);
		return false;  
	  }
	};
  }
}

addLoadListener(toggleButton);
addLoadListener(windowLinks);
addLoadListener(toggleSections);
addLoadListener(initSetSameAddress);
addLoadListener(initCheckEmailExists);
addLoadListener(validateNL);
addLoadListener(cancelButton);
addLoadListener(function () {document.oncontextmenu = new Function("return false;");});
/*
function disableselect(e) {var omitformtags=["input", "textarea", "select"].join("|"); if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1) {return false;}}
function reEnable() {return true;} if (typeof document.onselectstart!="undefined") {document.onselectstart=new Function ("return false");} else {document.onmousedown=disableselect;document.onmouseup=reEnable;} */