﻿// JScript File
function checkForBlankValue(txtID,msg,showAlert)
{
    var txtBoxValue='';
    txtBox=document.getElementById(txtID); 
    if (txtBox==null) return;
    if (txtBox.style.display=="none") return true;
    txtBoxValue=trimAll(txtBox.value);
    txtBoxValue=txtBoxValue.replace(/[(<>"'&]/g,'');
    txtBox.value=txtBoxValue;
    if(txtBoxValue=='')
    {
	    
	    if(showAlert)
	    {            
            alert(msg +' should not be blank.');
        }
        else
            {
             setLblErrorValue(msg +' should not be blank.');
            }

        txtBox.focus();
        return false;
    }
    return true;
}

function checkSpecialCharacters(elementID,showAlert,strAdditionalCharacters)
{
    var element = document.getElementById(elementID);
    if (element==null) return true;
    strToCheck = element.value;
    
	var strSpecialCharacters = '<~';    //'~#$%^|\\"<>';			
	strSpecialCharacters += strAdditionalCharacters;
	for(var charCounter = 0; charCounter < strToCheck.length; charCounter++)
	{
		if((strSpecialCharacters.indexOf(strToCheck.charAt(charCounter))) >= 0)
		{	
		    if(showAlert)
                alert(strToCheck.charAt(charCounter) + ' character is not allowed.');
            else
                setLblErrorValue(strToCheck.charAt(charCounter) + ' character is not allowed.');
                
            element.focus();
			return false;											
		}
	}				
	return true;
}
function checkForDecimalValue(txtID,msg,shouldBeDecimal,showAlert)
{
    var txtBoxValue='';
    txtBox=document.getElementById(txtID);
    if (txtBox==null) return;
    if (txtBox.style.display=="none") return true;
    txtBoxValue=trimAll(txtBox.value);
    txtBox.value=txtBoxValue;
    if(txtBoxValue!='')
    {
        var IsANumber = new Boolean(false);
        IsANumber = IsNumericDot(txtBoxValue);
        
        if(IsANumber==false && shouldBeDecimal==true)
        {
	        if(showAlert)
                alert(msg +' should be numeric value.');
            else
                setLblErrorValue(msg +' should be numeric value.');
    
            txtBox.focus();
            return false;
        }
        else if(IsANumber==true && shouldBeDecimal==false)
        {
	        if(showAlert)
                alert(msg +' should not be numeric value.');
            else
                setLblErrorValue(msg +' should not be numeric value.');
    
            txtBox.focus();
            return false;
        }
    }

    return true;
}
                
function trim(str)
{
    return str.replace(/^\s+|\s+$/g, '');
}

function trimAll(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function setLblErrorValue(lblErrorID,text)
{
    if(document.getElementById(lblErrorID))
        document.getElementById(lblErrorID).innerHTML=text;
}
function IsNumericDot(field)
{
	var num='0123456789.';
	var dot=".";
	var NoOfDots=0;			
	for(var i=0;i<field.length;i++)
	{
		if((num.indexOf(field.charAt(i)))==-1)
		{	
			return false;											
		}
		if((dot.indexOf(field.charAt(i)))>=0)
		{	
			NoOfDots=NoOfDots+1;
		}
	}
	if(NoOfDots>1) return false;				
	return true;
}
function IsBlankSpaceExists(str)
{
    if(str.indexOf(' ')>=0) return true;
    return false;    
}

function getElementsValue(elementID,elementType,dataType)
{
    var element,result;
    
    element = document.getElementById(elementID);
    if(element!=null)
    {
        switch(elementType.toLowerCase())
        {
            case "textbox":
            case "hidden":
            case "dropdownlist":
                result = element.value;
                break;
            case "label":
            case "span":
                result = element.innerHTML;
                break;    
        }
    }
    else
    {
        switch(dataType.toLowerCase())
        {
            case "string":
                result='';
                break;
            case "numeric":
                result=0;
                break;
        }
    }
    
    return result;
}

function setElementsValue(elementID,elementType,valueToset)
{
    var element;
    
    element = document.getElementById(elementID);
    if(element==null) return;
    switch(elementType.toLowerCase())
    {
        case "textbox":
        case "dropdownlist":
        case "hidden":
            element.value=valueToset;
            break;
        case "label":
        case "span":
            element.innerHTML=valueToset;
            break;    
    }
}

function setFocus(elementID)
{
    var element;
    element = document.getElementById(elementID);
    if(element!=null)
        element.focus();   
}

function displayRow(tableRowId)
{
     if(!document.getElementById(tableRowId)) return;
     
     try
     {
       document.getElementById(tableRowId).style.display='table-row';
     }
     catch(err)
     {
       document.getElementById(tableRowId).style.display='block';
     }
}

function hideRow(tableRowId)
{
     if(!document.getElementById(tableRowId)) return;
     document.getElementById(tableRowId).style.display='none';
}

function hideElement(ElementId)
{
     if(!document.getElementById(ElementId)) return;
     document.getElementById(ElementId).style.display='none';
}
function showElement(ElementId)
{
     if(!document.getElementById(ElementId)) return;
     document.getElementById(ElementId).style.display='block';
}
function isValidURL(url,showAlert)
{ 
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
    if(RegExp.test(url))
        return true; 
    else
    {
	    if(showAlert)
            alert('Invalid URL.');
        else
            setLblErrorValue('Invalid URL.');

        return false; 
    }
} 
function isValidEMail(EMail,showAlert)
{
	var RegExp  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (RegExp.test(EMail))
	    return true;
	else 
	{
	    if(showAlert)
            alert('Invalid Email Address.');
        else
            setLblErrorValue('Invalid Email Address.');

	    return false; 
	}
}
function trimFields(fieldsToTrim)
{
    if(fieldsToTrim.length > 0)
    {
        for(var counter=0; counter < fieldsToTrim.length; counter++)
        {
            var fieldsValue = getElementsValue(fieldsToTrim[counter],"textbox","string");
            fieldsValue = trimAll(fieldsValue);
            setElementsValue(fieldsToTrim[counter],"textbox",fieldsValue);
        }
    }
}

function ImposeMaxLength(ObjectID, MaxLen)
{
    var Object = document.getElementById(ObjectID);
    if(Object != null && Object.value.length > MaxLen)
    {
        Object.value = Object.value.substring(0,MaxLen);
    }
}
 function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function SelectAll()
{
	var c=document.getElementsByName("checkboxMailReceiverID");
	for (i=0; i<c.length; ++i)
	{
		c[i].checked = true;
	}
}

function DeselectAll()
{
	var c=document.getElementsByName("checkboxMailReceiverID");
	for (i=0; i<c.length; ++i)
	{
		c[i].checked = false;
	}
}

//for implementing maxlength on textarea
function ImposeMaxLength(ObjectID, MaxLen)
{
    var Object = document.getElementById(ObjectID);
    if(Object != null && Object.value.length > MaxLen)
    {
        Object.value = Object.value.substring(0,MaxLen);
    }
}

function CountCharactersTyped(txtID,updateElementID,MaxLength)
{
    var typedCharactersLength = getElementsValue(txtID,"textbox","string").length;
    setElementsValue(updateElementID,"span",typedCharactersLength + " of " + MaxLength + " characters");
}
function wordWrap()
{
    /******
    * wordWrap to firefox for big words
    * Creative Commons license * Version: 1.0 - 26/04/2006
    * Autor: Micox - Náiron J.C.G - micoxjcg@yahoo.com.br - http://elmicoxcodes.blogspot.com
    * Uso: call the function on onload of body element.
    * put the class "word-wrap" on elements to wordwrap
    *******/
    var larg_total,larg_carac,quant_quebra,pos_quebra;
    var elementos,quem, caracs, texto, display_orig;
    
    elementos = document.getElementsByTagName("span")
    
    for(var i=0; i<elementos.length;i++){
        if(elementos[i].className=="word-wrap"){
            quem = elementos[i];
            
            quem.innerHTML = String(quem.innerHTML).replace(/ /g,"Ø")
            texto = String(quem.innerHTML)
            
            quem.innerHTML = " "
            
            display_orig = quem.style.display;
            quem.style.display="block";
            larg_oficial = quem.offsetWidth;
            //alert("oficial: " + larg_oficial)
            //alert("display " + quem.style.display)
            if(!document.all) quem.style.display="table";
            //alert("display " + quem.style.display)
            quem.innerHTML = texto;
            larg_total = quem.offsetWidth;
            //alert("total: " + larg_total)
            
            pos_quebra = 0;
            caracs = texto.length;
            texto = texto.replace(/Ø/g," ")
            larg_carac = larg_total / caracs
            if(larg_total>larg_oficial){
                quant_quebra = parseInt(larg_oficial/larg_carac)
                quant_quebra = quant_quebra - (parseInt(quant_quebra/6)) //quanto menor o num, maior a garantia;
                quem.innerHTML = ""
                while(pos_quebra<=caracs){
                    quem.innerHTML = quem.innerHTML + texto.substring(pos_quebra,pos_quebra + quant_quebra) + " "
                    pos_quebra = pos_quebra + quant_quebra;
                }
            }else{
                quem.innerHTML = texto;
            }//end if do larg_total>larg_oficial
            quem.style.display = display_orig;
        }//end if do word wrap
    }//end for loop dos elementos
}

