function ShowDiv(divNameOrID)
{
if(document.getElementById(divNameOrID) != null)
	document.getElementById(divNameOrID).style.display = "block";
else
	try{
		document.getElementByName(divNameOrID).style.display = "block";
	}
	catch(e)
	{
		return;
	}	
}

function HideDiv(divNameOrId)
{

	if(document.getElementById(divNameOrId) != null)
		document.getElementById(divNameOrId).style.display = "none";
	else
		try{
			document.getElementByName(divNameOrId).style.display = "none";
		}
		catch(e)
		{
			return;
		}				
}

function ShowOneHideOthers(divToShow)
{
	var group = divGroup.split(";");
	
	for(var i=0; i < group.length -1 ; i++)
		HideDiv(group[i]);
						
	ShowDiv(divToShow);				
}

function SetThisRed(spanToSet)
{
	var group = spanGroup.split(";");
	
	for(var i=0; i < group.length -1; i++)
		if(document.getElementById(group[i]) != null)
			document.getElementById(group[i]).style.color = "#03174A";
	
	if(document.getElementById(spanToSet) != null)
		document.getElementById(spanToSet).style.color = "red";
}

function AlterImageSize(imageId,imageSrc,maxWidth,maxHeight)
{
	if(document.getElementById(imageId) == null)
		return;
		
	var img = new Image();
	
	img.src = imageSrc;
	
	var imgWidth = img.width;
	var imgHeight = img.height;
	
	if(imgWidth > maxWidth)
	{
		imgHeight = (imgHeight * maxWidth) / imgWidth;		
		imgWidth = maxWidth;
	}
	
	if( imgHeight > maxHeight )
	{
		imgWidth = (imgWidth * maxHeight) / imgHeight;
		imgHeight = maxHeight;
	}
	
    if (img.width > 0 && img.height > 0)
    {
        img.width = imgWidth;
        img.height = imgHeight; 
        document.getElementById(imageId).width = img.width;
        document.getElementById(imageId).height = img.height;
    }
	
	document.getElementById(imageId).src = img.src;
}

function ValidateForm(validationGroup,validationGroupName,validationPhrase,outputDivId,formId)
{
	if(document.getElementById(outputDivId).emailValidation != null && document.getElementById(outputDivId).emailValidation == "1")
		return;
		
	var group = validationGroup.split(";");
	var groupName = validationGroupName.split(";");
	var tdOutput =	"<table width='100%' cellpadding='5' cellspacing='0' class='channelListContent' style='color: red;'>"+
					"<tr><td><b>"+validationPhrase+"</b></tr></tr>";
	var validation = false;
	var firstToVerify = -1;
	for(var i = 0; i < group.length - 1; i++)
	{
		if(document.getElementById(group[i]) != null)
			if(document.getElementById(group[i]).value.length == 0)
			{
											
				tdOutput += "<tr><td>"+groupName[i]+"</tr></td>";
				validation = true;
				
				if(firstToVerify == -1)
					firstToVerify = i;
			}
	}
	
	if(validation)
	{
		document.getElementById(group[firstToVerify]).focus();
		tdOutput += "</table>";
		document.getElementById(outputDivId).innerHTML = tdOutput;
		document.getElementById(outputDivId).style.display = "block";
		return false;
	}
	else
		document.getElementById(outputDivId).style.display = "none";

	if(document.getElementById(outputDivId).style.display != "block")
		document.getElementById(formId).submit();
	
}

function emailValidator(fieldId,errorMessage,outputDivId) {

	if(document.getElementById(fieldId).value == "")
	{
		document.getElementById(outputDivId).style.display = "none";
		document.getElementById(outputDivId).emailValidation = "0";
		return;
	}
	
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	if(!email.test(document.getElementById(fieldId).value))
	{
		var tdOutput =	"<table width='100%' cellpadding='5' cellspacing='0' class='channelListContent' style='color: red;'>"+
					"<tr><td><b>"+errorMessage+"</b></tr></tr></table>";
		document.getElementById(outputDivId).innerHTML = tdOutput;
		document.getElementById(outputDivId).style.display = "block";
		document.getElementById(fieldId).focus();
		document.getElementById(outputDivId).emailValidation = "1";
	}
	else
	{
		document.getElementById(outputDivId).style.display = "none";
		document.getElementById(outputDivId).emailValidation = "0";
	}
	
}

function ValidateNumberOfChars(idInput,outputDivId,errorText,minNumber,formId)
{
	
	if(document.getElementById(idInput).value.length < minNumber)
	{
		document.getElementById(outputDivId).innerHTML = "<font class='channelListContent' style='color: red;'>"+errorText+"</font>";
		document.getElementById(outputDivId).style.display = "block";
		document.getElementById(idInput).focus();
		return false;
	}
	
	document.getElementById(formId).submit();
}


function ValidatePaymentsAux()
{
	if(document.getElementById('outroMeioPag').checked)
		return true;

	var cardNameObj = document.getElementsByName('fldNomeCartao')[0];	
	var paymentType = "CCRED";	
	var cardType = document.getElementById('fldTipoCartao').value;		
	var currentYearObj = document.getElementsByName('fldCartaoValidadeAno')[0];
	var currentMonthObj = document.getElementsByName('fldCartaoValidadeMes')[0];
	var securityCode = document.getElementsByName('fldCodigoSeguranca')[0];
	
	
		if (paymentType == "CCRED")
		{
			cardNameObj.value = RetiraAcentos(cardNameObj.value);
			
			if (Trim(cardNameObj.value)== "" || 
				!VerifyChar( removeSpace(String(cardNameObj.value))))
			{
				cardNameObj.focus();
				alert(document.getElementById('txtError1').value);
				return false;
			}
			
			if (!VerifyNumber(document.getElementsByName('fldCodigoSeguranca')[0].value))
			{
				alert(document.getElementById('txtError2').value);		    
				return false;
			}
			

			switch (cardType)
			{
				case "AMEX":
					if (document.getElementsByName('fldNumeroCartao1')[0].value.length < 4 ||
						document.getElementsByName('fldNumeroCartao2')[0].value.length < 6 ||
						document.getElementsByName('fldNumeroCartao3')[0].value.length < 5 ||					
						!VerifyNumber(document.getElementsByName('fldNumeroCartao1')[0].value) ||
						!VerifyNumber(document.getElementsByName('fldNumeroCartao2')[0].value) ||
						!VerifyNumber(document.getElementsByName('fldNumeroCartao3')[0].value) )
						
					{	
						
						document.getElementsByName('fldNumeroCartao1')[0].focus();
						ShowError('error_payment_msg', javascriptSentences[11]);
						return false;
						
					}
					
					if (document.getElementsByName('fldCodigoSeguranca')[0].value.length != 3)
					{
						ShowError('error_payment_msg', javascriptSentences[12]);			    
						return false;
					}
					
					break;
					
				default:
					if (document.getElementsByName('fldNumeroCartao1')[0].value.length < 4 ||
						document.getElementsByName('fldNumeroCartao2')[0].value.length < 4 ||
						document.getElementsByName('fldNumeroCartao3')[0].value.length < 4 ||
						document.getElementsByName('fldNumeroCartao4')[0].value.length < 4 ||
						!VerifyNumber(document.getElementsByName('fldNumeroCartao1')[0].value) ||
						!VerifyNumber(document.getElementsByName('fldNumeroCartao2')[0].value) ||
						!VerifyNumber(document.getElementsByName('fldNumeroCartao3')[0].value) ||
						!VerifyNumber(document.getElementsByName('fldNumeroCartao4')[0].value) 			 
					)
					{
						document.getElementsByName('fldNumeroCartao1')[0].focus();
						alert(document.getElementById('txtError3').value);
						return false;
					}
					
					if (document.getElementsByName('fldCodigoSeguranca')[0].value.length != 3)
					{
						alert(document.getElementById('txtError3').value);		    
						return false;
					}
					
					if ( document.getElementsByName('fldCodigoSeguranca')[0].value == '' || !VerifyNumber(document.getElementsByName('fldCodigoSeguranca')[0].value))
					{
						document.getElementsByName('fldCodigoSeguranca')[0].focus();
						alert(document.getElementById('txtError3').value);	
						return false;
					}
					break;	
			}
			
			if (!currentMonthObj.value != '')
			{	
				currentMonthObj.focus();
				alert('Data de Validade Incorrecta');
				return false;
			}
			if (!currentYearObj.value != '')
			{	
				currentYearObj.focus();
				alert('Data de Validade Incorrecta');
				return false;
			}
					
		}

				
	return true;
	
}




function RetiraAcentos(Campo) 
{
   //Campo = escape(Campo);
   //var Acentos =  "áàãââÁÀÃÂéêÉÊíÍóõôÓÔÕúÚÜacedilhas";
   var Acentos = "%E1%E0%E3%E2%E2%C1%C0%C3%C2%E9%EA%C9%CA%ED%CD%F3%F5%F4%D3%D4%D5%FA%DA%DC%E7%C7"
   //var Traducao = "AaaaaAAAAeeEEiIoooOOOuUU";
   var Traducao = "aaaaaAAAAeeEEiIoooOOOuUUcC";
   var Posic, Carac;
   var TempLog = "";
   for (var i=0; i < Campo.length; i++)
   {      
	Carac = Campo.charAt(i);
	Posic  = Acentos.indexOf(escape(Carac));
	if (Posic > -1 && escape(Carac).length > 1)
	{
		TempLog += Traducao.charAt(Posic / 3);
		//alert("sim" + Posic);
	}
	else
	{
		TempLog += Campo.charAt(i);
		//alert("nao" + Posic);
	}
   }
      return (TempLog);
}

function removeSpace(s) 
{  
  
  var temp = "";
  
  for(var i=0; i < s.length ;i++)
  {
	if(s.charAt(i) != ' ')
		temp += s.charAt(i);
  }
  
  return temp
  
  
}

function VerifyChar(str)
{
	str = RetiraAcentos(Trim(str));
	
	str = String(str);	
	str = str.replace(" ", "");
	str = str.toUpperCase(); 
	//alert(str);
	var ch;
	if (str == "")	
		return false;
	for (var i = 0; i < str.length; i++)
	{
		ch = str.charAt(i);		
		if ((ch < "A" || "Z" < ch))			 
			return false;
			
	}
	return true;
}

function Trim(str)
{
     return str.replace(/^\s+|\s+$/, ''); 

}

function VerifyNumber(str)
{
	var control = true;
	var ch;
	
	for (var i = 0; i < str.length; i++)
	{
		ch = str.charAt(i);
		if (ch < "0" || "9" < ch) 
			control = false;
	}
	
	return control;
}
/*
function VerifyDate(year, month, day)
{
	if(year != '' && month != '')
		return true;
	else
		return false;
}
*/
/*
function CheckDateAux(day, month, year) // determina se a data é ou não válida
{

            var control = (TestIfRegularYear(year) ? 28 : 29);
                                                                      
            if (month == 2)
                        return (day <= control);
            return (day <= GetDaysInMonth(month));

}

function TestIfRegularYear(year)
{           
			
            return !(((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0);

}

function GetDaysInMonth(month)
{

            var minDaysInMonth = 30;
            var maxDaysInMonth = 31;

            if (month < 8)
                   return (minDaysInMonth + (month % 2));

            return (maxDaysInMonth - (month % 2));

}
*/