//browser detection can be done many ways, this way gets the job done for now
var agent = navigator.userAgent.toLowerCase();
var is_ie = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));

function trim(strText) {
		// this will get rid of leading spaces 
		while (strText.substring(0,1) == ' ') 
				strText = strText.substring(1, strText.length);
		// this will get rid of trailing spaces 
		while (strText.substring(strText.length-1,strText.length) == ' ')
				strText = strText.substring(0, strText.length-1);
	 return strText;
}

function pad(value, padChar, strLength) {
	var newValue = value.toString();

	for (var i = value.length; i < strLength; i++) {
		newValue = padChar + newValue;
	}

	return newValue;
}

function calPad(value) { //2 digit left pad, 7 > "07"
	var newValue = value.toString();
	var valueLength = newValue.length;

	for (var i = valueLength; i < 2; i++) {
		newValue = "0" + newValue;
	}

	return newValue;
}

function noZeroPad(value) {
	return value * 1;
}

function checkEmptyRequired(field) {
	if (trim(field.value) == "") {
		field.className = "textfield_err";
	} else {
		field.className = "textfield";
	}
}

function setIfEmpty(item, value) {
	if (trim(item.value) == "") item.value = value;
}

function setIfZeroNum(item, value) {
	if ((1 * trim(item.value)) == 0 || trim(item.value) == "") item.value = value;
}

function checkForIntegerChar(e, f_value) {
	key_pressed = (is_ie) ? event.keyCode : e.which;

	var is_valid_key = (key_pressed == 0 || key_pressed == 8 || (key_pressed >= 48 && key_pressed <= 57))? true: false;
	//allow [Backspace] [Delete] 0-9

	if (is_valid_key == false) {
		if (is_ie){
			event.returnValue = false;
			return false;
		} else {
			return false;
		}
	}
}

function isEmpty(value) {
	if (trim(value) == "") {
		return true;
	} else {
		return false;
	}
}

function verifyIsValidContent(type, obj, class_norm, class_alert) {
	var is_valid;

	switch(type) {
		case "text":
			is_valid = (trim(obj.value) != "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		case "integer":
			var filter = /^([0-9])*$/;
			is_valid = (filter.test(obj.value) && obj.value != "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		case "integerOrBlank":
			var filter = /^([0-9])*$/;
			is_valid = (filter.test(obj.value) || trim(obj.value) == "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		case "image_file":
			//nothing - .jpg, .jpeg, .gif, .png
			break;
		case "decimalLatLon":
			var filter = /^([1-9]{1}[0-9]{0,2})(\.([0-9])+)*$/;
			is_valid = (filter.test(obj.value) || trim(obj.value) == "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		case "string":
			var filter = /^([a-zA-z])+.*$/;
			is_valid = (filter.test(obj.value) && obj.value != "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		case "stringOrBlank":
			var filter = /^([a-zA-z])+.*$/;
			is_valid = (filter.test(obj.value) || trim(obj.value) == "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		case "pjn":
			var filter = /^([0-9]{6})$/;
			is_valid = (filter.test(obj.value) && obj.value != "")? true: false;
			obj.className = (is_valid)? class_norm: class_alert;
			break;
		default:
			//nothing
	}

	return is_valid;
}

function verifyIsValidChar(type, e, value) {
	var key_pressed = (is_ie)? e.keyCode : e.which; //var key_pressed = (is_ie)? event.keyCode : e.which;
	
	var valid_key = false;

	var evt = (is_ie)? window.event: e;
	var ctrlPressed = false;
	if (evt.ctrlKey || evt.metaKey) ctrlPressed = true;

	switch(type) {
		case "text":
			valid_key = true;
			break;
		case "integer":
			valid_key = (key_pressed == 0 || key_pressed == 8 || (key_pressed >= 48 && key_pressed <= 57))? true: false;
			break;
		case "decimal":
			valid_key = (key_pressed == 0 || key_pressed == 8 || (key_pressed >= 48 && key_pressed <= 57) || key_pressed == 46)? true: false;
			break;
		default:
			//nothing
	}

	if ((key_pressed == 67 || key_pressed == 99) && ctrlPressed) valid_key = true; //copy
	if ((key_pressed == 86 || key_pressed == 118) && ctrlPressed) valid_key = true; //paste

	if (!valid_key) {
		if (is_ie) {
			event.returnValue = false;
			return false;
		} else {
			return false;
		}
	}
}

function imageFileValid(filename) {
	if (filename.length == 0) {
		return true;
	} else { //only check file extension validity if a filename has been specified
		var valid_image = false;
		var valid_img_arr = new Array(".jpg", ".jpeg", ".gif", ".png");
		var file_arr = filename.split(".");
		var ext = "." + (file_arr[file_arr.length - 1]).toLowerCase();
		for (i = 0; i < valid_img_arr.length; i++) {
			if (ext == valid_img_arr[i]) valid_image = true;
		}
		return valid_image;
	}
}

function verifyDateValid(year, month, day, hours, mins, secs) {
	//need to subtract 1 for month
	var valDate = new Date(year, (parseInt(month) - 1), day, hours, mins, secs, 0);
	
	var isValidDate = (valDate.getFullYear() == parseInt(year) && valDate.getMonth() == (parseInt(month) - 1) && valDate.getDate() == parseInt(day) && valDate.getHours() == parseInt(hours) && valDate.getMinutes() == parseInt(mins) && valDate.getSeconds() == parseInt(secs))? true: false;

	return isValidDate;
}

function formatForTextCompare(text) {
	text = text.replace(/\'/g, "&#39;");
	text = text.replace(/\"/g, "&#34;");
	return text;
}

function setToVis(obj) {
	document.getElementById(obj).style.visibility = "visible";
}

function setToInVis(obj) {
	document.getElementById(obj).style.visibility = "hidden";
}

function getWindowDimensions() {
	var myWidth = 0 //width of window
	var myHeight = 0; //height of window
	var myScrollX = 0; //current scroll distance from left
	var myScrollY = 0 //current scroll distance from top
	var myScrollWidth = 0; //height of page content
	var myScrollHeight = 0; //height of page content
	
	if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //DOM
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
    myScrollX = document.body.scrollLeft;
    myScrollY = document.body.scrollTop;
    myScrollWidth = document.body.scrollWidth;
    myScrollHeight = document.body.scrollHeight;
   
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6 in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
    myScrollX = document.documentElement.scrollLeft;
    myScrollY = document.documentElement.scrollTop;
    myScrollWidth = document.documentElement.scrollWidth;
    myScrollHeight = document.documentElement.scrollHeight;
    
  } else if (typeof(window.innerWidth) == 'number') {
    //Netscape
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
    myScrollX = window.pageXOffset;
    myScrollY = window.pageYOffset;
    myScrollWidth = window.scrollWidth;
    myScrollHeight = window.scrollHeight;
  }
  
  var myPageWidth = (myScrollWidth > myWidth)? myScrollWidth: myWidth;
  var myPageHeight = (myScrollHeight >  myHeight)? myScrollHeight: myHeight;
  
  //window.alert("width: "+myWidth+", height: "+myHeight+", scrollX: "+myScrollX+", scrollY: "+myScrollY+", pageWidth: "+myPageWidth+", pageHeight: "+myPageHeight);
  
	return {width: myWidth, height: myHeight, scrollX: myScrollX, scrollY: myScrollY, pageWidth: myPageWidth, pageHeight: myPageHeight};
}
