/************************************************* 
   Unique Systems Framework
   Técnicas de PHP e Javascript
   Robson Fernandes
   28/06/2008
   Classe usfw.js
*************************************************/

/******************************
  Escopo geral de uma função
function usfw_ ()
{
	
}

******************************/

var usfw_Width = screen.width;
var ModalWindow = 'ModalWindow';

function Trim(str)
{
  return str.replace(/^\s+|\s+$/g,"");
}

function usfw_OnlyNumbers(evt, CannotComma)
{
 if (evt.which == 0) 
   return true;

 var charCode = (evt.which) ? evt.which : event.keyCode;
 var Comma = (charCode == 44) || (charCode == 188);
 if (CannotComma)
   Comma = false;

return ((charCode == 8) || (charCode == 9) || Comma || (charCode >= 48 && charCode <= 57));
}

function usfw_CheckFields()
{
   var NomeCampo = '';
   var flCampo = true;
   var Ok = true;
   var Retorno = false;
   for (i=0;i<usfw_CheckFields.arguments.length;i++)
       {
		  Retorno = usfw_IsEmpty(usfw_CheckFields.arguments[i]);
		  if (Retorno && flCampo) 
		    {
			   flCampo = false;
			   NomeCampo = usfw_CheckFields.arguments[i];
		    }
		  Ok = Ok && (!Retorno);
	   }
   
   if (!Ok) 
     {
		alert('Por favor preencha os campos');
		usfw_Focus(NomeCampo);
	 }
   
   return Ok;
}

function usfw_CheckDate(pDia, pMes, pAno)
{
	  var Retorno = true;
	  
	  var Ano = document.getElementById(pAno).value;

      // Verifico o ano
	  
	  if (Ano==0)
		{
		   Retorno = false;
		   alert('Escolha o ano');	
		   usfw_Focus(pAno);
		}
	  else
		{
		  var Mes = document.getElementById(pMes).value;
		  var Dia = document.getElementById(pDia).value;
		  
		  var DiaLimite = 31;
		  if ((Mes == 4) || (Mes == 6) || (Mes == 9) || (Mes == 11))
			DiaLimite = 30;
		  else if (Mes==2)
			DiaLimite = 28;
			
		  if (Dia > DiaLimite)
			{
				Retorno = false;
				alert('Escolha a data corretamente');
				usfw_Focus(pDia);
			}
	  }
  return Retorno;
}

function usfw_ShowModal(Url, Width)
{

   var lPosition = (usfw_Width - Width) / 2;
   document.getElementById(ModalWindow).style.width = Width + 'px';
   document.getElementById(ModalWindow).style.left = lPosition + 'px';
   document.getElementById(ModalWindow).style.top = '90px';
   usfw_PutHTML(ModalWindow,'');
   usfw_ChangeClass('Modal','Visible'); 
   usfw_AjaxWait(Url, ModalWindow);
}

function usfw_InverseInline(Id)
{
	usfw_Toogle('ShowInline' + Id);
	usfw_Toogle('EditInline' + Id);
}

function usfw_IsEmpty(ObjectName)
{
   return document.getElementById(ObjectName).value == '';	
}

function usfw_Focus(ObjectName)
{
   return document.getElementById(ObjectName).focus();
}

function usfw_LoadMP3(MusicFile, StartPlay, Place)
{
	var Play = "autostart=false";
	if (StartPlay)
	  Play = "autostart=" + (StartPlay ? "true":"false");
	var s1 = new SWFObject('player/player.swf','player','240','20','9','#ffffff');
	s1.addParam('allowfullscreen','true');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('wmode','opaque');
	s1.addParam('flashvars','file=mp3/' + MusicFile + '&' + Play);
	s1.write(Place);
}

function usfw_GetAbsoluteDiv(DivName, Offset)
{
  var Divs = document.getElementsByTagName("div");
  var ReturnedDivId = '';
  for (i=0;i<Divs.length;i++)
	if (Divs[i].id == DivName)
	   {
		  if ((Divs[i+Offset]) && (i + Offset >=0))
			ReturnedDivId = Divs[i+Offset].id;
		  break;
	   }
  return ReturnedDivId;
}  

function usfw_GetPriorDiv(DivName)
{
  return usfw_GetAbsoluteDiv(DivName, -1);
}

function usfw_GetNextDiv(DivName)
{
  return usfw_GetAbsoluteDiv(DivName, 1);
}


function usfw_ChangeClass(ObjectName, ClassName)
{
  document.getElementById(ObjectName).className = ClassName;	
}


function usfw_GetHTML(ObjectName)
{
   return document.getElementById(ObjectName).innerHTML;
}

function usfw_PutHTML(ObjectName, Content)
{
   document.getElementById(ObjectName).innerHTML = Content;
}

function usfw_SwapContent(ObjectName1, ObjectName2)
{
   var Content1 = usfw_GetHTML(ObjectName1);
   var Content2 = usfw_GetHTML(ObjectName2);
   usfw_PutHTML(ObjectName1, Content2);
   usfw_PutHTML(ObjectName2, Content1);
}

function usfw_ShowHide(ObjectName, State)
{
  document.getElementById(ObjectName).style.display = (State ? "block":"none");
}

function usfw_Toogle(ObjectName)
{
   var State = (document.getElementById(ObjectName).style.display == 'none');
   usfw_ShowHide(ObjectName, State);
}

function usfw_SetDefaultValue(Target, Source)
{
  document.getElementById(Target).value = document.getElementById(Source).value;
}

function usfw_Ajax(Page, ObjectName)
{
	usfw_AjaxRequest(ObjectName, Page);
}


function usfw_AjaxWait(Page, ObjectName, Msg)
{
	var MsgProcess = Msg;
	if (!Msg)
	  MsgProcess = '<div align="center"><b>Processando...</b><br><img src=usfw/images/loading.gif></div>';
	usfw_AjaxRequest(ObjectName, Page,'', MsgProcess);
}

// start ajaxrequest.js


// Following is a javascript function that makes a httprequest - AJAX. This is the AJAX bit and all that is needed in that manner.
// Only in this one we won't be using XML in our response, we will accept and handle
// pure text and html and display this response directly to the user within the
// desired <div id> tags. It can even be used to include pure html files as a substitute
// solution to the "old" frames method where as no php or other scripting language is nessesary on the server.
// but use it with care - it is not a search engine supported method and indexing will fail. Workaround for this is not included here

function usfw_AjaxRequest(target_div,file,check_div, FraseLoading, PageRedirect)
{
	var MyHttpRequest = false;
	var MyHttpLoading = FraseLoading;
	if (!MyHttpLoading)
	  MyHttpLoading = '';
	// var MyHttpLoading = '<p>Loading...</p>'; 
	// or use an animated gif instead: var MyHttpLoading = '<img src="loading.gif" border="0" alt="running" />';
	var ErrorMSG = 'Sorry - No XMLHTTP support in your browser, buy a newspaper instead';
	
	if(check_div)
  	  var check_value = document.getElementById(check_div).value;
	else
	  var check_value = '';
		
	
	if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
	{
	  try
	    {
	      MyHttpRequest = new XMLHttpRequest();
	    }
	  catch(e)
	    {
	      MyHttpRequest = false;
	    }
	}
	else if(window.ActiveXObject) // client use Internet Explorer
	{
  	   try
	   {
	     MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	   }
	   catch(e)
	   {
	     try
	     {
	       MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	     }
	     catch(e)
	     {
	       MyHttpRequest = false;
	     }
	   }
	}
	else
	{
	   MyHttpRequest = false;
	}
		
	
	if(MyHttpRequest) // browser supports httprequest
	{
		var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching
		
		var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file
		if(file_array[1] == 'php') // no query string, just calling a php file
		{
		  var query_string = '?rand=' + random;
		}
		else if(file_array[1] == 'htm' || file_array[1] == 'html') // calling a htm or html file
		{
		  var query_string = '';
		}
		else // we have presumable a php file with a query string attached
		{
		  var query_string = check_value + '&rand=' + random;
		}
		
		
		MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET
	
	
		// handle the httprequest
		MyHttpRequest.onreadystatechange = function ()
		{
			if(MyHttpRequest.readyState == 4) // done and responded
			{
			   document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
			   if (PageRedirect)
			     location=PageRedirect;
			}
			else
			{
			   if (MyHttpLoading!='')
			     document.getElementById(target_div).innerHTML = MyHttpLoading; // still working
			}
			}
			  MyHttpRequest.send(null);
			}
			else 
			{
			  document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest
		}
	}
	// end of "AJAX" function
	
	
	// Here follows a function to urlencode the string we run through our httprequest, it has nothing to do with AJAX itself
	// If you look carefully in the above httprequest you se that we use this url_encode function around the file and query_string
	// This is very handy since we are using GET in our httprequest and for instance
	// any occurrance of the char # (from textboxes etc) will brake the string we are sending to our file - we don't want that to brake!
	// It will also convert spaces to +
	
	function url_encode(string)
	{
	var string;
	var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
	var hex = "0123456789ABCDEF";
	var encoded_string = "";
	for(var i = 0; i < string.length; i++)
	{
	var character = string.charAt(i);
	if(character == " ")
	{
	encoded_string += "+";
	}
	else if(safechars.indexOf(character) != -1)
	{
	encoded_string += character;
	}
	else
	{
	var hexchar = character.charCodeAt(0);
	if(hexchar > 255)
	{
	encoded_string += "+";
	}
	else
	{
	encoded_string += "%";
	encoded_string += hex.charAt((hexchar >> 4) & 0xF);
	encoded_string += hex.charAt(hexchar & 0xF);
	}
	}
	}
	return encoded_string;
}
	