//
// Fora CSS de Genexus
//
// document.styleSheets[0].disabled=true;

window.onresize = GXOnloadUsr;

var firstTime = true;

function GXOnloadUsr(param)
{
	try {
		var $j = jQuery.noConflict();
		var windowHeight = getWindowHeight();

		//
		// Si Plantilla complerta (MasterPage de veritat)
		//
		if( $j("#menu").length > 0 ) {
			$j("body").css("overflow-y", "hidden");
			$j("body").css("overflow", "hidden");
			//$j("#body").css("overflow", "hidden");
			//$j("#content").css("overflow", "auto");
			// $j("body").css("overflow-y", "none");

			var bodyHeight = windowHeight - $j("#header").height() - $j("#footer").height() - 1;

			$j("#menu").css("height", "" + bodyHeight + "px" );
			if($j("#tindremscroll").length == 0) {  
				$j("#body").css("height", "" + bodyHeight + "px" );
				$j("#content").css("height", "" + bodyHeight + "px" );
			
				// if($j("#menuoptions").height > bodyHeight-68 )
					$j("#menuoptions").css("height", "" + (bodyHeight-68) + "px" );
				
				//
				// Tenim GridContainer
				//
				if( $j(".GridContainer").length > 0) {
					var sobra = $j("#content-bottom").offset().top - windowHeight + 12;
					var ara =  $j(".GridContainer").height();
					$j(".GridContainer").css("height", "" + (ara-sobra) + "px");
					//alert( "ara: " + ara + "\nSobra: " + sobra);

					if(param != "reloadfix")
						setTimeout("GXOnloadUsr('reloadfix')", 10 );
				}
			} else {
				$j("body").css("overflow-y", "auto");
				$j("body").css("overflow", "auto");
			}

            mostra_caracters();
                        
			if( firstTime ) {

				$j("table.FormTable .FormLabel").each( function () {
					$j(this).parent().addClass("FormLabelTD");
				});

				$j("table.FormTable .TextBlock").each( function () {
					$j(this).parent().addClass("FormLabelTD");
				});

				// ErrorViewer es un dialog:
				$j(".ErrorViewer").fadeIn("slow");

				$j("#_TEXTESMS").attr('onkeyup', 'mostra_caracters()');
				
                                $j(".FCKEditor").each( function (i) {                       
                                    var oFCKeditor = new FCKeditor( this.id ) ;
                                    oFCKeditor.BasePath = "../static/fckeditor/" ;
                                    oFCKeditor.Width = '650'
                                    oFCKeditor.Height = '350'
                                    oFCKeditor.ToolbarSet = "Custom";
                                    oFCKeditor.ReplaceTextarea() ;
                                });
                                
                var currentDate = new Date();
                var day = currentDate.getDate();
                var month = currentDate.getMonth() + 1;
                var year = currentDate.getYear() % 100;
                
                if (day < 10)
                	day = "0" + day;
                
                if (month < 10)
                    month = "0" + month;
                    	  
                var hours = currentDate.getHours();
                var minutes = currentDate.getMinutes();
               
                if (hours < 10)
                	hours = "0" + hours;
                if (minutes < 10)
                	minutes = "0" + minutes;

                var currDateTimeString = "" + day + "/" + month + "/" + year + " " + hours + ":" + minutes;
                $j("#FECHALOCAL input[name='PUNDAT']").val(currDateTimeString);
                $j("#FECHALOCAL #span_PUNDAT").html(currDateTimeString);
             }
		}
		
	} catch(ex) {
  		alert("Exception: " + ex);
  	}

  	firstTime = false;
}

function getWindowHeight()
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }

  return myHeight;
}

function getWindowWidth()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }

  return myWidth;
}

//
// Canviem el desti del formulari Genexus cap a una nova finestra
// Deixem programat que tot torni al seu lloc un seg�n despres.
//
function newTarget()
{
	document.getElementById("MAINFORM").target="_blank";
	setTimeout('fixTarget();', 1000);
	return true;
}

//
// Tornem el formulari al seu lloc, i elominem la cortineta de genexus
//
function fixTarget()
{
	gxenableform();
	document.forms[0].target="_self";
}

function newTargetAndRedirect(url)
{
	document.getElementById("MAINFORM").target="_blank";
	setTimeout("document.location='" + url +"'", 1000);
	return true;
}

function mostra_caracters()
{
    var cad='',nummis=1,numeur=0, total=0, i=0;
    var str;
    
    if( ! document.MAINFORM._TEXTESMS ) {
        return;
    }
    
    cad = document.MAINFORM._TEXTESMS.value.replace(/\\r\\n/g,'\n');
    //cad = cad.replace(String.fromCharCode(8364),'EU');

    total=cad.length;

    for(i=0;i<total;i++)
    {
      if(cad.charCodeAt(i)=="8364")
        numeur=numeur+1;
    }

    total=total+numeur;
    if (total>765)
        cad = cad.substr(0,765);
    if (total>160)
    {
        nummis = Math.ceil(total/153);
        if (153 * nummis < total)
            nummis = nummis+1;
        str = (153*nummis-total)+'/'+nummis;
    }
    else
        str = (160-total)+'/'+nummis;
    if (cad!=document.MAINFORM._TEXTESMS.value.replace(String.fromCharCode(8364),'EU'))
        document.MAINFORM._TEXTESMS.value = cad;

    var $j = jQuery.noConflict();
    $j("#span__CARS").html(str);
}

