/* Function to deal with external links for XHTML Strict Compliance */
function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 
window.onload = externalLinks;


// general onload function(s)
// not used
function jsInit() {

	if (address.same_as_billing.defaultChecked) {
		toggleContent(document.address, 'shipping');
	}

}


// Error Handling
var error = new Array();
var field = new Array();

function pushErr(err,fld) {
	error[error.length] = err;
	field[field.length] = fld;
	return;
}

function clearErr(form) {

	for (i=0;i<form.elements.length;i++) {
		form.elements[i].style.backgroundColor='#ffffff';
	}

}

function showErr() {

	if (arguments.length > 0) str = arguments[0] + "\n";
	else str = '';

	if (arguments.length > 1) color = arguments[1];
	else color = '#ffaabb';

	if (error.length == 0) return true;
	
	for(i=0;i<error.length;i++) {
		str += error[i] + "\n";
	}
	alert(str);

	for(i=0;i<field.length;i++) {
		field[i].style.backgroundColor=color;
	}

	field[0].focus();
	error = new Array();
	field = new Array();
	return false;
}

function setMsg(msg) {
	window.status = msg
	return true
	}

// Opens Thawte Certificate
function OpenCertDetails()
	{
	thewindow = window.open('https://www.thawte.com/cgi/server/certdetails.exe?code=USNEWE41-1', 'anew', config='height=400,width=450,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=yes');
	}

// function that opens window for help
function newLocation(i) {
	newWindow = window.open	(i,'newWin','toolbar=no,location=no,scrollbars=no,resizable=yes,width=250,height=300')
	newWindow.document.close()
	newWindow.focus()	
}


function newWindow(i) {
	leftPos=0
	topPos=0
	if (screen) {
		leftPos = screen.width/2-150
		topPos = screen.height/2-100
		}

	editWindow = window.open	(i,'editWin4','toolbar=no,location=no,scrollbars=no,resizable=yes,width=300,height=300,left='+leftPos+',top='+topPos+'')
	editWindow.document.close()
	editWindow.focus()	
}


function editModule(i) {
	leftPos=0
	topPos=0
	if (screen) {
		leftPos = screen.width/2-255
		topPos = screen.height/2-250
		}

	editWindow = window.open	(i,'editWin5','toolbar=no,location=no,scrollbars=yes,resizable=yes,width=510,height=500,left='+leftPos+',top='+topPos+'')
	editWindow.document.close()
	editWindow.focus()	
}


function isEmpty(inputVal) {
	if (inputVal == null || inputVal == "") {
		return true
		}
	return false
}


function isPosInteger(inputVal) {
	if (isEmpty(inputVal)) {
		return false
		}
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

function isInteger(inputVal) {
	if (isEmpty(inputVal)) {
		return false
		}
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			if (i==0 && oneChar=='-') {
				continue;
			}
			return false
		}
	}
	return true
}



function isNumber(inputVal) {
	if (isEmpty(inputVal)) {
		return false
		}
	oneDecimal = false
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}


function inRange(inputStr,i,j) {
	if (!isInteger(inputStr)) {
		return false
		}
	num = parseInt(inputStr)
	if (num < i || num > j) {
		return false
	}
	return true
}


function isChecked(field) {
	j = -1
	for (var i = 0; i < field.length; i++) {
		if (field[i].checked) {
			j = i
			}
		}
		if (j == -1) {
		return false
	}
	return true
}


function validEmail(email,require) {
	if (arguments.length < 2) {
		var require = true;
	}
	invalidChars = " /:,;"
	if (require == true && email == "") {
		return false
	} else if (require == false && email == "") {
		return true
	}
	for (i=0; i<invalidChars.length;i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
		atPos = email.indexOf("@",1)
		if (atPos == -1) {
			return false
		}
	}
	if (email.indexOf("@",atPos+1) > -1) {
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length) {
		return false
		}
	return true
}

function validFile(fileName) {
	invalidChars = "!@#$%^&*()\\+={}[]\"\'<>?`\| /:,;"
	if (fileName == "") {
		return false
	}
	for (i=0; i<invalidChars.length;i++) {
		badChar = invalidChars.charAt(i)
		if (fileName.indexOf(badChar,0) > -1) {
			return false
		}
	}
	periodPos = fileName.indexOf(".",1)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+4 > fileName.length) {
		return false
		}
	return true
}

function minLen(str,i) {
	if (str.length < i) {
		return false
		}
	return true
}

function isHexColor(inputVal) {
	inputStr = inputVal.toString()
	if (inputStr.length != 6 && inputStr.length != 0) {
		return false
		}
	var j = -1
	for (var i = 0; i < inputStr.length; i++) {
		var c = inputStr.charAt(i)
		if (!( ((c >= "a") && (c <= "f")) || ((c >= "A") && (c <= "F")) || ((c >= "0") && (c <= "9")) )) {
		j += 1
		break
		}
	}
	if (j > -1) {
		return false
		}
	return true
}

function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return false;
       else return (isYear.arguments[1] == true);
    if (!isPosInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return false;
       else return (isMonth.arguments[1] == true);
    return inRange (s, 1, 12);
}

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return false;
       else return (isDay.arguments[1] == true);   
    return inRange (s, 1, 31);
}

function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

var daysInMonth = makeArray(12);
	daysInMonth[1] = 31;
	daysInMonth[2] = 29;   // must programmatically check this
	daysInMonth[3] = 31;
	daysInMonth[4] = 30;
	daysInMonth[5] = 31;
	daysInMonth[6] = 30;
	daysInMonth[7] = 31;
	daysInMonth[8] = 31;
	daysInMonth[9] = 30;
	daysInMonth[10] = 31;
	daysInMonth[11] = 30;
	daysInMonth[12] = 31;

function daysInFebruary (year)
{   return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isDate (year, month, day)
{   if (! (isYear(year) && isMonth(month) && isDay(day))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


// allows toggling of links
// <a href="index.html" onclick="toggle(document.getElementById('someID')); return false;">
function toggle(obj) {
	var el = document.getElementById(obj);
	if (el.style.display != 'none') {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

// used in address.class.php to hide shipping form
function toggleContent(box, div) {
	if(box.checked == true){
		document.getElementById(div).style.display = "none";
	} else {
		document.getElementById(div).style.display = "";
	}
}

function toggleAllCheckbox(theElement, theName) {
	theForm = theElement.form;
	z = 0;
	s = theName.length;
	for(z=0; z<theForm.length;z++){
		subName = theForm[z].name.substring(0,s);
		if(theForm[z].type == 'checkbox' && theName != theElement.name && subName == theName){
			theForm[z].checked = theElement.checked;
		}
	}
}

function hideLink(divID) { 
	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(divID).style.style.display = 'none'; 
	} else { 
	if (document.layers) { // Netscape 4 
		document.divID.style.display = 'none'; 
	} else { 
	document.all.divID.style.display = 'none'; 
	} 
	} 
}
function showLink(divID) { 
	if (document.getElementById) {
		document.getElementById(divID).style.display = ""; 
	} else { 
		if (document.layers) { 
			document.divID.style.display = ""; 
		} else { 
			document.all.divID.style.display = ""; 
		} 
	} 
} 

	

//	function custWindow(doc, w, h [,align, valign [, scroll] ] )
//
//	function that opens window with size and optional location and scroll input
//
//	arguments:
//	'doc'		is the document that will load in the new window
//	'w'			is the width of the window in pixels
//	'h'			is the height of the window in pixels
//	'align'		is the horizontal alignment (l, c, or r)
//	'valign'	is the vertical alignment (t, m, or b)
//	'scroll'	indicates whether or not to display scrollbars (yes or no)

function custWindow(doc,wi,hi,align,valign,scroll) {
	if (arguments.length < 6) {
		var scroll = 'yes'
		}
	if (arguments.length < 4) {
		var align = 'c'
		var valign = 'm'
		}
	topPos=0
	leftPos=0
	if (screen) {
		if (align == 'c') {
			leftPos = screen.width/2 - wi/2
			} else {
			if (align == 'r') {
				leftPos = screen.width - wi - 12
				}
			}
		if (valign == 'm') {
			topPos = screen.height/2 - hi/2
			} else {
			if (valign == 'b') {
				topPos = screen.height - hi - 45
				}
			}
		}
	custWin = window.open (doc,'custWin','toolbar=no,location=no,scrollbars='+scroll+',resizable=yes,width='+wi+',height='+hi+',left='+leftPos+',top='+topPos+'')
	custWin.document.close()
	custWin.focus()	
}

stateArray = new Array("AA","AE","AL","AK","AP","AS","AZ","AR","CA","CO","CT","DE","DC","FM","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY")

function isState(inputVal) {
	if (isEmpty(inputVal) || inputVal.length > 2) {
		return false
		}
	inputStr = inputVal.toString()
	inputStr = inputStr.toUpperCase()

	for (i=0; i<stateArray.length; i++) {
		if (inputStr == stateArray[i]) {
			return true
		}
	}
	return false
}

function stripCharsNotInBag (s, bag)
{   var i;
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function stripNonDigits (s)
{   var i;
	var bag = '0123456789';
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (bag.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function isCreditCard(st) {
  // Strip non-digits
  st = stripNonDigits(st);

  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
	return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
	digit = st.substring(l-i-1,l-i);
	tproduct = parseInt(digit ,10)*mul;
	if (tproduct >= 10)
	  sum += (tproduct % 10) + 1;
	else
	  sum += tproduct;
	if (mul == 1)
	  mul++;
	else
	  mul--;
  }
  if ((sum % 10) == 0)
	return (true);
  else
	return (false);

}


function isVisa(cc)
{
  // Strip non-digits
  cc = stripNonDigits(cc);
  if (((cc.length == 16) || (cc.length == 13)) &&
	  (cc.substring(0,1) == 4))
	return isCreditCard(cc);
  return false;
}


function isMasterCard(cc)
{
  // Strip non-digits
  cc = stripNonDigits(cc);
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
	  ((seconddig >= 1) && (seconddig <= 5)))
	return isCreditCard(cc);
  return false;

}


function isAmericanExpress(cc)
{
  // Strip non-digits
  cc = stripNonDigits(cc);
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
	  ((seconddig == 4) || (seconddig == 7)))
	return isCreditCard(cc);
  return false;

}

function isDiscover(cc)
{
  // Strip non-digits
  cc = stripNonDigits(cc);
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
	return isCreditCard(cc);
  return false;
}

function isJCB(cc)
{
  // Strip non-digits
  cc = stripNonDigits(cc);
  firstdig = cc.substring(0,1);
  first4digs = cc.substring(0,4);
  if (     ((cc.length == 15) && ((first4digs == "2131") || (first4digs == "1800")))
	  || ((cc.length == 16) && (firstdig == 3))
	 )
	return isCreditCard(cc);
  return false;
}


function textareaGrow(obj)
{
	var height = document.getElementById(obj).style.height;
	height = height.replace("px","");
	height = parseInt(height) * 1.33;
	if(height>650) var height = 650;
	document.getElementById(obj).style.height = height+"px";
}

function textareaShrink(obj)
{
	var height = document.getElementById(obj).style.height;
	height = height.replace("px","");
	height = parseInt(height) * 0.66;
	if(height<50) var height = 50;
	document.getElementById(obj).style.height = height+"px";
}						


/* how did you hear about us? */
function showOther(heard_about) {
	var chosen = heard_about[heard_about.selectedIndex].value.toLowerCase();
	if (chosen == 'other') {
		document.getElementById('heard_about_other').style.visibility = 'visible';
	} else {
		document.getElementById('heard_about_other').style.visibility = 'hidden';
	}
}


/* wish list select all */
function selectAll(formname) {
	var FORM = document.forms[formname];
	for (var z=0; z < FORM.length; z++) {
		if (FORM[z].type == 'checkbox' && FORM[z].name != 'wish_list_access') {
			FORM[z].checked = true;
		}
	}
}
function check(FIELD) {
	// checks the check box for this field
	var fieldname = FIELD.name;
	var id = fieldname.replace(/[a-z_]/g, "");
	var FORM = FIELD.form;
	for (var z = 0; z < FORM.length; z++) {
		if (FORM[z].type == 'checkbox' && FORM[z].value == id) {
			FORM[z].checked = true;
			break;
		}
	}
}
