function LoadFreeReport() { //v2.0
  //alert("In LoadFreeReport");
  var s_width = 380;
  var s_height = 350;
  var theURL = 'FreeReportPopup_Main.asp';
  var winName = 'FreeReport';
  var features = 'SCROLLBARS=NO,WIDTH=' + s_width + ',HEIGHT=' + s_height + ',status=yes';
  //alert('theURL=' + theURL + ' winName=' + winName + ' features=' + features);
  var windowHandle;
  windowHandle = window.open(theURL,winName,features);
  if (windowHandle.opener == null) 
	windowHandle.opener = self;
}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function RemoveBad(strTemp) { 
    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\!|\@|\#|\$|\^|\*|\=|\~|\.|\,|\?|\_|\ /g,""); 
    return strTemp;
}

function RemoveAll(strTemp) { 
    strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\!|\@|\$|\^|\*|\=|\~|\.|\,|\?|\_|\ /g,""); 
    return strTemp;
	}

//INITIALIZE DATE VARIABLES
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

//CHECK IF 's' IS A NUMBER
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

//STRIP UNWANTED CHARACTERS 's' FROM BAG 'bag'
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

//GET DAYS IN FEBRUARY
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

//CHECK IF 'dtStr' IS A VALID DATE
function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	var returnString = "";
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		returnString = "The autoship date format should be : mm/dd/yyyy";
	}
	if (strMonth.length<1 || month<1 || month>12){
		returnString = "Please enter a valid month for Autoship Date";
	}
	if (strDay.length<1 || day<1 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		returnString = "Not a valid date in the month";
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		returnString = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		returnString = "Please enter a valid date";
	}
	return returnString;
}

var frmNumber 	//holds the form's number in the Form Object
var arRequired 	//holds the array of text fields
function CheckRequiredFields(arRequired, frmNumber) {
	//this function checks to see if all the required fields are populated
	var strMsg = 'Please complete the following fields:\n';
	var iCount = 0;
	//if 'frmNumber' is not included, default to the first form on the page
	if (frmNumber) {
		//continue
	} else {
		frmNumber = 0;
	}
	for (var i = 0; i < arRequired.length; i++) {
		var elem = document.forms[frmNumber].elements[arRequired[i]]
		var x = elem.value;
		if (x == '' || x == null) {
			strMsg += ((0 < iCount) ? ', ' : '') + arRequired[i].substring(3,arRequired[i].length);
			iCount += 1;
		}
	}
	//if no errors were found, pass back an empty string
	if (iCount == 0)
		strMsg = ''
		
	//return the error message 
	return strMsg
}

function SecurityCheck(frmNumber) {
	//this function checks to see if there is vbscript or asp in the form
	var strMsg = 'There are unacceptable characters in these fields:\n';
	var iCount = 0;
	//if 'frmNumber' is not included, default to the first form on the page
	if (frmNumber) {
		//continue
	} else {
		frmNumber = 0;
	}
	for (i=0; i < document.forms[frmNumber].elements.length; i++) {
		var elem = document.forms[frmNumber].elements[i];
		if (elem.type == "text" || elem.type == "textarea") {
			var x = elem.value;
			var n = elem.name;
			if ((x.indexOf('<' + 'script') >= 0) || (x.indexOf('/script' + '>') >= 0) || (x.indexOf('%' + '>') >= 0) || (x.indexOf('<' + '%') >= 0)) {
				strMsg += ((0 < iCount) ? ', ' : '') + n.substring(3,n.length);
				iCount += 1;
			}
		}
	}
	//if no errors were found, pass back an empty string
	if (iCount == 0)
		strMsg = ''
		
	//return the error message 
	return strMsg
}

function CheckValidEmail(emailArray, frmNumber) {
	//this function makes sure the email address won't break an automated email system
	var strMsg = 'Your email address is invalid. Please consult the help:\n';
	var iCount = 0;
	//if 'frmNumber' is not included, default to the first form on the page
	if (frmNumber) {
		//continue
	} else {
		frmNumber = 0;
	}
	for (var i = 0; i < emailArray.length; i++) {
		var elem = document.forms[frmNumber].elements[emailArray[i]]
		var x = elem.value;
		if (x.length > 0) {
			if ((x.indexOf('@') == -1) || (x.indexOf('@') == 0)) {
				strMsg += ((0 < iCount) ? ', ' : '') + emailArray[i].substring(3,emailArray[i].length);
				iCount += 1;
			}
		}
	}
	//if no errors were found, pass back an empty string
	if (iCount == 0)
		strMsg = ''
		
	//return the error message 
	return strMsg
}

function CheckIsNumeric(arNumeric, frmNumber, typeCode) {
	var strMsg = 'The following fields are not numeric:\n';
	var iCount = 0;
	//if 'frmNumber' is not included, default to the first form on the page
	if (frmNumber) {
		//continue
	} else {
		frmNumber = 0;
	}
	//loop through the fields array 'arNumeric' and check if each is a number
	if (arNumeric) {
		for (var i = 0; i < arNumeric.length; i++) {
			var elem = eval(document.forms[frmNumber].elements[arNumeric[i]])
			var x = elem.value
			//check if the value is not a number; if value is not a number, run error count
			if (isNaN(Number(x))) {
				strMsg += ((0 < iCount) ? ', ' : '') + arNumeric[i].substring(3,arNumeric[i].length);
				iCount += 1;
			}
			//if the number needs to be between 0 and 100 as a percentage
			if (typeCode == 'Percentage') {
				if (x > 100 || x < 0) {
					strMsg += ((0 < iCount) ? ', ' : '') + arNumeric[i].substring(3,arNumeric[i].length);
					iCount += 1;
				}
			}
			//if the number needs to be between 15 and 17 as a credit card number
			if (typeCode == 'CreditCardNumber') {
				if ((x.length > 17) || (x.length < 15)) {
					strMsg += ((0 < iCount) ? ', ' : '') + arNumeric[i].substring(3,arNumeric[i].length);
					iCount += 1;
				}
			}
		}
	} else {
		//loop through all text fields
		for (i=0; i < document.forms[frmNumber].elements.length; i++) {
			var elem = document.forms[frmNumber].elements[i];
			if (elem.type == "text" || elem.type == "textarea") {
				var x = elem.value;
				var n = elem.name;
				if (isNaN(Number(x))) {
					strMsg += ((0 < iCount) ? ', ' : '') +  n.substring(3, n.length);
					iCount += 1;
				}
			}
		}
	}
	//if no errors were found, pass back an empty string
	if (iCount == 0)
		strMsg = ''
		
	//return the error message 
	return strMsg
}

//Function to replace other delimiters with a '/' so it can be read as a date into the database
function replaceDateDelimiter(strTemp) { 
    strTemp = strTemp.replace(/\-|\~|\.|\_|\ /g,"/"); 
    return strTemp;
} 

function CheckIsDate(arDate, frmNumber) {
	var strMsg = 'The following fields are not valid dates:\n';
	var iCount = 0;
	//if 'frmNumber' is not included, default to the first form on the page
	if (frmNumber) {
		//continue
	} else {
		frmNumber = 0;
	}
	//loop through each element in the field array 'arDate' and check that each is a valid date
	for (var i = 0; i < arDate.length; i++) {
		var elem = eval(document.forms[frmNumber].elements[arDate[i]])
		var val = replaceDateDelimiter(elem.value)
		//run the 'isDate' function on the value
		if (val.length > 0) { 
			if (isDate(val) != "") {
				strMsg += isDate(val) + "\n"
				strMsg += ((0 < iCount) ? ', ' : '') + arDate[i].substring(3,arDate[i].length);
				iCount += 1;
			}
		}
	}
	//if no errors were found, pass back an empty string
	if (iCount == 0)
		strMsg = ''
		
	//return the error message 
	return strMsg
}

//Check to see if expiration date is in the future
function CheckExpDateInFuture(month, year) {
	var strMsg = 'The Expiration Date must be in the future\n';
	var iCount = 0;
	//get today's date
	var now = new Date()
	//minus one to account for the month array
	readyMonth = month - 1
	//put drop down values into a date format
	var expDate = new Date(year, readyMonth, 01);
	//check that the expiration date is greater than today's date
	if (expDate > now) {
		//continue
	} else {
		iCount += 1;
	}
	//if no errors were found, pass back an empty string
	if (iCount == 0) {
		strMsg = ''
	}

	//return the error message 
	return strMsg
}


function LoadPopup(theURL) { //v2.0
  var s_width = 560;
  var s_height = 600;
  var winName = 'ProductPopup'
  var features = 'RESIZE=YES,SCROLLBARS=YES,WIDTH=' + s_width + ',HEIGHT=' + s_height + ',status=yes';
  var windowHandle
  windowHandle = window.open(theURL,winName,features);
  if (windowHandle.opener == null) 
	windowHandle.opener = self;
}

function ValidateData (strActionCode) {
	//this function validates the Person's log in name and password
	//first check to be sure that entries have been made in both text boxes.
	
	var strErrors //error message
	var arRequired //array storing names of required fields
		
	arRequired = new Array('txtFirstName', 'txtEmail')
	strErrors = CheckRequiredFields(arRequired)
	
	//if there is an error message, display it and exit sub
	if (strErrors.length != 0) {
		alert(strErrors);
		return false;
	}
		
	//submit the page
	document.forms["frmContact"].submit();	
}
