//<script>
//HEADER
//
//FILENAME		Common.js	
//AUTHOR		Elena Melekhina
//DATE			11 July 2002
//DESCRIPTION	This file contains the common jscript (including validation) used
//				by the Whatson application
//
//END HEADER

//MODIFICATION HISTORY
//
//NAME		DATE		COMMENTS
//--------- ----------- ----------------------------------------------------------
//
//
//END MODIFICATION HISTORY

//.................................................................................

function isValidCharacter(sStringToTest) {
	var re = /[^\u0100\u0101\u0112\u0113\u012a\u012b\u014c\u014d\u016a\u016b\xC4\xE4\xCB\xEB\xCF\xEF\xD6\xF6\xDC\xFCa-zA-Z0-9' -]/;
	var isBadChar = re.test(sStringToTest);
	if (!isBadChar) {
		var re2 = /[^'-]/;
		isBadChar = !re2.test(sStringToTest);
	}
	return (!isBadChar);
}

function setDateFormat(sObject, sDateToFormat) {
	/*	
		This function does several things: 
		1-	checks to see if the string matches one of the application
			date formats, namely, dd mmm yyyy or dd/mm/yyyy (incl. dd mmm yy or 
			dd/mm/yy). If not, nothing is done and the date format error is 
			reported during page validation.
		2-	once a ligitimate date passes step 1, it is reformatted in the primary 
			format, i.e. dd mmm yyyy
	*/	
	var sReFormattedDate = ""
	if (isDate(sDateToFormat) == 1) {	
		sReFormattedDate = getDateFormat(sDateToFormat)
		sObject.value = sReFormattedDate
	}
}

//.................................................................................
function getFullYear(oDate) {
// Used by NN4 browser as the Date.getFullYear() is not supported.
	var year = oDate.getYear();
	return (year < 1000)?(year + 1900):year;
}

function isDate(sDateToTest) {
	/*
		Validates that the input string is a valid date
		uses Regular Expressions to validate the format

		Matches:	dd mon yyyy	(primary date format 1)
					dd mon yy	(primary date format 2)
					dd/mm/yyyy	(secondary date format 1)
					dd/mm/yy	(secondary date format 2)
					
		Returns:	0	-	Invalid Format
					1	-	Valid Date
					2	-	Invalid Date
		
	*/
	//var rePrimary1 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(18|19|20|21|22|23|24)(\d{2})$/;
	//var rePrimary2 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(\d{2})$/;
	var rePrimary1 = /^(0?[1-9]|[0-9][0-9])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(18|19|20|21|22|23|24)(\d{2})$/;
	var rePrimary2 = /^(0?[1-9]|[0-9][0-9])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(\d{2})$/;
	//var reSecondary1 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\/])(0?[1-9]|1[0-2])([\/])(18|19|20|21|22|23|24)(\d{2})$/;
	//var reSecondary2 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\/])(0?[1-9]|1[0-2])([\/])(\d{2})$/;
	var reSecondary1 = /^(0?[1-9]|[0-9][0-9])([\/])(0?[1-9]|[0-9][0-9])([\/])(18|19|20|21|22|23|24)(\d{2})$/;
	var reSecondary2 = /^(0?[1-9]|[0-9][0-9])([\/])(0?[1-9]|[0-9][0-9])([\/])(\d{2})$/;
	
	var iCenturyCutoff = getFullYear(new Date()) - 1999
	
	var nMonth, nValid = 0

	if (sDateToTest != "") {
		if (rePrimary1.test(sDateToTest.toUpperCase())) {
			/*
				Convert to mm/dd/yyyy (parseInt is used to remove any leading zeroes)
			*/
			if (isNaN(RegExp.$3)) {
				/*	Date use 3 character month name so convert to number */
  				var sMonthNames = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
				nMonth = sMonthNames.indexOf(RegExp.$3)/3 + 1
			}
			var sDate = nMonth + "/" + parseInt(RegExp.$1, 10) + "/" + RegExp.$5 + RegExp.$6
			/*
				Date.parse() requires a month/day/year format when the month is a number
				NOTE: Date.parse() will make an invalid date valid by adjusting the days!
				e.g. 31/04/2000 becomes 1/05/2000
				Therefore by parsing the date and converting it back to a string we
				can check it was a valid date by comparing the before and after strings
			*/
			var d = new Date(Date.parse(sDate))
			/*
				Convert back to a string in the same format as our starting date
				For some weird reason getMonth() returns a number between 0 and 11
			*/
			var sDate2 = eval(d.getMonth() + 1) + "/" + d.getDate() + "/" + getFullYear(d)
			if (sDate2 == sDate) {
				nValid = 1;
			}
			else {
				nValid = 2;
			}
		}
		if (rePrimary2.test(sDateToTest.toUpperCase())) {
			/*
				Convert to mm/dd/yyyy (parseInt is used to remove any leading zeroes)
			*/
			if (isNaN(RegExp.$3)) {
				/*	Date use 3 character month name so convert to number */
  				var sMonthNames = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
				nMonth = sMonthNames.indexOf(RegExp.$3)/3 + 1
			}
			var sCentury = ""
			var sYear = parseInt(RegExp.$5, 10)
			if (sYear < iCenturyCutoff) {
				sCentury = "20"
			}
			else {
				sCentury = "19"
			}
			var sDate = nMonth + "/" + parseInt(RegExp.$1, 10) + "/" + sCentury + RegExp.$5
			/*
				Date.parse() requires a month/day/year format when the month is a number
				NOTE: Date.parse() will make an invalid date valid by adjusting the days!
				e.g. 31/04/2000 becomes 1/05/2000
				Therefore by parsing the date and converting it back to a string we
				can check it was a valid date by comparing the before and after strings
			*/
			var d = new Date(Date.parse(sDate))
			/*
				Convert back to a string in the same format as our starting date
				For some weird reason getMonth() returns a number between 0 and 11
			*/
			var sDate2 = eval(d.getMonth() + 1) + "/" + d.getDate() + "/" + getFullYear(d)
			if (sDate2 == sDate) {
				nValid = 1;
			}
			else {
				nValid = 2;
			}
		}
		if (reSecondary1.test(sDateToTest)) {
			/*
				It is a valid format but is it a valid date? ie not 30/02/2000
				Convert to mm/dd/yyyy (parseInt is used to remove any leading zeroes)
			*/
			var sDate = parseInt(RegExp.$3, 10) + "/" + parseInt(RegExp.$1, 10) + "/" + RegExp.$5 + RegExp.$6
			/*
				Date.parse() requires a month/day/year format when the month is a number
				NOTE: Date.parse() will make an invalid date valid by adjusting the days!
				e.g. 31/04/2000 becomes 1/05/2000
				Therefore by parsing the date and converting it back to a string we
				can check it was a valid date by comparing the before and after strings
			*/
			var d = new Date(Date.parse(sDate))
			/*
				Convert back to a string in the same format as our starting date
				For some weird reason getMonth() returns a number between 0 and 11
			*/
			var sDate2 = eval(d.getMonth() + 1) + "/" + d.getDate() + "/" + getFullYear(d)
			if (sDate2 == sDate) {
				nValid = 1;
			}
			else {
				nValid = 2;
			}
		}
		if (reSecondary2.test(sDateToTest)) {
			/*
				It is a valid format but is it a valid date? ie not 11/23/83
				Convert to mm/dd/yyyy (parseInt is used to remove any leading zeroes)
			*/
			var sCentury = ""
			var sYear = parseInt(RegExp.$5, 10)
			if (sYear < iCenturyCutoff) {
				sCentury = "20"
			}
			else {
				sCentury = "19"
			}
			var sDate = parseInt(RegExp.$3, 10) + "/" + parseInt(RegExp.$1, 10) + "/" + sCentury + RegExp.$5
			/*
				Date.parse() requires a month/day/year format when the month is a number
				NOTE: Date.parse() will make an invalid date valid by adjusting the days!
				e.g. 31/04/2000 becomes 1/05/2000
				Therefore by parsing the date and converting it back to a string we
				can check it was a valid date by comparing the before and after strings
			*/
			var d = new Date(Date.parse(sDate))
			/*
				Convert back to a string in the same format as our starting date
				For some weird reason getMonth() returns a number between 0 and 11
			*/
			var sDate2 = eval(d.getMonth() + 1) + "/" + d.getDate() + "/" + getFullYear(d)
			if (sDate2 == sDate) {
				nValid = 1;
			}
			else {
				nValid = 2;
			}
		}
	} 
	return nValid;
}

//.................................................................................
function isPrimaryDateFormat(sDateToTest) {
	var re = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(19|20|21|22|23|24)(\d{2})$/;
	var bValid = false
	
	if (sDateToTest != "") {
		if (re.test(sDateToTest.toUpperCase())) {
			bValid = true
		}
	}
	return bValid
}

//.................................................................................

function getDateFormat(sDateToTest) {
	var rePrimary1 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(18|19|20|21|22|23|24)(\d{2})$/;
	var rePrimary2 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\ ])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)([\ ])(\d{2})$/;
	var reSecondary1 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\/])(0?[1-9]|1[0-2])([\/])(18|19|20|21|22|23|24)(\d{2})$/;
	var reSecondary2 = /^(0?[1-9]|1[0-9]|2[0-9]|3[01])([\/])(0?[1-9]|1[0-2])([\/])(\d{2})$/;
	
	var iCenturyCutoff = getFullYear(new Date()) - 1999
	
	var sDate = ""
	
	if (sDateToTest != "") {
		if (rePrimary1.test(sDateToTest.toUpperCase())) {
			sDate = sDateToTest
		}
		if (rePrimary2.test(sDateToTest.toUpperCase())) {
			var nMonth, sMonth
			
			/*	Date use 3 character month name so convert to number */
  			var sMonthNames = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"
			nMonth = sMonthNames.indexOf(RegExp.$3)/3 + 1
			sMonth = getMonthInWords(nMonth)
			var sCentury = ""
			var sYear = parseInt(RegExp.$5, 10)
			if (sYear < iCenturyCutoff) {
				sCentury = "20"
			}
			else {
				sCentury = "19"
			}
			var sDate = RegExp.$1 + " " + sMonth + " " + sCentury + RegExp.$5
		}
		if (reSecondary1.test(sDateToTest)) {
			var sMonth 
			sMonth = getMonthInWords(parseInt(RegExp.$3, 10))
			sDate = RegExp.$1 + " " + sMonth + " " + RegExp.$5 + RegExp.$6
		}
		if (reSecondary2.test(sDateToTest)) {
			var sCentury = ""
			var sYear = parseInt(RegExp.$5, 10)
			if (sYear < iCenturyCutoff) {
				sCentury = "20"
			}
			else {
				sCentury = "19"
			}
			var sMonth 
			sMonth = getMonthInWords(parseInt(RegExp.$3, 10))
			sDate = RegExp.$1 + " " + sMonth + " " + sCentury + RegExp.$5
		}						
	}
	return sDate
}

//.................................................................................

function getMonthInWords(nMonth) {
	switch (nMonth) {
		case 1:	
			return "Jan"
		case 2:
			return "Feb"
		case 3:
			return "Mar"
		case 4:
			return "Apr"
		case 5:
			return "May"
		case 6:
			return "Jun"
		case 7:
			return "Jul"
		case 8:
			return "Aug"
		case 9:
			return "Sep"
		case 10:
			return "Oct"
		case 11:
			return "Nov"
		case 12:
			return "Dec"
	}
}

//.................................................................................

// general purpose function to see if a suspected numeric input 
// is a positive or negative integer 
function isInteger(inputVal) 
{
	var inputStr = inputVal.toString();
	for (var i = 0; i < inputStr.length; i++) 
	{ 
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") return false;  
	} 
	return true; 
} 

function getRadioValue(radioObject) {
	var count;

	if (radioObject[0]) {	
		for (count=0;count<radioObject.length;count++) {
			if (radioObject[count].checked)
				return (radioObject[count].value);
		}
	}
	else {
		if (radioObject)
			return radioObject.value;
	}
	return "";
}

function Trim(inTxt) {
	var inT = new String(inTxt);
	var tmpT, newTxt;
	var ch, p;
	
	p = 0;
	ch = inT.charAt(p);
	while (ch == " ") {
		p = p + 1;
		ch = inT.charAt(p);
	}
	tmpT = inT.substr(p)
	if (tmpT == "") 
		return "";
		
	p = tmpT.length - 1;
	ch = tmpT.charAt(p);
	while (ch == " ") {
		p = p - 1;
		ch = tmpT.charAt(p);
	}
	newTxt = tmpT.substr(0,p+1);	
	return ( newTxt );
}

function TrimLeadingChar(inTxt, chTrim) {
	var inT = new String(inTxt);
	var newTxt;
	var ch, p;
	
	p = 0;
	ch = inT.charAt(p);
	while (ch == chTrim) {
		p = p + 1;
		ch = inT.charAt(p);
	}
	newTxt = inT.substr(p);
	return ( newTxt );
}

function getListValue(oList) {
	var n = oList.selectedIndex;
	return oList.options[n].value;
}

function IsOriginalDateGreater(sOriginal, sCompare, bCheckTime){
// Determines if the original date is greater than another date and returns true or false
// Including the Time in the check is optional.

	var nDate1 = Date.parse(sOriginal);
	var nDate2 = Date.parse(sCompare);
	return (nDate1 < nDate2);

}

//Convert the Unicode Characters (supported only) into Extended ASCII.
function convertFromUnicode(sField) {
	var sObject			= eval(sField);
	var	sObjectValue	= sObject.value;

	var arUnicode = "\u0100|\u0101|\u0112|\u0113|\u012a|\u012b|\u014c|\u014d|\u016a|\u016b".split("|");
	var arANSI  = "\xC4|\xE4|\xCB|\xEB|\xCF|\xEF|\xD6|\xF6|\xDC|\xFC".split("|");
	var re, i;

	for(i=0; i<arUnicode.length; i++) {
		re = new RegExp(arUnicode[i], "g");
		sObjectValue = sObjectValue.replace(re, arANSI[i]);
	}

	sObject.value = sObjectValue;
	return true;
}

function toXML(sName, sValue) {
	var sXML;
	
	if (sName =="") {
		sXML = "";
	}
	else {
		if (sValue == "") {
			sXML = "<" + sName + "/>";
		}
		else {
			sXML = "<" + sName + ">" + sValue  + "</" + sName + ">";
		}
	}
	return sXML;
}

function dateDiffInMinutes(sStartDate, sEndDate) {
//requires already constructed valid StartDate and EndDate
	return (sEndDate - sStartDate)/(60*1000);
}

function dateDiffInDays(sStartDate, sEndDate) {
//requires already constructed valid StartDate and EndDate
	return (sEndDate - sStartDate);
}

function encodeEntities(sXMLText) {
	var sNewText = sXMLText;
	var sReturn;
	sReturn = sNewText.replace(/&/g,'&amp;');
	sNewText = sReturn;
	sReturn = sNewText.replace(/</g, '&lt;');
	sNewText = sReturn;	
	sReturn = sNewText.replace(/>/g, '&gt;');
	sNewText = sReturn;
	sReturn = sNewText.replace(/"/g, '&quot;');	
	sNewText = sReturn;
	sReturn = sNewText.replace(/'/g, '&apos;');
	alert(sReturn);
	return sReturn;
}

function getCheck (ChGrp)
//returns comma delimited list of selected checkbox values
    {
    var inStr = "";
    
    for (var x=0; x< ChGrp.length; x++)
        if (ChGrp[x].checked)
            {
            inStr = inStr + "," + ChGrp[x].value;
            }
    return inStr.substring(1, inStr.length);
    }