reqArray= new Array();

function unRequire(id, c) {
	if (c.options[c.selectedIndex].text=='United States' || c.options[c.selectedIndex].text=='Canada') {
		document.getElementById(id).style.visibility='visible';
	} else {
		document.getElementById(id).style.visibility='hidden';
	}
}

// Remove leading and trailing whitespace from a string
function trim(strData) {
	var WHITESPACE = " \t\n\r";

	// Return empty string if null or 0 length
	if ((strData == null) || (strData.length == 0)) {
		//alert("string null or 0 length");
		return ("");
	}

	// Get the index of the first non white space char
	var iFirstIndex = strData.length - 1;
	for(var i = 0; i < strData.length; i++) {
		if(WHITESPACE.indexOf(strData.charAt(i)) == -1) {
			iFirstIndex = i;
			break;
		}
	}

	// Get the index of the last non white space char
	var iLastIndex = strData.length - 1
	for(var i = strData.length - 1; i >= 0; i--) {
		if(WHITESPACE.indexOf(strData.charAt(i)) == -1) {
			iLastIndex = i;
			break;
		}
	}

	return (strData.substr(iFirstIndex, iLastIndex - iFirstIndex + 1));
}

// Form Validation
// requires a set of fields called 'required', one for each required field
// the visible fields are all prefixd by 'q'
// requires a set of fields called 'ql' (as in question label) and then the name or number of the visible field
// <input type=hidden name="required" value="13">
// <input type=text name="q13">
// <input type=hidden name="ql13" value="What color is your car?">

function verifyForm (frm) {
	if (document.pressed && document.pressed=='login')
	{
		frm.action='/app/login/dologin.asp';
		frm.elements['email1']=frm.elements['qemail'];
		frm.elements['qemail'].name='email1';
		document.pressed='';
		if (frm.elements['email1'].value=='')
		{
			alert('Please enter an e-mail address.');
			frm.elements['qemail'].focus();
			return false;
		}
		if (frm.elements['password'].value=='')
		{
			alert('Please enter your password.');
			frm.elements['password'].focus();
			return false;
		}
		frm.elements['action'].value='Sign In';
		return true;
	}
	for (x =0; x < reqArray.length; x++) {
		f=reqArray[x];
		var elementName;
		if (frm.elements['q'+f+'[]']) {
			elementName='q'+f+'[]';
		} else {
			elementName='q'+f;
		}
		if (frm.elements[elementName].tagName=='SELECT') {
			if (frm.elements[elementName].selectedIndex==0 || frm.elements[elementName].selectedIndex==-1) {
				if (elementName.indexOf('qcompany') > -1) {
					// company question
					if (frm.elements['qcompanyOther'].value=='') {
						alert ('The question "'+frm.elements['ql'+f].value+'" is required.');
						return false;
					}
				} else if (elementName.indexOf('state') == -1 || ! frm.elements[elementName.replace('state','province')]) {
					// this isn't the state question
					alert('The question "'+frm.elements['ql'+f].value+'" is required.');
					frm.elements[elementName].focus();
					return false;
				}
				else {
					// Check the province field && the country element
					c= frm.elements[elementName.replace('state', 'country')];
					if (frm.elements[elementName.replace('state','province')].value == '' && (c.options[c.selectedIndex].text=='United States' || c.options[c.selectedIndex].text=='Canada')) {
						alert('The question "'+frm.elements['ql'+f].value+'" is required.');
						frm.elements[elementName].focus();
						return false;
					}
				}
			}
		} else if (frm.elements[elementName].type=='text' || frm.elements[elementName].tagName=='TEXTAREA') {
			if (frm.elements[elementName].value=='') {
				alert('The question "'+frm.elements['ql'+f].value+'" is required.');
				frm.elements[elementName].focus();
				return false;
			}
		} else if (frm.elements[elementName].type=='checkbox') {
			if (! frm.elements[elementName].checked) { 
				alert('The question "'+frm.elements['ql'+f].value+'" is required.');
				frm.elements[elementName].focus();
				return false;
			}
		} else if (frm.elements[elementName][0].type=='radio' || frm.elements[elementName][0].type=='checkbox') {
			var ok=false;
			for (y=0; y< frm.elements[elementName].length; y++) {
				if (frm.elements[elementName][y].checked) { ok=true; }
			}
			if (!ok) {
				alert('The question "'+frm.elements['ql'+f].value+'" is required.');
				frm.elements[elementName][0].focus();
				return false;
			}
		} 
	}
	if (! frm.elements['maxChoices[]'])
	{
		return true;
	}
	for (x =0; x < frm.elements['maxChoices[]'].length; x++) { 
		var count=0;
		var m = frm.elements['maxChoices[]'][x].value;
		if (frm.elements['q'+m+'[]'].tagName=='SELECT') {
			// count the number of selected options
			for (y=0; y < frm.elements['q'+m+'[]'].options.length; y++ ) {
				if (frm.elements['q'+m+'[]'].options[y].selected) { count++; }
			}
		} else {
			// count the number of checked boxes
			for (y=0; y < frm.elements['q'+m+'[]'].length; y++) {
				if (frm.elements['q'+m+'[]'][y].checked == true) { count++; };
			}
		}
		if (count > frm.elements['maxChoices'+m].value) { 
			alert('Please select no more than '+frm.elements['maxChoices'+m].value+' choices for the question:\n"'+frm.elements['ql'+m].value+'"'); 
			return false;
		}
	}
	return true;
}
function isEmpty(s){
	s = trim(s);
	if(s=="") return true;
	return false;
}
function notEmpty(s){
	return !isEmpty(s);
}

function makeRequired (ary) {
	for (x=0; x<ary.length ; x++)
	{
	reqArray.push(ary[x]);
	}
}

function makeOptional (ary) {
	newReqArray = new Array();
	for (x=0; x< reqArray.length; x++) {
		required=true;
		for (y=0; y < ary.length; y++) {
			if (reqArray[x]==ary[y]) {
				required=false;
				break;
			}
		}
		if (required) { newReqArray.push(reqArray[x]); }
	}
	reqArray=newReqArray;
}

function getRequiredArray (frm) {
	var reqlength;
	if (isNaN(frm.elements['required[]'].length)) {
		reqlength=1;
	} else {
		reqlength=frm.elements['required[]'].length;
	}
	for (x =0; x < reqlength; x++) {
		if (reqlength == 1) { f = frm.elements['required[]'].value; }
		else {
			f = frm.elements['required[]'][x].value;
		}
		reqArray[x]=f;
	}
}

function enableAddresses (frm, company) {
	if (company=='') {
		frm.elements['qBillingSameAs'].style.visibility='visible';
		swapAddressDivsBilling('New');
	} else {
		frm.elements['qBillingSameAs'].style.visibility='hidden';
		/*optionList = frm.elements['qBillingSameAs'].options;
		for (i=0; i< optionList.length; i++) {
			if (optionList[i].value.indexOf('_') > 0) { 
				optionList.length=i;
				break;
			} 
		}*/
		var compIndex;
		for (i=0; i < BillingAddresses.length; i++) {
			if (trim(BillingAddresses[i][0]) == company) {
				compIndex=i;
				//newIndex = optionList.length;
				//optionList[newIndex]=new Option(BillingAddresses[i][2],BillingAddresses[i][0]+'_'+BillingAddresses[i][1]+'_');
			}
		}
	//	frm.elements['qBillingSameAs'].selectedIndex=0;
		swapAddressDivsBilling(BillingAddresses[compIndex][0]+'_'+BillingAddresses[compIndex][1]+'_');
	}

	optionList = frm.elements['qShippingSameAs'].options;
	for (i=0; i< optionList.length; i++) {
		if (optionList[i].value.indexOf('_') > 0) { 
			optionList.length=i;
			break;
		} 
	}
	for (i=0; i < ShippingAddresses.length; i++) {
		if (ShippingAddresses[i][0] == company) {
			newIndex = optionList.length;
			optionList[newIndex]=new Option(ShippingAddresses[i][2],ShippingAddresses[i][0]+'_'+ShippingAddresses[i][1]+'_');
		}
	}
	frm.elements['qShippingSameAs'].selectedIndex=0;
	swapAddressDivsShipping('New');


}

