var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)

var tempX = 0;
var tempY = 0;
// --

function trim(input) {
	return input.replace(/^\s+|\s+$/g,"");
}

function ltrim(input){
	return input.replace(/^\s+/,"");
}

function rtrim(input){
	return input.replace(/\s+$/,"");
}

function number_format( number, decimals, dec_point, thousands_sep ) {
	var n = number, prec = decimals;
	var toFixedFix = function (n,prec) {
		var k = Math.pow(10,prec);
		return (Math.round(n*k)/k).toString();
	};

	n = !isFinite(+n) ? 0 : +n;
	prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
	var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

	var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

	var abs = toFixedFix(Math.abs(n), prec);
	var _, i;

	if (abs >= 1000) {
		_ = abs.split(/\D/);
		i = _[0].length % 3 || 3;

		_[0] = s.slice(0,i + (n < 0)) +
			  _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		s = _.join(dec);
	} else {
		s = s.replace('.', dec);
	}
	if (s.indexOf(dec) === -1 && prec > 1) {
		s += dec+new Array(prec).join(0)+'0';
	}
	return s;
}

function print_r(theObj){
	if(theObj.constructor == Array || theObj.constructor == Object){
		document.write("<ul>");
		for(var p in theObj){
			if(theObj[p].constructor == Array|| theObj[p].constructor == Object){
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>");
				print_r(theObj[p]);
				document.write("</ul>");
			} else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
		}
	}
		document.write("</ul>");
	}
}

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}
	//document.getElementById("vx").innerHTML = tempX;
	//document.getElementById("vy").innerHTML = tempY;
	return true;
}


document.write("<div id=\"jsMachineDivAux\" style=\"display:none;position:absolute;top:0;left:0;z-index:9999999999999;\"></div>");
var jsMachineDivAux = document.getElementById('jsMachineDivAux');


function hideMagnify(){
	var jsMachineDivAux = document.getElementById('jsMachineDivAux');
	jsMachineDivAux.innerHTML = '';
	jsMachineDivAux.style.display='none';
}



function justMagnify(object){
	var jsMachineDivAux = document.getElementById('jsMachineDivAux');
	var tagName = object.tagName.toUpperCase();
	var mouseOut = '';

	if (tagName == 'IMG') {
		var imgSrc = object.getAttribute('src');
	} else if (tagName == 'A'){
		var imgSrc = object.getAttribute('href');
	} else {
		return false;
	}

	var title = object.getAttribute('title');
	if (!title) title = '';
	mouseOut = object.getAttribute('onmouseout');
	// seta funÃ§Ã£o pra remover a visualizaÃ§Ã£o
	if (mouseOut != '') {
		mouseOut += ' ; hideMagnify()';
	} else {
		mouseOut = 'hideMagnify()';
	}
	object.setAttribute('onMouseOut',mouseOut);

	var newX = tempX+5;
	var newY = tempY+5;

	//jsMachineDivAux.innerHTML = "<a  href=\"javascript:void(0)\" onclick=\"hideMagnify()\"><img id=\"jsMachineMagnifiedImage\" src=\""+imgSrc+"\"></a>";

	jsMachineDivAux.innerHTML = "<div class=\"jsMachineJustMagnifyOuter\"><div class=\"jsMachineJustMagnifyInner\"><img id=\"jsMachineMagnifiedImage\" src=\""+imgSrc+"\"></div>"+title+"</div>";
	jsMachineDivAux.style.display = "block";

	var jsMachineMagnifiedImage = document.getElementById('jsMachineMagnifiedImage');
	if (jsMachineMagnifiedImage.width+tempX > window.innerWidth) newX = tempX-15-jsMachineMagnifiedImage.width;
	if (jsMachineMagnifiedImage.height+tempY > window.innerHeight) newY = tempY-15-jsMachineMagnifiedImage.height;

	jsMachineDivAux.style.top = newY+"px";
	jsMachineDivAux.style.left = newX+"px";

}

function justMagnifyFromLink(object){
	//alert("x "+tempX+" y "+tempY);
	// verifica se jÃ¡ nÃ£o tem alguma funÃ§Ã£o no evento onMouseOut
	var mouseOut = '';
	var imgSrc = object.getAttribute('href');
	var title = object.getAttribute('title');
	if (!title) title = '';
	mouseOut = object.getAttribute('onmouseout');
	// seta funÃ§Ã£o pra remover a visualizaÃ§Ã£o
	if (mouseOut != '') {
		mouseOut += ' ; hideMagnify()';
	} else {
		mouseOut = 'hideMagnify()';
	}
	object.setAttribute('onMouseOut',mouseOut);

	var newX = tempX+5;
	var newY = tempY+5;

	var offsetX = 30;
	var offsetY = -30;

	//jsMachineDivAux.innerHTML = "<a  href=\"javascript:void(0)\" onclick=\"hideMagnify()\"><img id=\"jsMachineMagnifiedImage\" src=\""+imgSrc+"\"></a>";

	var jsMachineDivAux = document.getElementById('jsMachineDivAux');

	jsMachineDivAux.innerHTML = "<div class=\"jsMachineJustMagnifyOuter\"><div class=\"jsMachineJustMagnifyInner\"><img id=\"jsMachineMagnifiedImage\" src=\""+imgSrc+"\"></div>"+title+"</div>";
	jsMachineDivAux.style.display = "block";

	var jsMachineMagnifiedImage = document.getElementById('jsMachineMagnifiedImage');
	if (jsMachineMagnifiedImage.width+tempX+offsetX > window.innerWidth) newX = tempX-15-jsMachineMagnifiedImage.width;
	if (jsMachineMagnifiedImage.height+tempY+offsetY > window.innerHeight) newY = tempY-15-jsMachineMagnifiedImage.height;

	jsMachineDivAux.style.top = newY+"px";
	jsMachineDivAux.style.left = newX+"px";

}


// immediate configurations
document.onmousemove = getMouseXY;


/*calendar */

// construindo o calendário
function popdate(obj,div,ddd) {

var tam = 100;
var width = 190;
var height = 150;

    if (ddd)
    {
        day = ""
        mmonth = ""
        ano = ""
        c = 1
        char = ""
        for (s=0;s<parseInt(ddd.length);s++)
        {
            char = ddd.substr(s,1)
            if (char == "/")
            {
                c++;
                s++;
                char = ddd.substr(s,1);
            }
            if (c==1) day    += char
            if (c==2) mmonth += char
            if (c==3) ano    += char
        }
        ddd = day + "/" + mmonth + "/" + ano
    }

    if(!ddd) {today = new Date()} else {today = new Date(ddd)}

	//date_Form = eval (obj)
	date_Form = document.getElementById(obj);
    if (date_Form.value == "") { date_Form = new Date()} else {date_Form = new Date(date_Form.value)}

    ano = today.getFullYear();
    mmonth = today.getMonth ();
    day = today.toString ().substr (8,2)

    //umonth = new Array ("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
	umonth = new Array ('January','February','March','April','May','June','July','August','September','October','November','December')
    days_Feb = (!(ano % 4) ? 29 : 28)
    days = new Array (31, days_Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

    if ((mmonth < 0) || (mmonth > 11))  alert(mmonth)
    if ((mmonth - 1) == -1) {month_prior = 11; year_prior = ano - 1} else {month_prior = mmonth - 1; year_prior = ano}
    if ((mmonth + 1) == 12) {month_next  = 0;  year_next  = ano + 1} else {month_next  = mmonth + 1; year_next  = ano}
    txt  = "<table class=\"calendar\" bgcolor='#efefef' style='border:solid #999; border-width:2px ; z-index:9999999' cellspacing='0' cellpadding='1' border='0' width='"+width+"' height='"+height+"'>"
    txt += "<tr bgcolor='#FFF'><td colspan='7' align='center'><table border='0' cellpadding='0' width='100%' bgcolor='#fff'>";
    
    txt += "<tr><td align=center  bgcolor=\"\">";
    txt += "<a href=javascript:popdate('"+obj+"','"+div+"','"+((mmonth+1).toString() +"/01/"+(ano-1).toString())+"') class='Cabecalho_Calendario' title='Prev Year'><<</a>&nbsp;&nbsp;";
    txt += "<a href=javascript:popdate('"+obj+"','"+div+"','"+((month_prior+1).toString() + "/01" + "/" + year_prior.toString())+"') class='Cabecalho_Calendario' title='Prev Month'><</a>&nbsp;&nbsp;";
    txt += "<a href=javascript:popdate('"+obj+"','"+div+"','"+((month_next+1).toString() + "/01"  + "/" + year_next.toString())+"') class='Cabecalho_Calendario' title='Next Month'>></a>&nbsp;&nbsp;";
    txt += "<a href=javascript:popdate('"+obj+"','"+div+"','"+((mmonth+1).toString() +"/01/"+(ano+1).toString())+"') class='Cabecalho_Calendario' title='Next Year'>>></a>&nbsp;&nbsp;";
    txt += "</td>";
    
    var abrevMonth = umonth[mmonth];
    abrevMonth = abrevMonth.substr(0,3);
    abrevMonth = abrevMonth.toUpperCase();
    
    txt += "<td><a href=javascript:pop_year('"+obj+"','"+div+"','" + (mmonth+1) + "') class='mes'>" + ano.toString() + "</a></td>";
    txt += "<td><a href=javascript:pop_month('"+obj+"','"+div+"','"+tam+"','" + ano + "') class='mes' title='" + umonth[mmonth] + "'>" + abrevMonth + "</a></td>";
    txt += "<td></td>";
    
    txt += "<td align=right><a href=javascript:force_close('"+div+"') class='Cabecalho_Calendario' title='Close'><b>X</b></a></td></tr>";
  
    txt += "</table></td></tr>";
    
    txt += "<tr bgcolor='#ccc'><td width='14%' class='dia' align=center><b>Sun</b></td><td width='14%' class='dia' align=center><b>Mon</b></td><td width='14%' class='dia' align=center><b>Tue</b></td><td width='14%' class='dia' align=center><b>Wed</b></td><td width='14%' class='dia' align=center><b>Thu</b></td><td width='14%' class='dia' align=center><b>Fri<b></td><td width='14%' class='dia' align=center><b>Sat</b></td></tr>"
    today1 = new Date((mmonth+1).toString() +"/01/"+ano.toString());
    diainicio = today1.getDay () + 1;
    week = d = 1
    start = false;

    for (n=1;n<= 42;n++)
    {
        if (week == 1)  txt += "<tr bgcolor='#efefef' align=center>"
        if (week==diainicio) {start = true}
        if (d > days[mmonth]) {start=false}
        if (start)
        {
            dat = new Date((mmonth+1).toString() + "/" + d + "/" + ano.toString())
            day_dat   = dat.toString().substr(0,10)
            day_today  = date_Form.toString().substr(0,10)
            year_dat  = dat.getFullYear ()
            year_today = date_Form.getFullYear ()
            colorcell = ((day_dat == day_today) && (year_dat == year_today) ? " bgcolor='#FFCC00' " : "" )
            txt += "<td"+colorcell+" align=center><a href=javascript:block('"+  (mmonth+1).toString() + "/" + d + "/" + ano.toString() +"','"+ obj +"','" + div +"') class='data'>"+ d.toString() + "</a></td>"
            d ++
        }
        else
        {
            txt += "<td class='data' align=center> </td>"
        }
        week ++
        if (week == 8)
        {
            week = 1; txt += "</tr>"}
        }
        txt += "</table>"


        //div2 = eval (div)
		div2 = document.getElementById(div);
		//div2.style.zIndex = "9999999999999";
        div2.innerHTML = txt
}

// função para exibir a janela com os meses
function pop_month(obj, div, tam, ano)
{
  txt  = "<table bgcolor='#CCCCCC' border='0' width=80>"
  for (n = 0; n < 12; n++) { txt += "<tr><td align=center><a href=javascript:popdate('"+obj+"','"+tam+"','"+("01/" + (n+1).toString() + "/" + ano.toString())+"')>" + umonth[n] +"</a></td></tr>" }
  txt += "</table>"
  popd.innerHTML = txt
}

// função para exibir a janela com os anos
function pop_year(obj, div, tam, umonth)
{
  txt  = "<table bgcolor='#CCCCCC' border='0' width=160>"
  l = 1
  for (n=1991; n<2012; n++)
  {  if (l == 1) txt += "<tr>"
     txt += "<td align=center><a href=javascript:popdate('"+obj+"','"+div+"','"+(umonth.toString () +"/01/" + n) +"')>" + n + "</a></td>"
     l++
     if (l == 4)
        {txt += "</tr>"; l = 1 }
  }
  txt += "</tr></table>"
  popd.innerHTML = txt
}

// função para fechar o calendário
function force_close(div)  {http://www.tarantulaprojects.com/clients/mio_consumer/html/_xml/privacyPolicy.xml.php
	div2 = document.getElementById(div);
	//document.getElementById('cke_top_content').style.display = 'block';
	//div2 = eval (div);
	div2.innerHTML = ''
}

// função para fechar o calendário e setar a data no campo de data associado
function block(data, obj, div)
{
    force_close (div)
    //obj2 = eval(obj)
	obj2 = document.getElementById(obj);
    obj2.value = data
}

// function to take the file extension
function fileExtension(file){
	return file.replace(/^.*\.([a-z0-9]+)$/ig,"$1");
}


function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array
    //
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key].toUpperCase() === needle.toUpperCase()) {
                return true;            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {                return true;
            }
        }
    }
     return false;
}

function cleanUp(objectId){
	objectId = ''
	document.getElementById(objectId).value='';
}

function setInputInitialValue(inputId,initialValue){
	// set initial value to a text field. if text field got focus, value is erased, if lost focus without a new value, the initial value come back
	var signuptext;
	if (signuptext = document.getElementById(inputId)){
		signuptext.value = initialValue;
		
		signuptext.setAttribute('onfocus',"if (this.value=='"+initialValue+"') this.value = '';");
		signuptext.setAttribute('onblur',"if (this.value=='') this.value = '"+initialValue+"';");	

	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else var expires = "";
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function showObj(objId){
	document.getElementById(objId).style.display = "block";
}

function hideObj(objId){
	document.getElementById(objId).style.display = "none";
}
