var ie = false;
var ns = false;
if (navigator.appName == 'Microsoft Internet Explorer') {
	ie = true;
} else if (navigator.appName == 'Netscape') {
	ns = true;
}

function browserWidth() {
	var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth;
	} else {
		if( document.documentElement &&
			( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			myWidth = document.documentElement.clientWidth;
		} else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				myWidth = document.body.clientWidth;
			}
		}
	}

	return myWidth;
}

function changeKey (textControl, evt, keyChecker) {
	var keyCode = evt.keyCode ? evt.keyCode :
					evt.charCode ? evt.charCode :
					evt.which ? evt.which : void 0;
	var key;

	if (keyCode) {
		key = String.fromCharCode(keyCode);
	}

	var keyCheck = keyChecker(keyCode, key);

	if (keyCode && window.event && !window.opera) {
		if (keyCheck.cancelKey) {
			return false;
		} else if (keyCheck.replaceKey) {
			window.event.keyCode = keyCheck.newKeyCode;
			if (window.event.preventDefault) {
				window.event.preventDefault();
			}
			return true;
		} else {
			return true;
		}
	} else if (typeof textControl.setSelectionRange != 'undefined') {
		if (keyCheck.cancelKey) {
			if (evt.preventDefault) {
				evt.preventDefault();
			}
			return false;
		} else if (keyCheck.replaceKey) {
			if (evt.preventDefault) {
				evt.preventDefault();
			}

			var oldSelectionStart = textControl.selectionStart;
			var oldSelectionEnd = textControl.selectionEnd;
			var selectedText = textControl.value.substring(oldSelectionStart, oldSelectionEnd);
			var newText = typeof keyCheck.newKey != 'undefined'
							? keyCheck.newKey
							: String.fromCharCode(keyCheck.newKeyCode);

			textControl.value = textControl.value.substring(0, oldSelectionStart) + newText + textControl.value.substring(oldSelectionEnd);
			textControl.setSelectionRange(oldSelectionStart + newText.length,
			oldSelectionStart + newText.length);
			return false;
		} else {
			return true;
		} 
	} else if (keyCheck.cancelKey) {
		if (evt.preventDefault) {
			evt.preventDefault();
		}
		return false;
	} else {
		return true;
	}
}

function fillZero(num, length) {
	num = num + '';
	for (var i = num.length; i < length; i++) {
		num = '0' + num;
	}
	return num;
}

function goTo(url) {
	window.location = url;
}


function insertIFrame(obj, iframe) {
	if (ie) {
		var parent = obj.parentNode;
		iframe.setAttribute('style', 'z-index: 2');
		iframe.setAttribute('src', 'javascript: false;');
		iframe.setAttribute('frameborder', '0');
		iframe.setAttribute('scrolling', 'no');
		iframe.style.width = obj.style.width;
		iframe.style.height = obj.style.height;
		iframe.style.top = obj.style.top;
		iframe.style.left = obj.style.left;
		iframe.style.position = obj.style.position;
		parent.insertBefore(this.iframe, obj);
	}
}

function hideAllSelect() {
	var selects = document.getElementsByTagName('SELECT');
	for (var i = 0; i < selects.length; i++) {
		selects[i].style.visibility = 'hidden';
	}
}

function openWindow(Pagina, Largura, Altura, scrollbars, status) {
	window.open(Pagina, '', 'width=' + Largura + ',height=' + Altura + ',scrollbars=' + scrollbars + ',status=' + status + ',toolbar=no,location=no,directories=no,menubar=no,resizable=no,copyhistory=no, top='+(screen.height - Altura)/2+', left='+(screen.width - Largura)/2);
}

function showAllSelect() {
	var selects = document.getElementsByTagName('SELECT');
	for (var i = 0; i < selects.length; i++) {
		selects[i].style.visibility = 'visible';
	}
}

function linkRows(tb, color) {
	var table = document.getElementById(tb);
	if (table) {
		var group = table.getElementsByTagName('TBODY')[0];
		
		if (group) {
			var rows = group.childNodes;
			
			for (var i = 0; i < rows.length; i++) {
				if (rows[i].nodeName == 'TR') {
					rows[i].onmouseover = function() {
						if (this.getAttribute('locked') != 'true') {
							this.oldColor = this.style.backgroundColor;
							this.style.backgroundColor = color;
							this.style.cursor = 'hand';
							this.style.cursor = 'pointer';
						}
					}
					rows[i].onmouseout = function() {
						if (this.getAttribute('locked') != 'true') {
							this.style.backgroundColor = this.oldColor;
							this.style.cursor = 'auto';
						}
					}
		
					var cols = rows[i].childNodes;
					
					for (var e = 0; e < cols.length; e++) {
						if (cols[e].nodeName == 'TD' && (cols[e].getElementsByTagName('A').length <= 1 && cols[e].getElementsByTagName('INPUT').length == 0)) {
							cols[e].onclick = function() {
								var link, onclick;
								var links = this.parentNode.getElementsByTagName('A');
								for (var e = 0; e < links.length; e++) {
									if (links[e].nodeValue == null) {
										link = links[e].getAttribute('href');
										onclick = links[e].getAttribute('onclick');
										e = links.length;
									}
								}
								if (link) {
									if (onclick) {
										if (eval(onclick.replace('return ', ''))) {
											goTo(link);
										}
									} else {
										goTo(link);
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

var _lastRowSubMenu = null;
function showRowSubMenu(obj) {
	var __lastRowSubMenu = _lastRowSubMenu;
	if (_lastRowSubMenu != null) {
		_lastRowSubMenu.style.display = 'none';
	}

	_lastRowSubMenu = obj.parentNode.getElementsByTagName('UL')[0];
	if (_lastRowSubMenu != __lastRowSubMenu) {
		_lastRowSubMenu.style.display = 'block';
	} else {
		_lastRowSubMenu = null;
	}
	
	return false;
}

function ShowHideObj(str) {
	var obj = document.getElementById(str);
	if (obj.style.display == 'none') {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}
}

String.prototype.isDateTime = function() {
	var arr = this.split(' ');
	
	if (arr.length > 1) {
		var isDate = arr[0].isDate();
		var isTime = arr[1].isTime();
		return (isDate && isTime);
	}
	
	return false;
}

String.prototype.isTime = function() {
	var arr = this.split(':');
	
	if (arr.length > 1) {
		if (!isNaN(arr[0]) && !isNaN(arr[1])) {
			hour = parseInt(arr[0]);
			minute = parseInt(arr[1]);
			
			if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60) {
				return true
			}
		}
	}
	
	return false;
}

String.prototype.isEmail = function() {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this)){
		return (true);
	} else {
		return (false);
	}
}

String.prototype.isCpf = function() {
	cpf = this;
	valor = true;

	if (cpf.length < 11) {
		return false;
	}

	var nonNumbers = /\D/;
	if (nonNumbers.test(cpf)) {
		return false;
	}

	if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
		return false;
	}

	var a = [];
	var b = new Number;
	var c = 11;
	for (i=0; i<11; i++){
		a[i] = cpf.charAt(i);
		if (i < 9) b += (a[i] *  --c);
	}
	if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
	b = 0;
	c = 11;
	for (y=0; y<10; y++) b += (a[y] *  c--); 
	if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
	if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
		return false;
	}

	return true;
}

String.prototype.isDate = function() {
	DateToCheck = this;
	
	if (DateToCheck == "") {
		return true;
	}
	var m_strDate = this.formatDate();

	if (m_strDate == "") {
		return false;
	}

	var m_arrDate = m_strDate.split("/");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];

	if (m_YEAR.length > 4) {
		return false;
	}

	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
	var testDate = new Date(m_strDate);

	if (testDate.getMonth() + 1 == m_MONTH) {
		return true;
	} else {
		return false;
	}
}//end function

String.prototype.formatDate = function(FormatAs) {
	DateToFormat = this;
	
	if (DateToFormat == "") {
		return "";
	}
	if (!FormatAs){
		FormatAs="dd/mm/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){
		if(parseFloat(YEAR)>40){
			YEAR = "19" + YEAR;
		} else{
			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;;
} //End Function

String.prototype.reverse = function() {
	var rev = '';
	for (var i = this.length - 1; i >= 0; i--) {
		rev += this.substr(i, 1);
	}
	return rev;
}
