function OptionPopup(sFilename, sFieldName) {

	sImagename = eval(sFieldName + '_IMAGE_POPUP_' + eval(sFieldName + '.selectedIndex') + '.value');
	
	if (isEmpty(sImagename)) return;
	
	window.open(sFilename + '?FILE=' + sImagename, 'Preview', 'WIDTH=50,HEIGHT=50,scrollbars=no,left=150,top=100,screenX=150,screenY=100,resizable=yes');
}

function isDefined(obj) {

	if(typeof(obj) == 'undefined') {
          return false;
	} else {
          return true;
	}
}
function push(arrayName,value) {
	arrayName[arrayName.length+1] = value.valueOf();
}

function GetPriceIndex(oForm, sID, iQuantity) {
	var iStart, iEnd
	
	if (parseFloat(eval('oForm.START_' + sID + '_1.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_1.value')) >= iQuantity) {
		return 1;
	} else if (parseFloat(eval('oForm.START_' + sID + '_2.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_2.value')) >= iQuantity) {
		return 2;
	} else if (parseFloat(eval('oForm.START_' + sID + '_3.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_3.value')) >= iQuantity) {
		return 3;
	} else if (parseFloat(eval('oForm.START_' + sID + '_4.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_4.value')) >= iQuantity) {
		return 4;
	} else if (parseFloat(eval('oForm.START_' + sID + '_5.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_5.value')) >= iQuantity) {
		return 5;
	} else if (parseFloat(eval('oForm.START_' + sID + '_6.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_6.value')) >= iQuantity) {
		return 6;
	} else if (parseFloat(eval('oForm.START_' + sID + '_7.value')) <= iQuantity && parseFloat(eval('oForm.END_' + sID + '_7.value')) >= iQuantity) {
		return 7;
	}
	return -1;
}

function Recalc(oForm, sID) {	
	var aSaved = new Array(0);
	var fVal, fValue, iQuantity, iIndex, iSize;
	
	// calculate base price for item
	if (isDefined(oForm.ITEM_QUANTITY)) {
		iQuantity = eval('oForm.ITEM_QUANTITY.value')
	} else {
		iQuantity = eval('"oForm.ITEM_QUANTITY"+sID+".value"')
	}
	iQuantity = parseInt(iQuantity,10);
	if (isNaN(iQuantity)) return false;

	// Get price index (index starts with 0)
	iIndex = GetPriceIndex(oForm, sID, iQuantity)
	if (iIndex == -1) return false;
	
	// Get PRICE_ID for selected price
	iSize = eval('getValue(oForm.SIZE_' + sID + ')')
	
	var fValue = parseFloat(eval('oForm.PRICE_' + sID + '_' + iSize + '_' + iIndex + '.value'));

	// for each element on the form 
	// adjust price for all other attributes

	for (var i = 0; i< oForm.elements.length; i++) {
		var oElement = oForm.elements[i];
		var sName = new String(oElement.name);

		if (sName.indexOf('ATTR_' + sID) ==0 ) {
			var sFieldType = oElement.type;

			if (sFieldType == "select-one") {
				var iIndex = oElement.selectedIndex;
				fVal = eval('oForm.' + sName + '_' + iIndex + '.value');
				fVal = parseFloat(fVal);

			} else if (sFieldType == 'radio') {

				iLength = eval('oForm.' + sName + '.length');
				bChecked = false;
				for(var j=0; j < iLength; j++) {
					if (eval('oForm.' +  sName + '[' + j + '].checked')) {
						bChecked = true;
						break;
					}
				}	
				
				if (bChecked) {
					if (isDefined(eval('oForm.' + sName + '_' + j))) {

						// Add element but only when doesn't exist in aSaved
						var k = getIndex(aSaved, sName);
						if ( k < 0 ) { 
							push(aSaved, sName);
							fVal = eval('oForm.' + sName + '_' + j + '.value');
						} else {
							fVal = 0;
						}	
					} else fVal = 0;	
				} else fVal = 0;
				fVal = parseFloat(fVal);
			} else fVal = 0;

			if (!isNaN(fVal)) fValue = fValue + fVal;
		}

	}

	eval('oForm.PRICE_' + sID + '.value = fmtValue(fValue)');
	return true;
}

function fmtValue(value){
	result=Math.floor(value)+".";
	var cents=100*(value-Math.floor(value))+0.5;
	result += Math.floor(cents/10);
	result += Math.floor(cents%10);
	return result;
}	

function PutDate(iMonth, iDay, iYear, sMonthCtrlName, sDayCtrlName, sYearCtrlName) {
	var sMonth, sDay, sYear;
	var iTmp;
	
	sMonth 	= 'opener.' + sMonthCtrlName;
	sDay 	= 'opener.' + sDayCtrlName;
	sYear 	= 'opener.' + sYearCtrlName;

	iTmp	=  eval(sYear + '[1].value');
	
	if (typeof(opener.document) != 'object') { window.close(); return; }
	if (opener.document == null) { window.close(); return; }
	
    // SET THE VALUE OF THE FIELD THAT WAS PASSED TO THE CALENDAR
	if (typeof(eval(sMonth)) == "object") eval(sMonth + '.selectedIndex = ' + iMonth);
	if (typeof(eval(sDay)) == "object") eval(sDay + '.value = ' + iDay);
	if (typeof(eval(sYear)) == "object") eval(sYear + '.selectedIndex = ' + (iYear-iTmp+1));

    // GIVE FOCUS BACK TO THE DATE FIELD
	if (typeof(eval(sMonth)) == "object") eval(sMonth + '.focus()');

	window.close();
}

function PickDate(iMonth, iDay, iYear, sMonthCtrlName, sDayCtrlName, sYearCtrlName) {
	var sURL;
	
	sURL = '/includes/datepicker.asp?MONTH=' + iMonth + '&DAY=' + iDay + '&YEAR=' + iYear + '&M_NAME=' + sMonthCtrlName + '&D_NAME=' + sDayCtrlName + '&Y_NAME=' + sYearCtrlName;
	var newwin = window.open(sURL, 'calendar', 'TOP=150,LEFT=300,WIDTH=170,HEIGHT=220,RESIZABLE=yes,SCROLLBARS=no,STATUS=0');
	if (newwin != null)
	{
		if (newwin.opener == null)
			newwin.opener = self;
	}
}

function getIndex(arrayName, value) {
	for (var i=0; i < arrayName.length; i++) if(value == arrayName[i]) return i;
	return -1;
}

function getValue(Field) {
	fieldType = Field.type;

	if (fieldType == "text") {
		return getTextValue(Field);
	} else if (fieldType == "hidden") {
		return getTextValue(Field);
	} else if (fieldType == "select-one") {
		return getListValue(Field);
	} else if (fieldType == "textarea") {
		return getTextValue(Field);
	} else if (fieldType == "file") {
		return getTextValue(Field);
	} else if (fieldType == "password") {
		return getTextValue(Field);
	} else if (fieldType == "checkbox") {
		return getCheckboxValue(Field);
	} else if (isNaN(fieldType)) {
		return getRadioValue(Field);
	} else {
		return getTextValue(Field);
	}
}

function isEmptyField(Field) {
	fieldType = Field.type;

	if (fieldType == "text") {
		return isEmptyText(Field);
	} else if (fieldType == "hidden") {
		return isEmptyText(Field);
	} else if (fieldType == "file") {
		return isEmptyText(Field);
	} else if (fieldType == "select-one") {
		return isEmptyList(Field);
	} else if (fieldType == "textarea") {
		return isEmptyText(Field);
	} else if (fieldType == "password") {
		return isEmptyText(Field);
	} else if (fieldType == "checkbox") {
		return isEmptyCheckbox(Field)
	} else if (isNaN(fieldType)) {
		return isEmptyRadio(Field)
	} else {
		return isEmptyText(Field);
	}
}

function isCurrency(Field) {
	strValue = Field.value;

	regexp = /^(([0-9]{1,3}(\,[0-9]{3})*)|([0-9]{0,3}))(\.[0-9]{2})?$/

	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}


function isFloat(Field) {
	strValue = Field.value;

	regexp = /^(\+|\-)?([0-9]+)(((\.|\,)?([0-9]+))?)$/

	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}	

function isInteger(Field) {
	strValue = getValue(Field);

	regexp = /^(\+|\-)?([0-9]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}	

function isMemberUserName(Field) {
	strValue = getValue(Field);

	regexp = /^[^*]([^$@\\ ]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}	

function isUserName(Field) {
	strValue = getValue(Field);

	regexp = /^([^$@\\ ]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}	


function getCCNumber(sInput) {
	var sResult='';

	// make sure input is a string
	sInput+='';

	for (iLoop=0; iLoop<sInput.length; iLoop++) {
		if (sInput.substring(iLoop,iLoop+1) >= '0' && sInput.substring(iLoop, iLoop+1) <= '9')
			sResult += sInput.substring(iLoop, iLoop+1);
	}
	return sResult;
}

function isCreditCardNumber(Field) {
	var iChkSum=0;
	var ccnum = getCCNumber(getValue(Field));

	// check for correct card number length
	if (ccnum.length<13) return false;

    // make an array and fill it with the individual digits of the cc number
    ccnumchk=new Array;
	for (iLoop=0; iLoop < ccnum.length; iLoop++) {
		ccnumchk[ccnum.length-1-iLoop] = ccnum.substring(iLoop, iLoop+1);
	}

    // perform the weird mathematical method (some base 10 stuff) to
    // convert the number to a two digit number
    // for those of you who aren't as familiar with the js operators
    // i'll comment some of the math lines...well, really just one

    var skemp=0;
	for (iLoop=0; iLoop < ccnum.length; iLoop++) {
		// if splits is an even number...	
        if (iLoop %2 != 0) {      
			ccnumchk[iLoop]=ccnumchk[iLoop]*2;
			if (ccnumchk[iLoop] >= 10) ccnumchk[iLoop]=ccnumchk[iLoop]-9;
		}

        // switch ccnumchk[splits] to a number
		ccnumchk[iLoop]++; 
		ccnumchk[iLoop]--;

		iChkSum = iChkSum + ccnumchk[iLoop].valueOf();
	}
	if (iChkSum%10 != 0) { return false; } // The result isn't base 10 

	return true;
}

function isEmail(Field) {
	strValue = getValue(Field);

	regexp = /^([^$@\\ ]+)@((([^$@\\ \.]+)\.)+)([A-Za-z0-9]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}	

function isURL(Field) {
	strValue = getValue(Field);

	regexp = /^http(s?):\/\/([^$@\\ ]+)$/i
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}	

function isEmailList(Field) {
	strValue = getValue(Field);

	// delete all spaces near comma
	rexp = /, /gi;
	strValue = strValue.replace(rexp, ',');
	rexp = / ,/gi;
	strValue = strValue.replace(rexp, ',');
	strArray = strValue.split(",");

	regexp = /^([^$@\\ ]+)@((([^$@\\ \.]+)\.)+)([A-Za-z0-9]+)$/

	for (i=0; i < strArray.length; i++) {
		if (isEmpty(strArray[i])) return false;
		if (!regexp.test(strArray[i])) return false;
	}
	// set new field value (with removed spaces between comma and addresses)
	Field.value = strValue;
	return true;
}	

function isEmptyCheckbox(Field) {
	return !Field.checked;
}

function isEmptyRadio(Field) {
	found = false;

	if(isNaN(Field.length)) {
		return !Field.checked;
	}	

	for(i=0; i< Field.length; i++) {
		if ( Field[i].checked ) {
			found = true;
			break;
		}
	}	
	return !found;
}

function getListValue(Field) {
	return Field[Field.selectedIndex].value;
}

function getTextValue(Field) {
	return Field.value;
}

function getCheckboxValue(Field) {

	if (Field.checked) return Field.value;
	return '';
}

function getRadioValue(Field) {
	found = false;

	if(isNaN(Field.length)) {
		return Field.value;
	}	

	for(i=0; i< Field.length; i++) {
		if ( Field[i].checked ) {
			return Field[i].value;
			break;
		}
	}	
	return !found;
}

function isZip(Field) {
	strValue = getValue(Field);

	if (isEmpty(strValue)) {
		return false;
	}

	if (strValue.indexOf('-') >=0 ) {
		regexp = /^\d{5}-\d{4}$/
	} else {
		regexp = /^\d{5}$/
	}

	return regexp.test(strValue);
}

function isPhoneValue(strValue) {

	regexp = /^\d{3}-\d{3}-\d{4}$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isPhone(Field) {
	strValue = getValue(Field);

	regexp = /^1{0,1} *(-| ){0,1} *[\(]*[0-9]{0,3}[\)]* *(-| ){0,1} *[0-9]{3} *(-| ){0,1} *[0-9]{4}$/;

	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isFax(Field) {
	strValue = getValue(Field);

	regexp = /^\d{3}-\d{3}-\d{4}$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isEmptyList(Field) {
	return isEmpty(Field[Field.selectedIndex].value);
}

function isEmptyText(Field) {
	return isEmpty(Field.value)
}

function isEmpty(s) {
	if (s == null || trim(s) == '') {
		return true;
	}
	else {
		return false;
	}
}

function convertYear(y) {
	var borderYEAR = 40;

	yearvalue = parseInt(y,10);
	if (isNaN(yearvalue)) return y;

	if (yearvalue - borderYEAR <= 0) {
		yearvalue = yearvalue + 2000
	} else if (yearvalue - 100 < 0) {
		yearvalue = yearvalue + 1900
	}

	return yearvalue;
}

function CheckTime(hh,mm,ampm) {
	if (isNaN(parseInt(hh,10))) return false;
	if (isNaN(parseInt(mm,10))) return false;
	if (isEmpty(ampm)) return false;

	hh = parseInt(hh,10);
	mm = parseInt(mm,10);
	
	if (ampm == 'PM' && hh > 12) return false;
	return true;
}

function CheckDate(m,d,y) {
	Months = "31/!/28/!/31/!/30/!/31/!/30/!/31/!/31/!/30/!/31/!/30/!/31";
	MonthArray  = Months.split("/!/");

	if (isNaN(parseInt(m,10))) return false;
	if (isNaN(parseInt(d,10))) return false;
	if (isNaN(parseInt(y,10))) return false;

	d = parseInt(d,10);
	y = parseInt(y,10);
	m = parseInt(m,10);

	y = convertYear(y);

	if (y <= 1900 ) return false;
	if (m < 1 || m > 12 ) return false;
	if ( isLeapYear(y)) MonthArray[1] = eval(eval(MonthArray[1]) + 1);

	if (d<1 || MonthArray[m-1] < d ) return false;
	return true;
}

function isLeapYear(Year)
{
	if(Math.round(Year/4) == Year/4){
		if(Math.round(Year/100) == Year/100){
			if(Math.round(Year/400) == Year/400)
				return true;
			else return false;
		}else return true;
	}
	return false;
}

function trim(str) {
	while (str.substring(0,1) == " ") {
		str = str.substring(1,str.length);
	}
	while (str.substring(str.length-1,str.length) == " ") {
		str = str.substring(0,str.length-1);
	}
	return str;
}

