
function openWindow(strUrl,strWinName,iLocation,iMenubar,iResizable,iScrollbars,iStatus,iWidth,iHeight) {
	var winHnd = window.open(strUrl,strWinName, + "'location=" + iLocation + ",menubar=" + iMenubar + ",resizable=" + iResizable + ",scrollbars=" + iScrollbars + ",status=" + iStatus + ",width=" + iWidth + ",height=" + iHeight + "'");
}

function unFormatTextAreaField(str) {

	var regCarriageReturn = /<br>/g;
	// Replace links
	str = new String(str);
	return str.replace(regCarriageReturn,"\r\n");
}

function unescapeQuote(str) {

	var regDoubleQuote = /&#34;/g;
	// Replace links
	str = new String(str);
	return str.replace(regDoubleQuote,"\"");
}

function checkSpaces(fieldvalue, fieldname) {
	var regEx = /\W+/;
	if(regEx.test(fieldvalue))
		errorStr += fieldname + " may not contain spaces.\n";
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isWhitespace(s) {
	var reWhitespace = /^\s+$/
	return (isEmpty(s) || reWhitespace.test(s));
}

function testEmail(strEmail) {
    var regEmail = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";

    var regEx = new RegExp(regEmail);
    return regEx.test(strEmail);
}

function checkRequiredField(fieldvalue, fieldname) {
	if (isWhitespace(fieldvalue)) {
		errorStr += "Please enter a value for the field: '" + fieldname + "' \n";
	}
}

function checkCharacterSize(fieldvalue, fieldname, size) {
	// Don't count carriage returns
	if (fieldvalue.length < size) {
		errorStr += "Sorry, '" + fieldname + "' field must be at least " + size + " characters.\n";
	}
}

function checkCharacterLimit(fieldvalue, fieldname, size) {
	// Don't count carriage returns
	if (fieldvalue.length > size) {
		errorStr += "Sorry, '" + fieldname + "' field must be limited to " + size + " characters.\n";
	}
}

function checkSelectField(fieldvalue, fieldname) {
	if(fieldvalue==-1)
		errorStr += "Please select a value for the field '" + fieldname +"'\n";
}

function checkValidDate(fieldvalue, fieldname) {
    if (checkForDate(fieldvalue)) {	//make sure it's a date, not characters
		if (isDate(fieldvalue)) { //make sure date is valid - ie 11/31/2003 is not valid
			// make sure date gets passed in mm/dd/yyyy format
			//form.fieldname.value = FormatDate(fieldvalue,"mm/dd/yyyy");
		} else  {
			errorStr += "Please enter a valid date for " + fieldname + "\n";
		}
	} else  {
	   errorStr += "Please enter a valid date in mm/dd/yyyy format.\n";
    }	
}

function LeftTrim(strToTrim,TrimChar) {
	strToTrim += ""
	TrimChar += ""
	if((TrimChar == "") || (!(TrimChar.length == 1)))
		TrimChar = " "
	if(strToTrim.length == 0)
		return(strToTrim)
	var Count = 0
	for(Count = 0;Count < strToTrim.length;Count++)
	{
		if(!(strToTrim.charAt(Count) == TrimChar))
			return(strToTrim.substring(Count,strToTrim.length))
	}
	return("")
}

function RightTrim(strToTrim,TrimChar) {
	strToTrim += ""
	TrimChar += ""
	if((TrimChar == "") || (!(TrimChar.length == 1)))
		TrimChar = " "
	if(strToTrim.length == 0)
		return(strToTrim)
	var Count = 0
	for(Count = strToTrim.length -1;Count >= 0;Count--)
	{
		if(!(strToTrim.charAt(Count) == TrimChar))
			return(strToTrim.substring(0,Count + 1))
	}
	return("")
}

function AllTrim(strToTrim,TrimChar) {
	 strToTrim += ""
	 TrimChar += ""
	 if((TrimChar == "") || (!(TrimChar.length == 1)))
	  TrimChar = " "
	 return(RightTrim(LeftTrim(strToTrim,TrimChar),TrimChar))
}

function GetFieldAttributes(value,mode) {
	var arrSplitValue = value.split("_");

	if(value!=-1)
	{
		if(mode=="add")
			document.location.href="add_field.asp?fieldviewid=" + arrSplitValue[0] + "&fieldtypeid=" + arrSplitValue[1] + "&multipleon=" + arrSplitValue[2];
		else
			document.location.href="edit_field.asp?fieldviewid=" + arrSplitValue[0] + "&fieldid=" + document.editform.FieldID.value + "&fieldtypeid=" + arrSplitValue[1] + "&multipleon=" + arrSplitValue[2];
	}
}

function AddOption() {
	var iOptionLength = parseInt(document.editform.OptionText.options.length);
	var strOption = (prompt("Please enter the option.","Type option text here."));
	if(strOption!=null)
		document.editform.OptionText.options[iOptionLength]=new Option(strOption);
}

function EditOption(value) {
	if(parseInt(value)!=-1)
	{
		var iInd = document.editform.OptionText.options[value].value
		var strOption = (prompt("Please enter the option.",document.editform.OptionText.options[value].text));
		if(strOption!=null)
		{
			document.editform.OptionText.options[parseInt(value)]=new Option(strOption);
			document.editform.OptionText.options[parseInt(value)].value=iInd;
		}
	}
	else
	{
		alert("Please select an item to edit.\n");
	}
}

function RemoveOption(value) {
//	var strEmailDelete = new String(document.editform.EmailDelete.value);

	if(parseInt(value)!=-1)
	{
//		if(strEmailDelete!="")
//			document.editform.EmailDelete.value+= "|" + document.editform.OptionText.options[parseInt(value)].value;
//		else
//			document.editform.EmailDelete.value+=document.editform.OptionText.options[parseInt(value)].value;

		document.editform.OptionText.options[value] = null;
	}
	else
	{
		alert("Select an option to remove.");
	}
}

//
// 1/27/2004
// Date Validation functions for Data Field Type
//
//
function checkForDate(dateToCheck) {
	if(dateToCheck=="") return true;
   	var newDate = new Date();
  	newDate.setTime(Date.parse(dateToCheck));

  	if (isNaN(parseFloat(newDate.getFullYear()))||isNaN(parseFloat(newDate.getMonth()))||isNaN(parseFloat(newDate.getDate()))) {
		return false;
   	}
	return true;
}
function isDate(dateToCheck){
	if(dateToCheck=="") return true;

	var m_strDate = formatDate(dateToCheck,"mm/dd/yyyy");
	//alert ("date is now " + m_strDate);
	var m_arrDate = m_strDate.split("/");
	var m_MONTH = String(m_arrDate[0]);
	var m_DAY = String(m_arrDate[1]);
	var m_YEAR = String(m_arrDate[2]);

	// if month starts with 0, remove the first 0 for proper parsing
	if (m_MONTH.charAt(0)=="0" && m_MONTH.length==2){
		m_MONTH = m_MONTH.charAt(1);
	}

	// if day starts with 0, remove the first 0 for proper parsing
	if (m_DAY.charAt(0)=="0" && m_DAY.length==2){
		m_DAY = m_DAY.charAt(1);
	}

	//alert("date parsed is " + parseInt(m_MONTH) + "/" + parseInt(m_DAY) + "/" + parseInt(m_YEAR));

	return internationalDateCheck(parseInt(m_MONTH), parseInt(m_DAY), parseInt(m_YEAR));

}
function internationalDateCheck(month, day, year){
	if (day<1) return false;
	if (year>9999) return false;
	if (month==4 || month==6 || month==9 || month==11) {// months with 30 days
		if(day>30) return false;
	}
	else if (month==2) {// february, leap year
		var g=parseInt(year/4);
		if(isNaN(g)) return false;
		if(day>29) return false;
		if(day==29 && ((year/4)!=parseInt(year/4))) return false;
	}
	else {// all other months have 31 days
		if (day>31) return false;
	}
	return true;
}
function formatDate(DateToFormat,FormatAs){
	if(DateToFormat=="") return "";
	
	if(!FormatAs){FormatAs="mm/dd/yyyy";}

	var strReturnDate;
	FormatAs = FormatAs.toLowerCase();
	DateToFormat = DateToFormat.toLowerCase();
	var arrDate;
	var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var strMONTH;
	var Separator;

	while(DateToFormat.indexOf("st")>-1){
		DateToFormat = DateToFormat.replace("st","");
	}
	while(DateToFormat.indexOf("nd")>-1){
		DateToFormat = DateToFormat.replace("nd","");
	}
	while(DateToFormat.indexOf("rd")>-1){
		DateToFormat = DateToFormat.replace("rd","");
	}
	while(DateToFormat.indexOf("th")>-1){
		DateToFormat = DateToFormat.replace("th","");
	}

	if(DateToFormat.indexOf(".")>-1) Separator = ".";
	if(DateToFormat.indexOf("-")>-1) Separator = "-";
	if(DateToFormat.indexOf("/")>-1) Separator = "/";
	if(DateToFormat.indexOf(" ")>-1) Separator = " ";

	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
	
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
			DateToFormat += arrDate[iSD] + Separator;
		}
	}
	
	DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
	arrDate = DateToFormat.split(Separator);

	if(arrDate.length < 3){
		return "";
	}

	var DAY = arrDate[0];
	var MONTH = arrDate[1];
	var YEAR = arrDate[2];

	if(parseFloat(arrDate[1]) > 12){
		DAY = arrDate[1];
		MONTH = arrDate[0];
	}

	if(parseFloat(DAY) && DAY.toString().length==4){
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
	}
	for(var iSD = 0;iSD < arrMonths.length;iSD++){
		var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
		if(MonthPosition > -1){
			MONTH = iSD + 1;
			if(MonthPosition == 0){
				DAY = arrDate[1];
				YEAR = arrDate[2];
			}
			break;
		}
	}

	var strTemp = YEAR.toString();
	if(strTemp.length==2){
		YEAR = "20" + YEAR;
	}
	
	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
		MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
		DAY = "0" + DAY;
	}
	switch (FormatAs){
		case "dd/mm/yyyy": return DAY + "/" + MONTH + "/" + YEAR;
		case "mm/dd/yyyy": return MONTH + "/" + DAY + "/" + YEAR;
		case "dd/mmm/yyyy": return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
		case "mmm/dd/yyyy": return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
		case "dd/mmmm/yyyy": return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
		case "mmmm/dd/yyyy": return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}
	return DAY + "/" + strMONTH + "/" + YEAR;
}

function toggle(divId, imageId) {
	if (document.getElementById(divId).style.display == "none") {
		document.getElementById(divId).style.display = "block";
		document.getElementById(imageId).src = "../images/icon_tree_collapse.gif";
	}
	else {
		document.getElementById(divId).style.display = "none";
		document.getElementById(imageId).src = "../images/icon_tree_expand.gif";	

	}
}
