function equalizeHeight(one,two) {
	if (document.getElementById(one) && document.getElementById(two)) {
		var lh=document.getElementById(one).offsetHeight;
		var rh=document.getElementById(two).offsetHeight;
		var nh = Math.max(lh, rh);
		document.getElementById(one).style.height=nh+"px";
		document.getElementById(two).style.height=nh+"px";
	}
}

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
function getIEVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

///////////////////////////////////////////////////////////////////////////////
// proof form validation
///////////////////////////////////////////////////////////////////////////////

function validateProofForm(theform) {
	var prefix = "";
	var fieldsets = document.getElementsByTagName("fieldset");
	for (var i=0; i<fieldsets.length; i++) {
		prefix = fieldsets[i].id;
		if (validateProofSection(theform,prefix)) {
			continue;
		}
		return false;
	}
	return true;
}

function validateProofSection(thisform,section) {
	yesButton = document.getElementById(section + "-yes");
	noButton = document.getElementById(section + "-no");
	if (yesButton.checked || noButton.checked) {
		if (noButton.checked) {
			var instructions = document.getElementById(section + "-instructions"); 
			if (instructions.value=="") {
				alert("Please specify a reason for selecting Not Approved");
				markField(instructions);
				goto(section);
				return false;
			}
		}
	} else {
		alert("You must select Approved or Not Approved");
		markField(document.getElementById(section + "-radiogroup"));
		goto(section);
		return false;
	}
	return true;
}

///////////////////////////////////////////////////////////////////////////////
// common form functions
///////////////////////////////////////////////////////////////////////////////

function markField(element,clearValue) {
	element.className += " invalidField";
	if (clearValue==undefined) {
		element.value = "";
	}
	element.focus();
}

function goto(x){
	window.location.hash=x;
} 

///////////////////////////////////////////////////////////////////////////////
// request form validation
///////////////////////////////////////////////////////////////////////////////

function validateRequestForm(form) {
	var fn = form.elements['firstname'];
	var ln = form.elements['lastname'];
	var em = form.elements['email'];
	var co = form.elements['company'];
	var pw1 = form.elements['password'];
	var pw2 = form.elements['confirm'];
	var ac = form.elements['account'];
	var or = form.elements['order'];
	
	if (fn.value == '') {
		alert('Sorry, please enter your first name.');
		markField(fn);
		return false;
	}
	if (ln.value == '') {
		alert('Sorry, please enter your last name.');
		markField(ln);
		return false;
	}
	if (em.value == '') {
		alert('Sorry, please enter your email address.');
		markField(em);
		return false;
	}
	if (co.value == '') {
		alert('Sorry, please enter your company\'s name.');
		markField(co);
		return false;
	}

	if (!validatePasswordFields(pw1,pw2)) {
		return false;
	}

	if (ac.value == '' && or.value == '') {
		alert('Sorry, please enter either your account # or a previous order #.');
		markField(ac);
		markField(or);
		return false;
	}

	return true;	

}

function resetRequestForm(form) {
	form.elements['firstname'].className = "field";
	form.elements['lastname'].className = "field";
	form.elements['email'].className = "field";
	form.elements['company'].className = "field";
	form.elements['password'].className = "field";
	form.elements['confirm'].className = "field";
	form.elements['account'].className = "field";
	form.elements['order'].className = "field";
	return true;
}

///////////////////////////////////////////////////////////////////////////////
// search form validation
///////////////////////////////////////////////////////////////////////////////

function validateSearchForm(form) {
	return true;
}

///////////////////////////////////////////////////////////////////////////////
// password form validation
///////////////////////////////////////////////////////////////////////////////

function validatePasswordFields(pw1,pw2) {
	var minLength = 6; // Minimum length
	// check for a value in both fields.
	if (pw1.value == '' || pw2.value == '') {
		alert('Sorry, please enter your new password twice.');
		markField(pw1);
		markField(pw2);
		return false;
	}

	// check for minimum length
	if (pw1.value.length < minLength) {
		alert('Sorry, your password must be at least ' + minLength + ' characters long. Please try again.');
		markField(pw1);
		markField(pw2);
		return false;
	}

	// password cannot contain whitespace 
	var reInvalid = /^.*\s+.*$/;
	if (reInvalid.test(pw1.value)) { 
		alert("Sorry, your password cannot contain spaces or other whitespace.");
		markField(pw1);
		markField(pw2);
		return false;
	}
		
	if(pw1.value != pw2.value) {
		markField(pw1);
		markField(pw2);
		alert('Sorry, your new password does not match the confirmation. Please try again.');
		return false;
	} 
	return true;
}

function validatePasswordForm(form) {
	var old = form.elements['oldpass'];
	var pw1 = form.elements['newpass'];
	var pw2 = form.elements['confirm'];
	
	if (old.value == '') {
	alert('Sorry, pease enter your old password.');
		markField(old);
		return false;
	}

	if (!validatePasswordFields(pw1,pw2)) {
		return false;
	}

	return true;
}

function resetPasswordForm(form) {
	var e = form.elements;
	e['newpass'].className = "field"; 
	e['confirm'].className = "field"; 
	return true;
}

///////////////////////////////////////////////////////////////////////////////
// image resize functions
///////////////////////////////////////////////////////////////////////////////

function constrain(el) {
	var maxHeight = 100;
	var maxWidth = 300;
	var h=el.height;
	var w=el.width;
	var horizRatio = maxWidth/w;
	var vertRatio = maxHeight/h;
	var ratio=0;
	if (horizRatio < vertRatio) {
		ratio=horizRatio;
	} else {
		ratio=vertRatio;
	}
	if (ratio < 1) {
		nw = w*ratio;
		nh = h*ratio;
		el.style.width = nw+"px";
		el.style.height = nh+"px";
	}
}

function constrainAll(strClassName) {
    var aryClassElements = getElementsByClassName( strClassName, document.body );
    for ( var i = 0; i < aryClassElements.length; i++ ) {
    	//aryClassElements[i].className = 'youFoundMe';
        constrain(aryClassElements[i]);
    }
}

function getElementsByClassName( strClassName, obj ) {
    var ar = arguments[2] || new Array();
    var re = new RegExp("\\b" + strClassName + "\\b", "g");
    if ( re.test(obj.className) ) {
        ar.push( obj );
    }
    for ( var i = 0; i < obj.childNodes.length; i++ )
        getElementsByClassName( strClassName, obj.childNodes[i], ar );
    return ar;
}

///////////////////////////////////////////////////////////////////////////////
// print view functions
///////////////////////////////////////////////////////////////////////////////

function popupPrintView() {
	newwindow2 = window.open('','win1','status=no,menubar=yes,toolbar=yes,scrollbars=yes,titlebar=no,resizable=yes,width=800,height=600');
	var tmp = newwindow2.document;
	tmp.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'); 
	tmp.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
	tmp.write('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Indyweb</title>');
	tmp.write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
	tmp.write('<link href="images/indystyle.css" rel="stylesheet" type="text/css" />');
	tmp.write('<script src="images/indyscript.js" type="text/javascript"></script>');
	//tmp.write('</head><body id="print" onload="disableAllForms();">');
	tmp.write('</head><body id="print">');
	tmp.write('<div class="clickcover"><img id="clickblock" src="images/clear.gif" border="0" ');
	tmp.write('height="1" width="1" /></div>');
	tmp.write('<div class="indyprint"> ');
	strHTML = getInnerHTML('indycontent');
	tmp.write(strHTML);
	tmp.write('</div></body></html>');	
	tmp.close();
	if (window.focus) {newwindow2.focus()}
	return false;
}

function getInnerHTML(id) {
	if (navigator.userAgent.indexOf("Opera")!=-1
    	&& document.getElementById) type="OP";
	if (document.all) type="IE";
	if (document.layers) type="NN";
	if (!document.all && document.getElementById) type="MO";
	
	// change the content
	if (type=="IE") {
		return document.all[id].innerHTML;
	}
	if (type=="NN") {
		// sorry, don't know this one
	}
	if (type=="MO" || type=="OP") {
		el = document.getElementById(id);
		return el.innerHTML;
	}
}

function disableAllForms(){
	//disableAllSubmit();
	var el = document.forms[0].elements;
	for(var i=0;i<el.length;i++){
		el[i].setAttribute('disabled',true)
	}
}

function disableAllSubmit(){
	var f = document.getElementsByTagName('input');
	for(var i=0;i<f.length;i++){
		if(f[i].getAttribute('type')=='submit'){
			f[i].setAttribute('disabled',true)
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// print view functions
///////////////////////////////////////////////////////////////////////////////


