
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function validateRegex(value, pattern) {
	var objRegExp = new RegExp( pattern);
	//check if string matches pattern
	return objRegExp.test(value);
}

/*
 * validatePostalCode
 * param @value - string, US or Canadian postal code
 * return true or false
 */
function validatePostalCode(value) {

	if (  this.validateRegex(value,/(^\d{5}$)|(^\d{5}-\d{4}$)/) || 
	      this.validateRegex(value,/^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/) ) {
	    return true;
	} else {
	    return false;
	}	

}  //validatePostalCode

/*
 * validatePhone
 * param @value - string, US phone number xxx-yyy-zzzz or xxxyyyzzzz
 * return true or false
 */
function validatePhoneNumber(value) {

	value = value.replace(/[\D]*/g, '');

	if (value.length == 10) {
		return true;
	}
	else {
		return false;
	}

/*
	if (  validateRegex(value,/(^\d{3}-\d{3}-\d{4}$)/) || ( !isNaN(value) && value.length == 10 ) ) {
	    return true;
	} else {
	    return false;
	}	
*/

}  //validatePostalCode


function getCheckBoxValue(obj) {
	var message = '';
	
	if (obj.length == undefined) {
	
		if (obj.checked) {
			message += obj.value;
		}
	
	} else {
	
		for (i = 0; i < obj.length; i++) {
		    if (obj[i].checked) {
			message += obj[i].value + ', ';
		    }
		}

	}
	
	return message;
}

 function getRadioButtonValue(obj) {
      for (i = 0; i < obj.length; i++) {
	if (obj[i].checked) {
	    return obj[i].value;
	}
     }
  }





function isValidCreditCard(type, ccnum) {
   if (type == "VISA") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "DISC") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AMEX") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "DINERS") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   } else {
   	return false;
   }
   
   
   if (!re.test(ccnum)) {
   	return false;
   }
   
   // Remove all dashes for the checksum checks to eliminate negative numbers
   ccnum = ccnum.split("-").join("");
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}


// This function is used to make sure that required form fields have values.
//    Field_Name: name of a form field in the form of document.form_name.form_field_name that will be validated.
//    Error_Message: this is the alert error message that will be displayed to the user.
function Has_Value(Field_Name, Error_Message) {
	var text;

	text = Field_Name.value;

	while(text.search(/\s/) > -1) {
        text = text.replace(/\s/, "");
   	}
	
    if(text == 0) {
    	if (Error_Message.length) {
        	alert(Error_Message);	
        }
        
        Field_Name.focus();
        return false;   
    }
    return true;
}

// IS_VALID_EMAIL( emailaddy )
// Returns TRUE if Email is valid and FALSE if NOT VALID
function Is_Valid_Email( emailaddy ){
    var tempchar;       // temp holder
    var atPos;          // string position holder
    var periodPos;      // position of the period in string
    var invalchar = "/:,;"; // not valid email characters
    // if the email string is empty return false
    //if( Is_Empty( emailaddy ) ){ return false; }
    // Loop through each invalid character
    for( count = 0; count < invalchar.length; count++ ){
        // Pull one character out of the string
        tempchar = invalchar.charAt( count );
        // If an invalid character is found return false
        if( emailaddy.indexOf( tempchar, 0 ) > -1 ){
            return false;
        }
    }
    // find and store the position of the at sign
    atPos = emailaddy.indexOf( "@", 1 );
    // If no at sign is in the string return false
    if( atPos == -1 ){ return false; }
    // If at sign is the last character in the string return false
    if( emailaddy.indexOf( "@", atPos + 1 ) > -1 ){ return false; }
    // Find and store the position of the period in the email string
    periodPos = emailaddy.indexOf(".", atPos);
    // If there is no period in the string return false
    if( periodPos == -1 ){ return false; }
    // If the period is less than 3 characters from
    // the end of the string return false
    if( periodPos + 3 > emailaddy.length ){ return false; }
    // all is well this is a valid email address so return true
    return true;
}
// END OF FUNCTION

function Is_Valid_Date(Field_Name, Error_Message)
	//This function will validate a form date field to make sure that its in the proper
	//format.  It will accept dates in the following format: mm/dd/yyyy || m/dd/yyyy || m/d/yy || 
	// 	m/dd/yy || mm/d/yy || any valid date that is delimited by the '/' symbol.
	//Field_Name: name of a form field in the form of document.form_name.form_field_name that will be validated.
	//Error_Message: this is the alert error message that will be displayed to the user.
	{
		if (!Is_Date(Field_Name, Error_Message))
			{
			 alert(Error_Message);
			 Field_Name.focus();
			 return false;
			}
		//everything passed so...
		return true;
	}	
	
	function Is_Date(Field_Name) {
	    //Returns true if value is a date format or is NULL
	    //otherwise returns false   
	
	    if (Field_Name.value.length == 0)
	        return true;
	
	    //Returns true if value is a date in the mm/dd/yyyy format
	        isplit = Field_Name.value.indexOf('/');
	
	        if (isplit == -1 || isplit == Field_Name.value.length) { return false; }
	
	    sMonth = Field_Name.value.substring(0, isplit);
	
	        if (sMonth.length == 0)
	        {
			 return false;
			}
	
	        isplit = Field_Name.value.indexOf('/', isplit + 1);
	
	        if (isplit == -1 || (isplit + 1 ) == Field_Name.value.length)
	                {
					 return false;
					}
	
			sDay = Field_Name.value.substring((sMonth.length + 1), isplit);
	
	        if (sDay.length == 0)
	        {
			 return false;
			}
			
	        sYear = Field_Name.value.substring(isplit + 1);
	
	        if (!_CF_checkinteger(sMonth)) //check month
	                {
					 return false;
					}
	        else
	        if (!_CF_checkrange(sMonth, 1, 12)) //check month
	                {
					 return false;
					}
	        else
	        if (!_CF_checkinteger(sYear)) //check year
	                {
					 return false;
					}
	        else
	        if (!_CF_checkrange(sYear, 0, 9999)) //check year
	                {
					 return false;
					}
	        else
	        if (sYear.length != 4)
				{
				return false;
				}
	        if (!_CF_checkinteger(sDay)) //check day
	                {
					 return false;
					}
	        else
	        
	        
	        if (!_CF_checkday(sYear, sMonth, sDay)) // check day
	                {
					 return false;
					}
	        else
	                return true;
	    }







function _CF_checkday(checkYear, checkMonth, checkDay)
    {

        maxDay = 31;

        if (checkMonth == 4 || checkMonth == 6 ||
                        checkMonth == 9 || checkMonth == 11)
                maxDay = 30;
        else
        if (checkMonth == 2)
        {
                if (checkYear % 4 > 0)
                        maxDay =28;
                else
                if (checkYear % 100 == 0 && checkYear % 400 > 0)
                        maxDay = 28;
                else
                        maxDay = 29;
        }

        return _CF_checkrange(checkDay, 1, maxDay); //check day
    }

function _CF_checkinteger(Field_Name)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false   

    if (Field_Name.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
        var decimal_format = ".";
        var check_char;

    //The first character can be + -  blank or a digit.
        check_char = Field_Name.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
        return _CF_checknumber(Field_Name);
    else
        {
		 return false;
		}
    }

function _CF_numberrange(Field_Name, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
        {
        if (Field_Name < min_value)
                {
				 return false;
				}
        }

    // check maximum
    if (max_value != null)
        {
        if (Field_Name > max_value)
                {
				 return false;
				}
        }
        
    //All tests passed, so...
    return true;
    }

function _CF_checknumber(Field_Name)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false   

    if (Field_Name.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
        var start_format = " .+-0123456789";
        var number_format = " .0123456789";
        var check_char;
        var decimal = false;
        var trailing_blank = false;
        var digits = false;

    //The first character can be + - .  blank or a digit.
        check_char = start_format.indexOf(Field_Name.charAt(0))
    //Was it a decimal?
        if (check_char == 1)
            decimal = true;
        else if (check_char < 1)
                {
				 return false;
				}
        
        //Remaining characters can be only . or a digit, but only one decimal.
        for (var i = 1; i < Field_Name.length; i++)
        {
                check_char = number_format.indexOf(Field_Name.charAt(i))
                if (check_char < 0)
                        {
						 return false;
						}
                else if (check_char == 1)
                {
                        if (decimal)            // Second decimal.
                                {
								 return false;
								}
                        else
                                decimal = true;
                }
                else if (check_char == 0)
                {
                        if (decimal || digits)  
                                trailing_blank = true;
        // ignore leading blanks

                }
                else if (trailing_blank)
                        {
						 return false;
						}
                else
                        digits = true;
        }       
    //All tests passed, so...
    return true
    }

function _CF_checkrange(Field_Name, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (Field_Name.length == 0)
        return true;


    if (!_CF_checknumber(Field_Name))
        {
         return false;
        }
    else
        {
        return (_CF_numberrange((eval(Field_Name)), min_value, max_value));
        }
        
    //All tests passed, so...
    return true;
    }



function ValidatePostal() {
	if( (document.LocateForm.reqzip.value.length < 5) || (document.LocateForm.reqzip.value.length > 7) || (document.LocateForm.reqzip.value == 'enter postal code') ) { 
		alert('Please enter your 5-digit US ZIP code.');
		document.LocateForm.reqzip.focus();
		return false; 
		
	}
	else return true;
}
