// JavaScript Document
function formSubmit(formName){
	eval('document.'+ formName + '.submit();');
}

function getFileExtension($file){
	return ($file.substring($file.lastIndexOf('.') + 1)).toLowerCase();
}
function open_popup(url,popup_name,width,height){
	var left = Math.floor((screen.width - width) / 2);
	var top = Math.floor((screen.height - height) / 2);
	window.open(url,popup_name, 'height='+height+', width='+width+', left='+left+',top='+top+',toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, status=no');
}
function redir(url){
	eval("location.href = '" + url + "'");
}
function redirParent(url){
	eval("opener.location.href = '" + url + "'");
	self.close();
}
function check_all(obj, objImage) {
	if (obj) {
		if (obj.length) {
			value = obj[0].checked==false?true:false;
			
			for (i=0;i<obj.length;i++)
				obj[i].checked = value;
		}
		else {
			value = obj.checked==false?true:false;
			obj.checked = value;
		}
		/**/
		if(objImage){
			objImage.src = (value) ? srcImageClearAll : srcImageCheckAll; 
		}
		
	}
}

function ClearSelectBox(obj){
	var NumState = obj.options.length;
	while(NumState > 0){
		NumState--;
		obj.options[NumState] = null;
	}
}



/**
 * COOKIE
 */
// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false
/* 
 name - name of the cookie
 value - value of the cookie
 [expires] - expiration date of the cookie (defaults to end of current session)
 [path] - path for which the cookie is valid (defaults to path of calling document)
 [domain] - domain for which the cookie is valid (defaults to domain of calling document)
 [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
 * an argument defaults when it is assigned null as a placeholder
 * a null placeholder is not required for trailing omitted arguments
 */
function setCookie(name, value, expires, path, domain, secure)
{
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name)
{
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex +
prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex +
prefix.length, cookieEndIndex))
}
// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds

function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}
function sendWithAction( FormName, sAction ){
	if(document.forms[FormName]){
		document.forms[FormName].action = sAction;
		document.forms[FormName].submit();
	}	
	if(document.FormName){
		document.FormName.action = sAction;	
		document.FormName.submit();
	}	

}

function send(FormName){
	 alert('FormName');
	if(document.forms[FormName]){
		document.forms[FormName].submit();
	}	
	if(document.FormName){
		document.FormName.submit();
	}
}

function sendID(id){
	if(document.getElementById(id)){
		document.getElementById(id).submit();
	}
}

function print(sMsg,sID){
	try{
		document.getElementById(sID).innerHTML=sMsg;
	}
	catch(e){
		try{
			document.sID.innerHTML=sMsg;
		}
		catch(e){
			try{
				document.all.sID.innerHTML=sMsg;
			}
			catch(e){
				alert(sMsg);
			}
		}
	}
}
	
function validateNumeric(Num){
	er=new RegExp("^[0-9]+$");
	return er.test(Num);
}

function validateDate(day,month,year){
	date=new Date();
	yearNow=1900+date.getYear();
	if(year==''||month==''||day==''){
		return false;
	}
	if(isNumber(year)||isNumber(month)||isNumber(day)){
		return false;
	}
	if(month<1||month>12){
		return false;
	}if(year>yearNow){
		return false;
	}
	
	var dayInFebruary=checkLeapYear(year)?29:28;
	var daysInMonth=new Array('Days',31,dayInFebruary,31,30,31,30,31,31,30,31,30,31);
	if(month<10&&month.length==2)month=month.substring(1);
	if(day>0&&day<=daysInMonth[month]){
		return true;
	}else{
		return false;
	}
}

function checkLeapYear(year){
	return(((year%4==0)&&((!(year%100==0))||(year%400==0)))?true:false);
}
function validateEmail(email){
	er=new RegExp("^([A-Za-z0-9.-_-]+)@[A-Za-z0-9-_]+(\.[A-Za-z0-9]+)?\.([A-Za-z]){2,4}$");
	return er.test(email);
}

function OpenWindow(URL,title,features,width,height,isCenter){
	var left=0;
	var top=0;
	if((window.screen)&&isCenter){
		var left=(window.screen.width/2)-(width/2);
		var top=(window.screen.height/2)-(height/2);
	}
	features+=(features!='')?',':'';
	features+='left='+left+',top='+top+',width='+width+',height='+height;
	window.open(URL,title,features);
}

function removeChar(characters,text){
	var ntext='';
	if(characters.length>0){
		for(i=0;i<text.length;i++){
			exist=false;
			for(j=0;j<characters.length;j++){
				if(text.charAt(i)==characters[j]){
					exist=true;
					break;
				}
			}
			if(!exist){
				ntext+=text.charAt(i);
			}
		}
		return ntext;
	}else{
		return text;
	}
}

function addClass(sID,sClassName){
	var oID=document.getElementById(sID);
	if(oID.className===''){
		oID.className=sClassName;
	}else{
		oID.className=oID.className+', '+sClassName;
	}
}

function removeClass(sID,sClassName){
	var oID=document.getElementById(sID);
	var aClass=oID.className.split(', ');
	oID.className='';
	for(i=0;i<=aClass.length;i++){
		if(!(aClass[i]===sClassName)&&typeof(aClass[i])!='undefined'){
			addClass(sID,aClass[i]);
		}
	}
}

function selectAll(){
	fields=document.getElementsByTagName('input');
	for(i=0;i<fields.length;i++){
		if(fields[i].type=='checkbox'){
			fields[i].checked=true;
		}
	}
	fields=document.getElementsByTagName('tr');
	for(i=0;i<fields.length;i++){
		if(fields[i].id.charAt(0)==='t'&&fields[i].id.charAt(1)==='r')addClass(fields[i].id,'highlight3');
	}
}

function unselectAll(){
	fields=document.getElementsByTagName('input');
	for(i=0;i<fields.length;i++){
		if(fields[i].type=='checkbox'){
			fields[i].checked=false;
		}
	}
	fields=document.getElementsByTagName('tr');
	for(j=0;j<fields.length;j++){
		if(fields[j].id.charAt(0)==='t'&&fields[j].id.charAt(1)==='r')removeClass(fields[j].id,'highlight3');
	}
}

function select(){
	fields=document.getElementsByTagName('input');
	for(i=0;i<fields.length;i++){
		if(fields[i].type=='checkbox'){
			if(fields[i].checked==false){
				selectAll();
				return;
			}
		}
	}
	unselectAll();
}

function mountMessages(sMsgs){
	aMsgs=sMsgs.split('||');
	aNewMsgs=Array();
	for(i=0;i<aMsgs.length;i++){
		aAux=aMsgs[i].split(':++:');
		aNewMsgs[aAux[0]]=aAux[1];
	}
	return aNewMsgs;
}

function getElement(sId,bUseOpener){
	if(bUseOpener){
		if(document.getElementById){
			return window.opener.document.getElementById(sId);
		}else{
			if(document.layers){
				return window.opener.document.sId;
			}else{
				return window.opener.document.all.sId;
			}
		}
	}else{
		if(document.getElementById){
			return document.getElementById(sId);
		}else{
			if(document.layers){
				return document.sId;
			}else{
				return document.all.sId;
			}
		}
	}
}

/*
Function validateFields(aCheck)
	Note: aCheck[i][0] = field ID,
		  aCheck[i][1] = minimum character
		  aCheck[i][2] = maximum character
		  aCheck[i][3] = error message
		  aCheck[i][4] = Is numberic
		  aCheck[i][5] = div ID of error message.  (if not found div id will appear a window message box
													
Sample to use:
var aCheck = new Array();
	/*
	Note: aCheck[i][0] = field ID,
		  aCheck[i][1] = minimum character
		  aCheck[i][2] = maximum character
		  aCheck[i][3] = error message
		  aCheck[i][4] = Is numberic
		  aCheck[i][5] =Is_show_message_follow_template, if == true: then show default message
		  aCheck[i][6] = div ID of error message.  (if not found div id will appear a window message box
	
	aCheck.push ( new Array('store_id', 1, 200, "{LOGIN.store_empty}", false, false, 'form_room_name') );
	aCheck.push ( new Array('password', 1, 200, "{LOGIN.password_empty}", false, false,'form_password') );
	aCheck.push ( new Array('UserId', 1, 200, "{LOGIN.password_empty}", false, false,'form_UserId' ) );
	var bRes = validateFields(aCheck);
	if(!bRes)
	{
		return bRes; // return false
		
	}
	else
	{			
        return bRes; // return true
	}	
*/	

function validateFields(aCheck){
	for(i=0;i<aCheck.length;i++){
		if((document.getElementById(aCheck[i][0]))){
			var obj=document.getElementById(aCheck[i][0]).value;
			var size=obj.length;
			
			if(aCheck[i][1]==0&&aCheck[i][2]==0&&obj===''){
				if(aCheck[i][5]){
					print('Field '+aCheck[i][3]+' cannot be blank',aCheck[i][6],'att');
					//scrollToByElement('msgs');	
				}else{
					print(aCheck[i][3],aCheck[i][6],'att');
						//scrollToByElement('msgs');
				}
				return false;
			}else if(aCheck[i][4]&&isNaN(obj)){
				if(aCheck[i][5]){
					print('Field '+aCheck[i][3]+' must Accountin only number.',aCheck[i][6],'att');
					//scrollToByElement('msgs');
				}else{
					print(aCheck[i][3],aCheck[i][6],'att');
					//scrollToByElement('msgs');
				}
				return false;
			}else if(aCheck[i][1]>0&&aCheck[i][2]&&(size<aCheck[i][1]||size>aCheck[i][2])){
				if(aCheck[i][5]){
					print('Field '+aCheck[i][3]+' must Accountin value in maximum '+aCheck[i][2]+' and minimum '+aCheck[i][1]+' characters.',aCheck[i][6],'att');
					scrollToByElement('msgs');
				}else{
					print(aCheck[i][3],aCheck[i][6],'att');
					//scrollToByElement('msgs');
				}
				return false;
			}
		}
	}
	return true;
}

function print(sMsg,sID,sClass){
	try{
		document.getElementById(sID).innerHTML=sMsg;
		if(sClass)addClass(sID,sClass)
	}
	catch(e){
		try{document.sID.innerHTML=sMsg;
		if(sClass)addClass(sID,sClass)
		}
		catch(e){
			try{
				document.all.sID.innerHTML=sMsg;
				if(sClass)addClass(sID,sClass)
			}
			catch(e){
				alert(sMsg);
			}
		}
	}
}

function addClass(sID,sClassName){
	if(document.getElementById){
		oID=document.getElementById(sID);
	}else{
		if(document.layers){
			oID=document.sID;
		}else{
			oID=document.all.sID;
		}
	}
	if(oID.className===''){
		oID.className=sClassName;
	}else{
		oID.className=oID.className+' '+sClassName;
	}
}

function removeClass(sID,sClassName){
	if(document.getElementById){
		oID=document.getElementById(sID);
	}else{
		if(document.layers){
			oID=document.sID;
		}else{
			oID=document.all.sID;
		}
	}
	
	var aClass=oID.className.split(' ');
	oID.className='';
	for(i=0;i<=aClass.length;i++){
		if(!(aClass[i]===sClassName)&&typeof(aClass[i])!='undefined'){
			addClass(sID,aClass[i]);
		}
	}
}

function checkByID(sID,sTRID){
	if(document.getElementById){
		oID=document.getElementById(sID);
	}else{
		if(document.layers){
			oID=document.sID;
		}else{
			oID=document.all.sID;
		}
	}
	if(oID.checked===true){
		oID.checked=false;
		removeClass(sTRID,'highlight3');
	}else{
		oID.checked=true;
		addClass(sTRID,'highlight3');
	}
}


function countChars(eFrom,iIdTo){
	var countFrom=eFrom;
	var printIn=getElement(iIdTo);
	printIn.value=countFrom.value.length;
}

function openURLOpener(sURL,bClose){
	window.opener.location.href=sURL;
	if(bClose==true){
		window.close();
	}
}

function openURL(sURL,bClose){
	window.location.href=sURL;
	if(bClose==true){
		window.close();
	}
}

function protegerEmail(dominio,extensao,usuario){
	document.write("<a href="+"mail"+"to:"+usuario+"@"+dominio+extensao+">"+usuario+"@"+dominio+extensao+"</a>");
}


function scrollToByElement(sID){
	if ( !getElement(sID) ) return;
	var eOBJ=getElement(sID);
	var iWidth=encontrarPosX(eOBJ);
	var iHeight=encontrarPosY(eOBJ);
	window.scrollTo(iWidth,iHeight);
}

function encontrarPosY(eOBJ){
	var iPosTop=0;
	if(eOBJ.offsetParent){
		while(eOBJ.offsetParent){
			iPosTop+=eOBJ.offsetTop;
			eOBJ=eOBJ.offsetParent;
		}
	}else if(eOBJ.y){
		iPosTop+=eOBJ.y;
	}
	return iPosTop;
}

function encontrarPosX(eOBJ){
	var iPosLeft=0;
	if(eOBJ.offsetParent){
		while(eOBJ.offsetParent){
			iPosLeft+=eOBJ.offsetLeft;
			eOBJ=eOBJ.offsetParent;
		}
	}else if(eOBJ.x){
		iPosLeft+=eOBJ.x;
	}
	return iPosLeft;
}

/*
   function: hide
      Hide an Element

   parameters:

      id - ID of Element
*/
function hide(id) {
    if (document.getElementById) // DOM3 = IE5, NS6
    {
        document.getElementById(id).style.display = 'none';
        document.getElementById(id).style.visibility = 'hidden';
    }
    else 
    {
        if (document.layers) // Netscape 4
        {
            document.id.display = 'none';
            document.id.visibility = 'hidden';
        }
        else // IE 4
        { 
            document.all.hideshow.style.display = 'none';
            document.all.hideshow.style.visibility = 'hidden';
        }
    }
}

/*
   function: show
      Show an Element

   parameters:

      id - ID of Element
      sDisplayProp - Type of display that allows modify. Block, table, table-row
*/

function show(id, sDisplayProp ) 
{
    if ( typeof( sDisplayProp ) == "undefined" )
        sDisplayProp = 'block';

    if (document.getElementById) // DOM3 = IE5, NS6
    {
        try { 
            document.getElementById(id).style.display = sDisplayProp;
            document.getElementById(id).style.visibility = 'visible';
        }
        catch(e)
        { // current tables has problems with IE
            document.getElementById(id).style.display = 'block';
            document.getElementById(id).style.visibility = 'visible';
        }
    }
    else
    {
        if (document.layers) // Netscape 4
        {
            document.id.display = sDisplayProp;
            document.id.visibility = 'visible';
        }
        else // IE 4
        {
            document.all.id.style.display = sDisplayProp;
            document.all.id.style.visibility = 'visible';
        }
    }
}

/*
function: showhide
    Similar Hide with accordancing a div
    
parameters:

      id - ID of Element
      sDisplayProp - Type of display that allows modify. Block, table, table-row
*/
function showhide( id, sDisplayProp )
{
    if (document.getElementById) // DOM3 = IE5, NS6
    {
        if ( document.getElementById(id).style.display == 'none' )
            show( id, sDisplayProp );
        else
            hide( id );
    }
    else
    {
        if (document.layers) // Netscape 4
        {
            if ( document.id.display == 'none' )
                show( id, sDisplayProp );
            else
                hide( id );
        }
        else // IE 4
        {
            if ( document.all.id.style.display == 'none' )
                show( id, sDisplayProp );
            else
                hide( id );
        }
    }
}

/*
    function: setFocus
       Set focus

    parameters:

       string:

       FormName - 
       FieldName - 
*/
function setFocus( sFormName, sFieldName )
{
    if (document.forms[sFormName])
    {
    document.forms[sFormName][sFieldName].focus();
    }
    else if (document.sFormName)
    {
    document.sFormName.sFieldName.focus();
    }
}

