// Constants
var sCrLf = String.fromCharCode(13,10);

// Global replacement expressions
var g_ampEXP	 = /&/ig;
var g_ltEXP		 = /</ig;
var g_gtEXP		 = />/ig;
var g_aposEXP 	 = /'/ig;
var g_quoteEXP	 = /"/ig;
var g_BR		 = /<br>/ig;
var g_BRslashEXP = new RegExp("<br/>","ig");
var g_apos2EXP	 = new RegExp("&apos;","ig");
var g_CrLfEXP	 = new RegExp(sCrLf,"ig");

// Pattern expressions
var charExp = /./;
var numInteger = /^\d+$/;
var letterExp = /[a-z]/i;
var alphaNumExp = /^[a-zA-Z0-9_\s.\-]*$/;
var phoneExp =  /^\d{10}$/;
var zip5Exp = /^\d{5}$/;
var zipEexp = /^\d{4}$/;
var postalCodeExp = /^[a-z]{1}\d{1}[a-z]{1}\s\d{1}[a-z]{1}\d{1}$/i;
var emailExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i;
var dateYYExp = /^\d{1}\/\d{1}\/\d{2}$|^\d{1}\/\d{2}\/\d{2}$|^\d{2}\/\d{1}\/\d{2}$|^\d{2}\/\d{2}\/\d{2}$/;
var dateYYYYExp = /^\d{1}\/\d{1}\/\d{4}$|^\d{1}\/\d{2}\/\d{4}$|^\d{2}\/\d{1}\/\d{4}$|^\d{2}\/\d{2}\/\d{4}$/;

// Checks for alpha characters
function hasChar(str) {
	return charexp.test(str)
}

// Checks for alphanumeric characters
function isAlphaNumeric(str) {

	if (!isValid(alphaNumExp, str)) {
		return false;
	} else {
		return true;
	}
}

// Strips "pattern" of characters
function stripChars(pattern, str) {
	return str.replace(pattern,"")
}

// Strips non-digits
function stripNonDigits(str) {
	return str.replace(/[^0-9]/g,"")
}	

// Validates data using a pattern expression
function isValid(pattern, str) {
	return pattern.test(str);
}

// Removes special characters
function RemoveSpecialChars(strTemp) { 
	strTemp = strTemp.replace(/\<|\>|\"|\%|\;|\(|\)|\&|\+|\-/g,""); 
return strTemp;
}  

// Checks for valid e-mail address
function ValidEmail(sText) {

	if (Trim(sText) != "") {
		if (!isValid(emailExp,sText)) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

// Checks for valid e-mail address from an XSLT page
function check_email_xslt(sText)
		{
		if (Trim(sText) != "") {
			if (!isValid(emailExp,sText)) {
				return alert("Please verify that your email address is correct!");
			} else {
				return true;
			}
	} else {
		return true;
	}
}

// Checks for alphanumeric characters
function Check_Alpha_Numeric_xslt(str) {

	if (Trim(str.value) !="") {
		if (!isValid(alphaNumExp, str.value)) {
			str.focus();
			return alert("You can not uncommon characters such as '#,$,&,(,/, etc'");
		} else {
			return true;
		}
	} else {
		return true;
	}
}
		
// Checks for valid Canadian Postal Codes
function ValidPostalCode(sText) {

	if (Trim(sText) != "") {
		if (!isValid(postalCodeExp,sText)) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

// Checks for valid phone numbers (999) 999-9999 format
function ValidPhone(sText) {

	if (Trim(sText) != "") {
		if (!isValid(phoneExp,sText)) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function isValidDate(dateStr) {

	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		alert(dateStr + " Date is not in a valid format.")
		return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];

	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		return false;
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
	    }
	}
	return true;
}

function doDateCheck(from, to) {

	if (Date.parse(from) > Date.parse(to)) {
		alert("To date must occur after the from date.");
		return false;
	}
	
	return true;
}

// Returns single data item from between specified XML tag
function getXMLItem(sXML, sItemName) {
		
	// parse item type from xml string
	var iPos1 = sXML.indexOf("<" + Trim(sItemName) + ">", 0);
	iPos1 = iPos1 + sItemName.length + 2;
	var iPos2 = sXML.indexOf("</" + Trim(sItemName) + ">", iPos1);
	return Trim(sXML.substring(iPos1, iPos2));
}

// Converts special XML characters to their corresponding entity values
function toXMLEntities(sText) {

	var sText2 = Trim(sText);
		
	if (sText.length > 0) {
		sText2 = sText2.replace(g_ampEXP, "&amp;")
		sText2 = sText2.replace(g_ltEXP, "&lt;")
		sText2 = sText2.replace(g_gtEXP, "&gt;")
		sText2 = sText2.replace(g_quoteEXP, "&quot;")
		sText2 = sText2.replace(g_aposEXP, "&apos;")
		sText2 = sText2.replace(g_CrLfEXP, "&lt;br/&gt;")
	} else {
		sText2 = "";
	}
	return sText2;
}


// Converts special XML entities to their corresponding characters
function fromXMLEntities(sText) {

	var sText2 = Trim(sText);
	
	if (sText.length > 0) {
		sText2 = sText2.replace(g_BR, sCrLf)
		sText2 = sText2.replace(g_BRslashEXP, sCrLf)
		sText2 = sText2.replace(g_apos2EXP, "'")
	} else {
		sText2 = "";
	}
	return sText2;
}


// This function replaces the data between specified XML tags.
function replaceXMLitem(sXMLStream, sItemName, sValue) {

	// The entire XML data stream
	var sStream = Trim(sXMLStream);
	
	// The specified start tag
	var sStartTag = "<" + Trim(sItemName) + ">";
	
	// The expected end tag
	var sEndTag = "</" + Trim(sItemName) + ">";
	
	// Trim the supplied data that will replace the current
	// data between the start and end tags
	var sData = Trim(sValue);

	//  Find the beginning position of the data to be replaced
	var iPos  = sStream.indexOf(sStartTag) + sStartTag.length;
	
	// Save the "left-side" portion of the XML data stream up to but not
	// including the data to be replaced
	var lText = sStream.substring(0, iPos);
	
	// Find the ending position of the data to be replaced
	var iPos  = sStream.indexOf(sEndTag);
	
	// Save the "right-side" portion of the XML data stream
	var rText = sStream.substring(iPos, sStream.length);

	// Re-string the left-side and right-side XML placing
	// the new data in between
	var sReplacedXMLStream = lText + sData + rText;
	return sReplacedXMLStream;
}


// This function will parse a full URL returning an array
// of elements found.
//
// result[0]		Will contain the entire input string
// result[1]		Will contain the protocol (http, https, ftp, etc)
// result[2]		Will contain a base URL (www.microsoft.com)
// result[3]		Will (if present) contain a path relative to the base URL
//
function checkURL(sURL) {

	// RegExp
	var url = /^(\w+):\/\/([\w.]+)\.([\w.]+)\.([\w.]+)\/?(\S*)$/i;
	
	// Try matching
	var result = sURL.match(url);

	// If null, then definately not a URL
	if (result != null) {	
		if (result[1] != "http" && result[1] != "https" && result[1] != "ftp") { 
			alert("Your link must begin with a valid protocol such as: \n http://, https:// or ftp://");
			return false;
		} else {
			return true;
		}
	} else {
		alert("The URL specified is not valid.");
		return false;
	}
}

// Checks for HTML tags
function hasHTML(sText) {

	// Check for "less-than" and "greater-than" symbols
	if ((sText.match(g_ltEXP) != null) ||
			(sText.match(g_gtEXP) != null)) {
		return true;
	} else {
		return false;
	}
}

// Trims leading and trailing spaces
function Trim(sText) {

	if (sText != "" && sText != null) {
		// trim leading spaces
		while (sText.charAt(0) == " ") {
			sText = sText.substring(1, sText.length);
		}
		// trim trailing spaces
		while (sText.charAt(sText.length - 1) == " ") {
			sText = sText.substring(0, sText.length - 1);
		}
	}
	return sText;
}

// Checks for null or empty string values
function ItIsEmpty(sText) { 

	if (sText == null) {
		return true;
	} else { 
		if (Trim(sText) == "") {
			return true;
		} else {
			return false;
		}
	}
}

function popHelp(pURL) {
		window.open(pURL, null, 
		"height=450,width=440,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes");
}

// Used when a dynamic link is clicked
function clickIt(sid, pid){	
	document.forms["frmMain"].action = 'default.asp?sid='+sid +'&'+'pid='+pid;
	document.forms["frmMain"].submit();	
} 

// used for the small calendar view
// accepts a hidden field value for navigating forward and backward based on view type
function change_sm_date(p_new_date)
	{
		cal_form.new_date.value = p_new_date;
		cal_form.submit();
	} 

// the main recurse folder function used for fileviewer.asp
function recurse_folder(p_folder_name)
	{
		form_view.hidden_folder.value = p_folder_name;
		form_view.initial_directory.value = p_folder_name;
		form_view.submit();
	}
		
// Temporary way to handle links to file viewer links

function ShowMSDS() {
	recurse_folder("msds");
}

function ShowTDS() {
	recurse_folder("tds");
}

function ShowPolicies() {
	recurse_folder("policies");
}

function ShowISO() {
	recurse_folder("ISO");
}

function ShowHSETraining() {
	recurse_folder("HSE_Training");
}

function ShowLesonalSBTDS() {
	recurse_folder("Les_SB_TDS");
}

function ShowLesonalSBMSDS() {
	recurse_folder("Les_SB_MSDS");
}

function BranchSOP() {
	recurse_folder("Branch_SOP");
}

function ShowHSE() {
	recurse_folder("HSE");
}	




	
	
