var imageTagsInError = new Array();

function loadErrorImage(imageTag, imagePath) {

	var foundImage = false;
	var i = 0;
	
	while (!foundImage && (i < imageTagsInError.length)) {
		foundImage = (imageTag == imageTagsInError[i]);
		i++;
	}
	
	if (!foundImage) {
		imageTag.src = imagePath;
		imageTagsInError[imageTagsInError.length] = imageTag;
	}
}

function expandPageRange(rangeString) {

	var pageArray = new Array();
	
	if (rangeString && (rangeString.length > 0)) {
		var pages = rangeString.split(",");
		var i = 0;
		
		for (ii = 0; ii < pages.length; ii++) {
			if (pages[ii].indexOf("-") > -1) {
				var p1 = parseInt(pages[ii].split("-")[0]);
				var p2 = parseInt(pages[ii].split("-")[1]);
				
				for (iii = p1; iii <= p2; iii++) {
					pageArray[i] = iii;
					i++;
				}
			}
			else {
				pageArray[i] = parseInt(pages[ii]);
				i++;
			}
		}
	}
	
	return pageArray;
}

function collapsePageRange(pageArray) {

	var rangeString = "";
	var p1 = -1;
	var p2 = -1;
	
	for (i = 0; i < pageArray.length; i++) {
		if (pageArray[i] > (p2 + 1)) {
			if (p1 != p2) {
				rangeString += "-" + p2;
			}
			
			rangeString += "," + pageArray[i];
			p1 = pageArray[i];
			p2 = pageArray[i];
		}
		else {
			p2 = pageArray[i];
		}
	}
	
	if (p1 != p2) {
		rangeString += "-" + p2;
	}
	
	if (rangeString.length > 0) {
		rangeString = rangeString.substring(1);
	}
	
	return rangeString;
}

function utilSetCookie(name, value, expYears, expMonths, expDays, expHours, expMinutes, expSeconds) {

	var currentExpiration = utilGetCookie(name, true);
	value += "|";
	
	if (!currentExpiration) {
		if (expYears != null) {
			var expires = new Date();

			expires.setFullYear(expires.getFullYear() + expYears);
			if (expMonths) expires.setMonth(expires.getMonth() + expMonths);
			if (expDays) expires.setDate(expires.getDate() + expDays);
			if (expHours) expires.setHours(expires.getHours() + expHours);
			if (expMinutes) expires.setMinutes(expires.getMinutes() + expMinutes);
			if (expSeconds) expires.setSeconds(expires.getSeconds() + expSeconds);
			
			value += expires.toGMTString() + ";expires=" + expires.toGMTString();
		}
	}
	else if (currentExpiration.length > 0) {
		value += currentExpiration + ";expires=" + currentExpiration;
	}

	document.cookie = name + "=" + value;
}

function utilGetCookie(name, returnExpirationDate) {

	cookieValue = null;
	
	if ((document.cookie.length > 0) && (document.cookie.indexOf(name) > -1)) {
		cookieValue = document.cookie.substring(document.cookie.indexOf(name));

		if (returnExpirationDate) {
			cookieValue = cookieValue.substring(cookieValue.indexOf("|") + 1);

			if (cookieValue.indexOf(";") > -1) {
				cookieValue = cookieValue.substring(0, cookieValue.indexOf(";"));
			}
		}
		else if (cookieValue.indexOf("|") > -1) {
			cookieValue = cookieValue.substring(cookieValue.indexOf("=") + 1, cookieValue.indexOf("|"));
		}
		else {
			cookieValue = cookieValue.substring(cookieValue.indexOf("=") + 1);
		}
	}
	
	return cookieValue;
}

function e(elementId) {
	
	return document.getElementById(elementId);
}
		
function fadeDiv(divId, curOpacity, finOpacity) {

	var div = e(divId);
	
	if (div) {
		if (finOpacity > 0) {
			div.style.display = "block";
			div.style.visibility = "visible";
		}
		
		div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + curOpacity + "),alpha(Opacity=" + curOpacity + ");"; /* IE, Older IE */
		div.style.opacity = (curOpacity == 100 ? "1.00" : "0." + curOpacity); /* Mozilla, Safari, and Opera */

		if (curOpacity != finOpacity) {
			curOpacity = Math.round(((finOpacity - curOpacity) / 3) + curOpacity);
			
			var diff = finOpacity - curOpacity; 	
			
			if ((diff >= -10) && (diff <= 10)) {
				curOpacity = finOpacity;
			}
			
			setTimeout("fadeDiv('" + divId + "', " + curOpacity + ", " + finOpacity + ")", 25);
		}
		else if (finOpacity == 0) {
			div.style.visibility = "hidden";
			div.style.display = "none";
		}
		else if (finOpacity == 100) {
			div.style.filter = null;
			div.style.opacity = null;
		}
	}
}

function stripWhiteSpace(str) {
    
	var regEx = / /g;
	str = str.replace(regEx, "");
	    
	regEx = /\r/g;
	str = str.replace(regEx, "");
	    
	regEx = /\n/g;
	return str.replace(regEx, "");
}
  
function trim(str) {
    
	if (str.length > 0) {
    while ((str.charAt(0) == " ") || 
        (str.charAt(0) == "\n") || 
        (str.charAt(0) == "\r")) {
      str = str.substring(1);
    }
	    
    while ((str.charAt(str.length - 1) == " ") || 
        (str.charAt(str.length - 1) == "\n") ||
        (str.charAt(str.length - 1) == "\r")) {
      str = str.substring(0, str.length - 1);
    }
   }
    
   return str;
}
  
function removeSpecialWebChars(str, replace) {
  return removeSpecialChars(str, "&\\\"'<>", replace);
}
  
function removeSpecialChars(str, specialChars, replace) {
  
  var sc;
  var regEx;
  var replaceWith = "";
    
  for (i = 0; i < specialChars.length; i++) {
    sc = specialChars.charAt(i);

    if (sc == "\\") {
      sc = "\\\\";
    }

    if (replace) {
      if (sc == "&") {
        replaceWith = "and";
      }
      else if (sc == "<") {
        replaceWith = "less than";
      }
      else if (sc == ">") {
        replaceWith = "greater than";
      }
      else if (sc == "\\\\") {
        replaceWith = "/";
      }
      else {
        replaceWith = "";
      }
    }

    regEx = new RegExp(sc, "g");
    str = str.replace(regEx, replaceWith);
  }
    
  return str;
}
  
function verifyAndFormatDate(dateField, format) {
    
  var formattedEndDate = verifyDate(dateField.value, format); 

  if (formattedEndDate != "NaD") {
    dateField.style.color = "";
    dateField.value = formattedEndDate;
  }
  else {
    dateField.style.color = "#f00";
  }
}

function verifyDate(inDate, outDateFormat) {
  
	var tmpDate = trim(inDate);
	var outDate = "";
				
	if (tmpDate.length > 0) {
		var month;
		var day;
		var year;
			
		// Strip dividers
		var regEx = /-/g;


		if (tmpDate.search(regEx) > -1) {
			for (i = 0; i < 10; i++) {
				var regEx2 = new RegExp("-" + i + "-", "g");
				tmpDate = tmpDate.replace(regEx2, "-0" + i + "-");
			}
		
			tmpDate = tmpDate.replace(regEx, "");
		
		}
		else {
			regEx = /\//g;
	
			if (tmpDate.search(regEx) > -1) {
				for (i = 0; i < 10; i++) {
					var regEx2 = new RegExp("/" + i + "/", "g");
					tmpDate = tmpDate.replace(regEx2, "/0" + i + "/");
				}
			}
			tmpDate = tmpDate.replace(regEx, "");
		}
			
		// Extract month, day, and year depending on length

		if (tmpDate.length == 4) {
			month = tmpDate.substring(0,1) - 1;
			day   = tmpDate.substring(1,2);
			year  = tmpDate.substring(2);
		}
		else if (tmpDate.length == 5) {
			month = 0 + tmpDate.substring(0,1) - 1;
			day   = tmpDate.substring(1,3);
			year  = tmpDate.substring(3);
		}
		else if (tmpDate.length == 6) {
			month = tmpDate.substring(0,2) - 1;
			day   = tmpDate.substring(2,4);
			year  = tmpDate.substring(4);
		}
		else if (tmpDate.length == 7) {
			month = 0 + tmpDate.substring(0,1) - 1;
			day   = tmpDate.substring(1,3);
			year  = tmpDate.substring(3);
		}
		else if (tmpDate.length == 8) {
			month = tmpDate.substring(0,2) - 1;
			day   = tmpDate.substring(2,4);
			year  = tmpDate.substring(4);
		}
			
		if (year.length == 2) {
			if (year >= 90) {
				year = "19" + year;
			}
			else {
				year = "20" + year;
			}
		}
		
		// Create a date object from the extracted month, day, and year.
		var dateObj = new Date(year, month, day, 0, 0, 0);
			
		if ((dateObj != "NaN") && (dateObj != "Invalid Date")) {
			// If we have a valid date then put it into the given format
			month = "0" + (dateObj.getMonth() + 1);
			month = month.substring(month.length - 2);
			day = "0" + dateObj.getDate();
			day = day.substring(day.length - 2);
			year = "" + dateObj.getFullYear();
			tmpDate = outDateFormat;
	
			regEx = /mm|MM/g;
			tmpDate = tmpDate.replace(regEx, month);
			regEx = /dd|DD/g;
			tmpDate = tmpDate.replace(regEx, day);
			regEx = /yyyy|YYYY/g;
			tmpDate = tmpDate.replace(regEx, year);
			regEx = /yy|YY/g;
			tmpDate = tmpDate.replace(regEx, year.substring(year.length - 2));
			
			outDate = tmpDate;
		}
		else {
			outDate = "NaD";
		}
	}
		
	return outDate;
}

function isEmpty(str) {
    return ((str == null) || (str.length == 0));
}     

function isNumeric(str) {
	
	if (str && (str.indexOf("-") == 0)) {
		str = str.substring(1);
	}
	
	var pattern = /^\d+$/;
	return (pattern.test(str));
}
