
function isNum(theString) {
    flag = true;
    
    if (theString.length <= 0){
		flag = false;
		return(flag);
	}
    for (i = 0; i < theString.length; i++) {
        if ((theString.substring(i, i+1) != '0') &&
	    (theString.substring(i, i+1) != '1') &&
	    (theString.substring(i, i+1) != '2') &&
	    (theString.substring(i, i+1) != '3') &&
	    (theString.substring(i, i+1) != '4') &&
	    (theString.substring(i, i+1) != '5') &&
	    (theString.substring(i, i+1) != '6') &&
	    (theString.substring(i, i+1) != '7') &&
	    (theString.substring(i, i+1) != '8') &&
	    (theString.substring(i, i+1) != '9'))
        {
            flag = false;
            break;
        }
    }
    return(flag);
}

function popupWindow(path, xWidth, yHeight){
		var infoWin = window.open(path , "Lollipops_and_Lemon_Drops", 'width='+xWidth+', height='+yHeight+', toolbar=no, status=no, scrollbars=yes, resizeable=no');
}

/* ------------------------------------------------------------------------
  validateField
  	Accepts: sValue - form value to validate (pass in document.frmName.fieldname.value)
			 sType - type of form element: combo, text, date, phone, zip
			 sDisplayName - name to use if there is an error
			 isRequired - true/false if the field is required to have a value
			 sOther - field for other info that may be used by some types (like Date: "notpast" or "notfuture")
	Returns:  Will return an error message if there is a problem or blank if no error
	Notes:	isError & sErrorMsg need to be var'd in the calling function
	Created: Brian Jennings - 10.20.2000
------------------------------------------------------------------------ */
function validateField(sValue, sType, sDisplayName, isRequired, sOther){
	var sReturnVal = true;
	var sErrorMsg = '';
	if (sValue == null){
		sValue = '';
	}
	switch(sType){
		case 'combo':
			//alert('checking combo: ' + sDisplayName);
			//sOther will be the blank value (if other than blank) to check for when required
			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}else{
				//It is not blank, so if there is an sOther value, check those
				if (sOther != ''){
					//sOther was NOT blank, so we need to check on it
					if (isRequired && sValue == sOther){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';}
				}
			}		
			break;
		case 'text':
			//Text boxes are mainly checked to see if they have data or not.
			//alert('checking text: ' + sDisplayName);		
			//First, check if it is required and empty
			if (isRequired && (sValue=='' || isBlank(sValue))){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}	//End Required Check
			break;
//		case 'date':
			//alert('checking date: ' + sDisplayName);		
//			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
//			}else{
				//If the date field is empty then skip the validation
//				if (sValue != ''){
					//Validate that the date is a valid date (with 4 digit year)
//					var dateMsg;
//					dateMsg = validateDateMDYy(sValue);
//					if (dateMsg != 'Success'){
//						sErrorMsg = sErrorMsg + ' * ' + sDisplayName + ': ' + dateMsg + '\n';
//					}else{
						//It is a valid date
						//Need to check that there is a 4-digit year
//						if ((!is4DigitYear(sValue))){
							//Not a 4-digit year
//							sErrorMsg = sErrorMsg + ' * ' + sDisplayName + ' must be a 4-digit year\n';
//						}else{
							//It is a 4-digit year, make sure it is after 1755 (minimum value SQL2k will allow - approx)
//							if (sValue.substring((sValue.length-4), sValue.length) < 1755){
//								isError = true;	sErrorMsg = sErrorMsg + ' * ' + sDisplayName + ' must occur after the year 1755\n';
//							}else{
//								if (sOther == 'notfuture'){
									//At this point, the date is valid, test to see if the date is a future date
//									if (!isNotFutureDate (sValue)){ isError = true;	sErrorMsg = sErrorMsg + ' * ' + sDisplayName + ' cannot be a future date\n';}
//								}//End sOther == 'notfuture'
//							}
//						}
//					}
//				}
//			}	//End Required Check
//			break;
//		case 'time':
//			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
//			}else{if (sValue != ''){ if (!isValidTime(sValue)){	sErrorMsg = ' * ' + sDisplayName + ' is not a valid time format. Use hh:mm\n';}}}
//			break;
		case 'phone':
			//alert('checking phone: ' + sDisplayName);		
			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}else{ if (!isTelephone(sValue)){	sErrorMsg = ' * ' + sDisplayName + ' is not a valid phone number - format: (253)555-1212\n';}}
			break;
		case 'zip':
			//alert('checking phone: ' + sDisplayName);
			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}else{
			 if (!isZip(sValue)){
			 sErrorMsg = ' * ' + sDisplayName + ' is not in the proper format.  Format: 98405 or 98405-2321 \n';
			 }
			}
			break;
		case 'email':
			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}else{
				if (sValue != ''){
					if (sValue.indexOf ('@',0) == -1 || sValue.indexOf ('.',0) == -1){
						sErrorMsg = ' * ' + sDisplayName + ' is invalid.  It should be like "name@domain.com"\n';
					}
				}
			}
			break;
//		case 'ssn':
//			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
//			}else{	if (sValue != ''){
//						if (!isSSN(sValue)){
//							sErrorMsg = ' * ' + sDisplayName + ' is invalid.  SSN format: 222334444 or 222-33-4444\n';
//						}
//					}
//			}
//			break;
		case 'integer':
			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}else{	if (sValue != ''){
						//Check to see if the value passed is an integer
						if (!isNum(sValue)){
							sErrorMsg = ' * ' + sDisplayName + ' is invalid.  It must be a whole number\n';
						}
					}
			}
			break;
		case 'float':
			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
			}else{	if (sValue != ''){
						//Check to see if the value passed is an integer
						if (!isFloat(sValue)){
							sErrorMsg = ' * ' + sDisplayName + ' is invalid.  It must be a real number\n';
						}
					}
			}
			break;
//		case 'money':
//			if (isRequired && sValue == ''){ sErrorMsg = ' * ' + sDisplayName + ' is required\n';
//			}else{	if (sValue != ''){
//				if (!validAmount(sValue)){
//					sErrorMsg = ' * ' + sDisplayName + ' is not a valid dollar amount\n';
//				}				
//			  }
//			}
//			break;
	}//End Switch

	return sErrorMsg;
} //End validateField


/*-------------------------------------------------------
 isTelephone
 	Accepts: String
	Returns: true if the string is a telephone number (xxx)xxx-xxxx
 ---------------------------------------------------------*/
function isTelephone(str_input) {
	if(str_input == "") {
		return true; 
	}
	var digit_flag; //set to true if a digit
	temp_string = new String;

	for (i=0; i < str_input.length; i++) {
		chr_value = str_input.charAt(i);
		digit_flag = false;
		for(y = 0; y < 10; y++)  //Loop through all digits 0 - 9
		{
			if(chr_value == (y + ''))
			{
				digit_flag = true;
				continue;
			}
		}

		if(digit_flag == false && (chr_value != "(" && chr_value != ")" && chr_value != "-" && chr_value != " "))
		{
			return false;  //not a digit or separator
		}

		if(digit_flag == true) {
			temp_string += chr_value;
		}
	}
	if(temp_string.length != 10) {
		return false;
	}
	return true;  //valid phone number
}
/*-----------------------------------------------------
 isZip
 	Accepts: String
	Returns: True if string is a Zip (Can include 9-digits)
-------------------------------------------------------*/
function isZip(str_input) {
	if(str_input == "") {
		return true; 
	}
	var digit_flag; //set to true if a digit
	temp_string = new String;

	for (i=0; i < str_input.length; i++) {
		chr_value = str_input.charAt(i);
		digit_flag = false;
		for(y = 0; y < 10; y++){  //Loop through all digits 0 - 9
			if(chr_value == (y + '')){
				digit_flag = true;
				continue;
			}
		}
		if(digit_flag == false && chr_value != "-")	{
			return false;  //not a digit or separator
		}
		if(digit_flag == true) {
			temp_string += chr_value;
		}
	}
	if(temp_string.length != 5 && temp_string.length !=9) {
		return false;
	}
	return true;  //valid Zip Code
}
/* ---------------------------------------------
	isBlank
		Returns True if the string is all spaces or empty, 
		        false otherwise.
		Created: 11.22.2000 - Michael Kibler
   --------------------------------------------- */	
function isBlank(inputStr)
	{
	var blnspace;
	blnspace=true;
	if (inputStr == "")	{
		return(true)
	}
	else {
		for (var c = 0; c < inputStr.length && blnspace ; c++) {
			if (inputStr.charAt(c) != " " ) {
				blnspace = false;
			}
		}
	}
	return(blnspace);
}

/* ---------------------------------------------
	isFloat
		Validates a valid floating point number, 1.0, -1.xxx, .0, etc.
		Returns True if it is a valid float, false otherwise
		Created: 11.22.2000 - Michael Kibler
   --------------------------------------------- */	

function isFloat(inputStr)
	{
	var decimal,numdigits;
	decimal="";
	numdigits=0;
	
	if (inputStr == "")
		{
			return(false)
		}
	else
		{
			for (var c = 0; c < inputStr.length; c++)
				{
					var oneChar = inputStr.charAt(c)
					if (oneChar >= "0" && oneChar <= "9")
									{
										numdigits++;
									}
					if (c == 0 && oneChar == "-" || oneChar == "."  && decimal == "")
							{
								if (oneChar == ".")
									{
										decimal = "yes"
									}
								continue
								
							}
								if (oneChar < "0" || oneChar > "9")
									{
										return(false) 
									}
				}
		}
		if (numdigits == 0) {
			return false
		}
		else {
		return(true)
		}
}

/* *******************************************************
	toggleImage
		Toggles image of image name (passed in) to image value
		(passed in).  If it is needed to traverse more than the 
		image directory, pass in a number of directories to go back
		in the iDirLoc field
		Created: 04.13.2001 - Brian Jennings
   ******************************************************* */
function toggleImage(sImgName, sImg, iDirLoc){
	var sDir = '';
	if (iDirLoc == 1){sDir = '../';}
	if (iDirLoc == 2){sDir = '../../';}
	document.images[sImgName].src = sDir + 'images/'+sImg;
}

/* *******************************************************
	toggleProductImage
		Toggles product image of image name (passed in) to image value
		(passed in).  If it is needed to traverse more than the 
		image directory, pass in a number of directories to go back
		in the iDirLoc field
		Created: 04.13.2001 - Brian Jennings
   ******************************************************* */
function toggleProductImage(sImgName, sImg, iDirLoc){
	var sDir = '';
	if (iDirLoc == 1){sDir = '../';}
	if (iDirLoc == 2){sDir = '../../';}
	document.images[sImgName].src = sDir + 'productimages/'+sImg;
}

function formatCurrency(str, bIncludeSign){
//Takes a string that is a number (can contain $) and 
//format it with 2 decimal places and $ (if bIncludeSign = true)
	var num, dec;
	str = str.toString();
	if (str.indexOf('$') != -1){
		//there is a $ in the string
		str = str.replace('$', '')
	}
	str = str.replace(',', '')
	//Find the decimal point
	if (str.indexOf('.') != -1){
		s = str.indexOf('.');
		dec = str.slice(s+1,str.length);
		num = str.slice(0,s)
		//Fix the decimal if it needs it
		if (dec.length != 2){
			//Need to fix the length of the decimal
			if (dec.length = 0){
				dec = '00';
			}
			if (dec.length = 1){
				dec = dec + '0';
			}
			if (dec.length > 2){
				dec = dec.slice(0,2);
				//TODO: fix to round up	
			}
		}	
	}else{
		//there is no decimal point
		num = str;
		dec = '00';
	}
//	alert('Num: ' + num + '\nDecimal: ' + dec)	
	str = num + '.' + dec;
	if (bIncludeSign){
		str = '$' + str;
	}
	return str;
}

//-->