var win = null;
function NewWindow(mypage,myname,w,h,scroll,r){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+r+'status=yes'
	win = window.open(mypage,myname,settings)
}
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") {
			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;
			   };
			}
		}
	}
}

/* add an event listener*/
function addEvent(target, eventType, functionRef, capture) {
  if (typeof target.addEventListener != "undefined") {
    target.addEventListener(eventType, functionRef, capture);
  } else if (typeof target.attachEvent != "undefined") {
    target.attachEvent("on" + eventType, functionRef);
  } else {
    eventType = "on" + eventType;
    if (typeof target[eventType] == "function") {
      var oldListener = target[eventType];
      target[eventType] = function() {
        oldListener();
        return functionRef();
      }
    } else {
      target[eventType] = functionRef;
    }
  }
  return true;
}

/* stack load functions */
function addLoadListener(fn) {
  if (typeof window.addEventListener != 'undefined') {
    window.addEventListener('load', fn, false);
  } else if (typeof document.addEventListener != 'undefined') {
    document.addEventListener('load', fn, false);
  } else if (typeof window.attachEvent != 'undefined'){
    window.attachEvent('onload', fn);
  } else {
    var oldfn = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = fn;
    } else {
      window.onload = function() {
        oldfn();
        fn();
      };
    }
  }
}

/* stop an event's default action */
function stopDefaultAction(event) {
  event.returnValue = false;
  if (typeof event.preventDefault != "undefined") {
    event.preventDefault();
  }
  return true;
}

var filter = /\d{0,3}[^\D]/;
var fieldName = /addquantity/;
function validateNum(theForm) {
	if(!document.getElementById) return false;
	var numBoxes = theForm.getElementsByTagName("input");
	for (var i=0; i<=numBoxes.length; i++) {
		var numBox = numBoxes[i];
		if (numBox.name.match(fieldName) && numBox.value != "1" && numBox.value != numBox.value.match(filter)) {
			alert("Please enter a number.");
			numBox.select();	
			return false;
		}	
	}
	return true; 	
}

function wipe() {
if (!document.getElementsByTagName) return;
var textboxes = document.getElementsByTagName('input');
	for (var i=0; i<textboxes.length; i++) {
		var textbox = textboxes[i];
		textbox.onfocus = function() {if (this.value == '1') this.value = '';};	
	}
}

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/

function checkmail(){
var theForm = document.getElementById("friendForm");
var email_1 = theForm.email_1.value;
var email_2 = theForm.email_2.value;
var email_3 = theForm.email_3.value;
var email_4 = theForm.email_4.value;
var email_5 = theForm.email_5.value;
var val_1=emailfilter.test(email_1);
var val_2=emailfilter.test(email_2);
var val_3=emailfilter.test(email_3);
var val_4=emailfilter.test(email_4);
var val_5=emailfilter.test(email_5);
	if (val_1==false){
		alert("Please enter a valid email address.")
		theForm.email_1.select()
		return false;
	}
	if (val_2==false){
		alert("Please enter a valid email address.")
		theForm.email_2.select()
		return false;
	}
	if (val_3==false){
		alert("Please enter a valid email address.")
		theForm.email_3.select()
		return false;
	}
	if (val_4==false){
		alert("Please enter a valid email address.")
		theForm.email_4.select()
		return false;
	}
	if (val_5==false){
		alert("Please enter a valid email address.")
		theForm.email_5.select()
		return false;
	}
	if (email_1 == email_2 || email_1 == email_3 || email_1 == email_4 || email_1 == email_5
		|| email_2 == email_3 || email_2 == email_4 || email_2 == email_5
		|| email_3 == email_4 || email_3 == email_5 || email_4 == email_5) {
		alert("Please enter only unique email addresses.")
		return false;
	}
	return true;
}

function confirmDelete(str) {
	if(confirm('Are you sure you want to delete\n- '+str)) return true;
	else return false;
}

addLoadListener(windowLinks);
addLoadListener(wipe);