String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
String.prototype.stripTags = function() { return this.replace(/<[^>]+>/g, ''); };

String.prototype.isEmail = function() {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this)){
		return (true);
	} else {
		return (false);
	}
}

String.prototype.convertUTF8Chars = function () {
	return this.replace(/\+/g, '%2B');
}

String.prototype.validURL = function () {
	var expreg = new RegExp();
	expreg.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+\."); 
	if (expreg.test(this)) {
		return true;
	}
	
	return false;
}

String.prototype.isCpf = function() {
	var CPF        = this.replace(/\.|\-/g, ''); // Recebe o valor digitado no campo
	var arrayFalso = new Array('11111111111', '22222222222', '33333333333', '44444444444', '55555555555',
							   '66666666666', '77777777777', '88888888888', '99999999999', '00000000000',
							   '19100000000', '01910000000', '00191000000', '00019100000', '00001910000',
							   '00000191000', '00000019100', '00000001910', '00000000191');

	for (var i = 0; i < arrayFalso.length; i++) {
		if (CPF == arrayFalso[i]) return false;
	}

	// Aqui começa a checagem do CPF
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado

	// Desemembra o número do CPF na array DIGITO
	for (I=0; I<=8; I++) {
		DIGITO[I] = CPF.substr( I, 1);
	}

	// Calcula o valor do 10º dígito da verificação
	POSICAO = 10;
	SOMA = 0;

	for (I=0; I<=8; I++) {
		SOMA = SOMA + DIGITO[I] * POSICAO;
		POSICAO = POSICAO - 1;
	}

	DIGITO[9] = SOMA % 11;
	if (DIGITO[9] < 2) {
		DIGITO[9] = 0;
	} else{
		DIGITO[9] = 11 - DIGITO[9];
	}

	// Calcula o valor do 11º dígito da verificação
	POSICAO = 11;
	SOMA = 0;

	for (I=0; I<=9; I++) {
		SOMA = SOMA + DIGITO[I] * POSICAO;
		POSICAO = POSICAO - 1;
	}

	DIGITO[10] = SOMA % 11;

	if (DIGITO[10] < 2) {
		DIGITO[10] = 0;
	} else {
		DIGITO[10] = 11 - DIGITO[10];
	}

	// Verifica se os valores dos dígitos verificadores conferem
	DV = DIGITO[9] * 10 + DIGITO[10];

	if (DV != DV_INFORMADO) {
		return false;
	}

	return true;
}

// '00.997.244/0001-90'
String.prototype.isCNPJ = function () {
	x      = this.replace(/\.|\-|\//g, '');
	strNum = "";

	if (x == "")
		return (false);

	l = x.length;

	for (i = 0; i < l; i++) {
		caracter = x.substring(i, i+1)
		
		if ((caracter >= '0') && (caracter <= '9'))
			strNum = strNum + caracter;
	}
	strMul = "6543298765432";
	iValido = 1;
	
	if(strNum.length != 14)
		return(false);

	iSoma = 0;
	strNum_base = strNum.substring(0,12);
	iLenNum_base = strNum_base.length - 1;
	iLenMul = strMul.length - 1;

	for(i = 0;i < 12; i++)
		iSoma = iSoma +	parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) * parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)

	iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);

	if(iSoma == 11 || iSoma == 10)
		iSoma = 0;

	strNum_base  = strNum_base + iSoma;
	iSoma        = 0;
	iLenNum_base = strNum_base.length - 1
	
	for(i=0; i < 13; i++)
		iSoma = iSoma +	parseInt(strNum_base.substring((iLenNum_base-i),(iLenNum_base-i)+1),10) * parseInt(strMul.substring((iLenMul-i),(iLenMul-i)+1),10)

	iSoma = 11 - (iSoma - Math.floor(iSoma/11) * 11);
	
	if(iSoma == 11 || iSoma == 10)
		iSoma = 0;
	
	strNum_base = strNum_base + iSoma;
	
	if(strNum != strNum_base)
		return(false);

	return(true);
}

String.prototype.isDate = function() {
	DateToCheck = this;
	
	if (DateToCheck == "") {
		return false;
	}
	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;;
}

String.prototype.validAge = function (intNum) {
	var Idade = new Date(this.formatDate("mm/dd/yyyy"));
	var Hoje  = new Date();
	var Passa = false;
	
	if ((Hoje.getFullYear()-Idade.getFullYear()) > intNum) { // passou do ano
		Passa = true;
	} else if ((Hoje.getFullYear()-Idade.getFullYear()) == intNum) { // está no ano
		if (Idade.getMonth() < Hoje.getMonth()) Passa = true; // passou do mes
		else if (Idade.getMonth() == Hoje.getMonth() && Idade.getDate() <= Hoje.getDate()) Passa = true; // no dia de nascimento ou se o aniversario já passou
	}
	
	return Passa;
}

String.prototype.wordWrap = function(m, b, c){
	var i, j, s, r = this.split("\n");
	if(m > 0) for(i in r){
		for(s = r[i], r[i] = ""; s.length > m;
			j = c ? m : (j = s.substr(0, m).match(/\S*$/)).input.length - j[0].length
			|| j.input.length + (j = s.substr(m).match(/^\S*/)).input.length + j[0].length,
			r[i] += s.substr(0, j) + ((s = s.substr(j)).length ? b : "")
		);
		r[i] += s;
	}
	return r.join("\n");
}

String.prototype.utf8_encode = function () {
    var string = this; 
    string     = (string+'').replace(/\r\n/g,"\n");
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}

String.prototype.utf8_decode = function () {
    var str_data = this;
    var tmp_arr  = [], i = ac = c1 = c2 = c3 = 0;
 
    str_data += '';
 
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}

String.prototype.base64_encode = function () {
	var data = this;
	var b64  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";  
	var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];  
	data = data.utf8_encode();  
	  
	do { // pack three octets into four hexets  
		o1 = data.charCodeAt(i++);  
		o2 = data.charCodeAt(i++);  
		o3 = data.charCodeAt(i++);  
	 
		bits = o1<<16 | o2<<8 | o3;  
	 
		h1 = bits>>18 & 0x3f;  
		h2 = bits>>12 & 0x3f;  
		h3 = bits>>6 & 0x3f;  
		h4 = bits & 0x3f;  
	 
		// use hexets to index into b64, and append result to encoded string  
		tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);  
	} while (i < data.length);  
	  
	enc = tmp_arr.join('');  
	  
	switch( data.length % 3 ){  
		case 1:  
			enc = enc.slice(0, -2) + '==';  
		break;  
		case 2:  
			enc = enc.slice(0, -1) + '=';  
		break;  
	}  
	 
	return enc;  
}

String.prototype.base64_decode = function () {
	var data = this;
    var b64  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];

    data += '';

    do {
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);

    dec = tmp_arr.join('');
    dec = data.utf8_decode();

    return dec;
}