/// <summary>
/// Hides all divs with a given prefix
/// </summary>
/// <param name="prefix">Spans ID prefix to hide</param>
function HideAllDivs(prefix)
{
	var aDivs;
	var div;
	var i;
	var elem;
	var id;
	
	try
	{
		aDivs = document.getElementsByTagName("TABLE");
		
		for (i=0; i < aDivs.length; i++)
		{
			div = aDivs[i];
			id = div.id;

			if (id.indexOf(prefix) != -1)
			{
				HideDiv(id);
			}
		}
	}
	catch(e)
	{
	}	
}


/// <summary>
/// Hides a div
/// </summary>
/// <param name="id">Div ID to hide</param>
function HideDiv (id)
{
	try
	{
		elem = document.getElementById(id);
		elem.style.display = "none";
	}
	catch(e)
	{
	}

}	

/// <summary>
/// Shows a span
/// </summary>
/// <param name="id">Div ID to show</param>
function ShowDiv (id)
{	
	try
	{
		elem = document.getElementById(id);
		elem.style.display = "inline";
	}
	catch(e)
	{
	}

}	

/// <summary>
/// Toggles Span visibility
/// </summary>
/// <param name="id">Span ID to toggle visibility</param>
/// <param name="prefix">Spans ID prefix to hide</param>
function HideShowSpan (id,prefix)
{
	var elem;
	var display;
	try
	{
		elem = document.getElementById(id);
		display = elem.style.display;
		
		HideAllSpans(prefix);
		if (display == 'none')
			ShowSpan(id);
	}
	catch (e)
	{		
	}
}

/// <summary>
/// Hides a span
/// </summary>
/// <param name="id">Span ID to hide</param>
function HideSpan (id)
{
	try
	{
		elem = document.getElementById(id);
		elem.style.display = "none";
	}
	catch(e)
	{
	}

}	

/// <summary>
/// Shows a span
/// </summary>
/// <param name="id">Span ID to show</param>
function ShowSpan (id)
{	
	try
	{
		elem = document.getElementById(id);
		elem.style.display = "inline";
	}
	catch(e)
	{
	}

}	

/// <summary>
/// Hides all spans with a given prefix
/// </summary>
/// <param name="prefix">Spans ID prefix to hide</param>
function HideAllSpans(prefix)
{
	var aSpans;
	var span;
	var i;
	var elem;
	var id;
	
	try
	{
		aSpans = document.getElementsByTagName("SPAN");
		
		for (i=0; i < aSpans.length; i++)
		{
			span = aSpans[i];
			id = span.id;

			if (id.indexOf(prefix) != -1)
			{
				HideSpan(id);
			}
		}
	}
	catch(e)
	{
	}	
}

/// <summary>
/// Shows all spans with a given prefix
/// </summary>
/// <param name="prefix">Spans ID prefix to show</param>
function ShowAllSpans(prefix)
{
	var aSpans;
	var span;
	var i;
	var elem;
	var id;
	
	try
	{
		aSpans = document.getElementsByTagName("SPAN");
		
		for (i=0; i < aSpans.length; i++)
		{
			span = aSpans[i];
			id = span.id;

			if (id.indexOf(prefix) != -1)
			{
				ShowSpan(id);
			}
		}
	}
	catch(e)
	{
	}	
}

/// <summary>
/// Toggles Visibility of FAQ Categories
/// </summary>
/// <param name="id">Category Span ID show</param>
/// <param name="CategotyPrefix">Category Spans ID prefix to hide</param>
/// <param name="FAQPrefix">FAQ Spans ID prefix to hide</param>
function ToggleFAQCategories(id, CategoryPrefix, FAQPrefix)
{
	try
	{
		HideAllSpans(CategoryPrefix);
		ShowSpan(id);
		HideAllSpans(FAQPrefix);
	}
	catch(e)
	{	
	}

}

/// <summary>
/// Obtains the mask from the Best for me area
/// </summary>
function GetMask()
{
  var aElems;
  var aSpans;
  var strInputType;
  var strMask;
  var strTempMask;
  var bFoundRadioSelected;
  
  strMask = "";
  
  aSpans = document.getElementsByTagName("SPAN");
  //Iterates spans - categories
  for (cat_index = 0; cat_index < aSpans.length; cat_index++)
  {
	 strInputType = "";
	 bFoundSelected = false;
	 
	 //Mask value before the inputs
	 strTempMask = strMask;
	 
	 aElems = document.getElementsByName(aSpans[cat_index].id + "_profile");
	 if (aElems.length > 0)
	 	strInputType = aElems[0].type;
	
	 //Iterates inputs - questions
	 for (index = 0; index < aElems.length; index++)
     {  
			if (aElems[index].checked)
			{
				strMask = strMask + "1";
			}
			else
				strMask = strMask + "0";

	 } //for inputs
  } // for spans
  
  return strMask;
}

/// <summary>
/// Returns a string padded with length characters
/// </summary>
function PadString(strExp, length)
{
    var index;
	var result;
	
	result = strExp;
	try
	{
	result = "";
	if (strExp.length < length)
	{
  	  for (index = strExp.length; index < length; index++)
	  {
	    result = "0" + result;
	  }
	}
	result = result + strExp;
	}
	catch(e)
	{}
	
	return result;
}

/// <summary>
/// Performs a bitwise AND without carriage return
/// </summary>
function BinaryAnd (strPartA, strPartB)
{
  var strLeftSideOperator, strRightSideOperator;
  var iOperatorLength;
  var index;
  var iSumPart;
  var result;
  
  result = strPartA;
  try
  {
	strLeftSideOperator = "";
	strRightSideOperator = "";
	result = "";
	  
	if (strPartA.length > strPartB.length)
	{
		strLeftSideOperator = strPartA;
		strRightSideOperator = PadString(strPartB,strPartA.length);
	}
	else
	{
		strLeftSideOperator = PadString(strPartA,strPartB.length);
		strRightSideOperator = strPartB;
	}

	iOperatorLength = strPartA.length;
	for (index = iOperatorLength - 1; index >= 0; index--)
	{
		iSumPart = parseInt(strLeftSideOperator.substring(index,index+1)) * parseInt(strRightSideOperator.substring(index,index+1))
		result = iSumPart + result;
	}
  }
  catch(e)
  {}

  return result;  
}


/// <summary>
/// Applies the mask to the BestForMe User Control
/// </summary>
function ApplyMask()
{
  var strMask;  

  var aSpans;
  var span;
  var i;
  var id;
  var product_profile_sep;
  var product_profile;
  var profileKeywork;
  
  profileKeywork = "PROFILE_"
  
  try
  {

	strMask = GetMask();
	// resets the product visibility
	HideAllSpans (profileKeywork);	
	aSpans = document.getElementsByTagName("SPAN");

	for (i=0; i < aSpans.length; i++)
	{	
		span = aSpans[i];
		id = span.id;
		product_profile_sep = id.indexOf(profileKeywork);
		if (product_profile_sep > 0)
		{
		  product_profile = id.substring(product_profile_sep + profileKeywork.length,id.length);
		  if (BinaryAnd(strMask,product_profile) == strMask)
		  {
			ShowSpan(id);
		  }
		}
	}    
	
  }
  catch(e)
  {}
}

/// <summary>
/// Resets the inputs with a given prefix
/// </summary>
function ResetOptions(prefix)
{
	var aElems;
	var elem;
	var id;
	
	try
	{
		aElems = document.getElementsByTagName("INPUT");
		
		for (i=0; i < aElems.length; i++)
		{	
			elem = aElems[i];
			id = elem.id;
			
			if (id.indexOf(prefix) != -1)
			{
				elem.checked = false;
			}
		}
		ApplyMask();
	}
	catch(e)
	{}
}

/// <summary>
/// Changes a style class for an element
/// </summary>
function ChangeClass(id,ClassName)
{
	var elem;
	
	try
	{
		elem = document.getElementById(id);
		elem.className = ClassName;
	}
	catch(e)
	{
	}
}

/// <summary>
/// Changes the visibilities of FAQs and Glossaries
/// </summary>
function ToggleFAQGlossary(SpanIDToShow,SpanPrefixToHide)
{
	try
	{
		//Hide all spans
		HideAllSpans (SpanPrefixToHide);
		
		ShowSpan(SpanIDToShow);
	}
	catch(e)
	{
	}
	
	
}

/// Switches the DropDown Menus
/// Input: spanName: Parent Menu name being switch
function SwitchMenu(spanName)
{
	var elems;
	var elem;
	var nameParts;
	var MainSplit;
	var MainSplitCount;
	var arDivs;
	var mainDiv;
	var arActiveElem;
	var activeElem;

	try
	{		
		//Determines the start menu level
		MainSplit = spanName.split("_");
		MainSplitCount = MainSplit.length;
	
		//obtains all Divs from screen
		arDivs = document.getElementsByTagName("div");
		for (var i=0; i< arDivs.length; i++)
		{
			if (arDivs[i].id.indexOf("masterdiv") != -1)
			{
				mainDiv = arDivs[i];
				break;	
			}
		}
		
		//obtains all inputs into an array
		arActiveElem = mainDiv.getElementsByTagName("input");
		for (var i=0; i< arActiveElem.length; i++)
		{
			activeElem = arActiveElem[i];
			
			//verifies if it's the Active menu Hidden input
			if (activeElem.id.indexOf("ActiveMenu") != -1)
			{		
				activeElem.value = spanName;
				break;
			}
			
		}	
		
		//Build the menu full path 
		//to build the breadcrumb
		BuildMenuPath();
		
		//obtains all spans into an array
		elems =  mainDiv.getElementsByTagName("span");
		for (var i=0; i< elems.length; i++)
		{
			elem = elems[i];
			//determines the level of menu item
			nameParts = elem.id.split("_");
			
			//Verifies if it's a sub-menu 
			//The "_" is inserted to prevent other menus with
			//similar names to be shown
			if (elem.id.indexOf(spanName + "_") != -1)
			{
			
				//verifies if it's a direct sub-menu
				if ((nameParts.length - 1 ) == MainSplitCount)
				{
					//switch menu visibility
					if (elem.style.display == "block")
					{
						elem.style.display = "none";
					}
					else					
					{
						elem.style.display = "block";
					}
				}
				
				//if it's not a direct sub-menu hides it
				if ((nameParts.length - 1 ) > MainSplitCount)			
				{
					elem.style.display = "none";
				}
	
			}
			else
			{
				//make sure the menu is not an ancester
				if (spanName.indexOf(elem.id) == -1)
					if (nameParts.length > MainSplitCount)
					{
						elem.style.display = "none";
					}
			}
	
		}			
	}
	catch(e)
	{
		//alert(e.message);
	}
	
}

/// Function that reads from a hidden field and shows
/// the active field there indicated
function SetActiveMenuItem()
{
	var elemActive;
	var arInputs;
	
	
	
	try
	{
		arInputs = document.getElementsByTagName("input");
		for (var i=0; i< arInputs.length; i++)
		{
			if (arInputs[i].id.indexOf("m_inActiveMenu") != -1)
			{
				elemActive = arInputs[i];
				break;	
			}
		}		
				
		if (elemActive.value != "")
		{
			ShowMenu(elemActive.value);
		}	
			
	}
	catch(e)
	{
		//alert(e.message);
	}
	
}

/// Shows the indicated menu and all at the same level, sons of same menu or upper
function ShowMenu(menuName)
{
	var elems;
	var arDivs;
	var mainDiv;
	var MainSplit;
	var MainSplitCount;
	var nameParts;
	var i;
	var bParentShown;
	var parentName;
	
	try
	{
	
		//Determines the start menu level
		MainSplit = menuName.split("_");
		MainSplitCount = MainSplit.length;
		
		
		//build the parent name
		parentName = '';
		for (var i=0; i < MainSplitCount - 1; i++)
		{
			parentName += MainSplit[i];
			if (i < MainSplitCount - 2)
				parentName += "_";
		}
		
		if (parentName != '' && parentName != 'menu')
		{
			//obtains all Divs from screen-Discover masterdiv
			arDivs = document.getElementsByTagName("div");
			for (i=0; i< arDivs.length; i++)
			{
				if (arDivs[i].id.indexOf("masterdiv") != -1)
				{
					mainDiv = arDivs[i];
					break;	
				}
			}
			
			//obtains all spans in an array
			elems =  mainDiv.getElementsByTagName("span");
			for (var i=0; i< elems.length; i++)
			{
				elem = elems[i];

				//determines the level of menu item
				nameParts = elem.id.split("_");
				
				//It's an element of the same level...show
				//The "_" is inserted to prevent other menus with
				//similar names to be shown				
				if (elem.id.indexOf(parentName + "_") != -1 && nameParts.length == MainSplitCount)
				{
					elem.style.display = "block";				
				}					
			}	
			
			// show parents - recursive
			if (MainSplitCount > 3)	
				ShowMenu(parentName);
		}
	}
	catch(e) {}
	
}



/// Builds the menu path for the breadcrumb
function BuildMenuPath()
{

	var activeElem;
	var ActiveName;
	var ActiveSplit;
	var ActiveSplitCount;
	var parentName;
	var parentElem;
	var FullMenuPath;

	try
	{	
		FullMenuPath = LoadElementByPartialName("div","masterdiv","input","FullMenuPath");
		FullMenuPath.value = "";
		
		activeElem = LoadElementByPartialName("div","masterdiv","input","ActiveMenu");	
		
		ActiveName = activeElem.value;	
		
		//obtains the level of the active menu
		ActiveSplit = ActiveName.split("_");
		ActiveSplitCount = ActiveSplit.length;
		
		parentName = "menu";
		for (var i=1; i < ActiveSplitCount; i++)
		{
			parentName += "_" + ActiveSplit[i];
			//Load the menu Label element
			parentElem = LoadElementByPartialName("span",parentName,"a","_MenuLabel");
	
			//if it's the first time don't place the seperator
			if (FullMenuPath.value != "")		
				FullMenuPath.value += "&nbsp;>&nbsp;";
				
			// childNodes instead of innerText for mozilla compatibility		
			//FullMenuPath.value += parentElem.innerText;			
			FullMenuPath.value += parentElem.childNodes[0].nodeValue;	
			
		}
				
	}
	catch(e)
	{
		//alert(e.message);
	}
	
}




/// Builds the BreadCrumb based on the hidden values 
function BuildBreadCrumb(BreadCrumbPath)
{
	var BeginCrumb;
	var AreaCrumb;
	var AreaCrumbLink;
	var MenuCrumb;
	var PageCrumb;	
	var BreadCrumb;
	var Seperator;
	var BreadCrumbElement;
	
	BreadCrumb = "";
	Seperator = "&nbsp;>&nbsp;"	
	
	try
	{
		//fixed crumb
		BeginCrumb = "<a href='../site/homepage.aspx'>In&iacute;cio</a>";
		
		//Load Area Description crumb
		AreaCrumb = LoadElementByPartialName("","","input","hdnAreaDescription");

		//Load Area Description crumb Link
		AreaCrumbLink = LoadElementByPartialName("","","input","hdnAreaDescriptionLink");

		//Load Menu crumb
		MenuCrumb = LoadElementByPartialName("div","masterdiv","input","FullMenuPath");
		
		//Load Page crumb
		PageCrumb = LoadElementByPartialName("","","input","hdnPageCrumb");	

		//*** Begin building the output ***
		BreadCrumb = BeginCrumb;	
				
		if (AreaCrumb != null && AreaCrumb.value != "")
		{
			if (AreaCrumbLink != null && AreaCrumbLink.value != "")
				BreadCrumb += Seperator + "<a href='" + AreaCrumbLink.value + "'>" + AreaCrumb.value + "</a>";				
			else
				BreadCrumb += Seperator + AreaCrumb.value;
		}
			
		if (MenuCrumb != null && MenuCrumb.value != "")
			BreadCrumb += Seperator + MenuCrumb.value;
		
		if (PageCrumb != null && PageCrumb.value != "")
			BreadCrumb += Seperator + PageCrumb.value;								
		
		BreadCrumbElement = LoadElementByPartialName("","","div",BreadCrumbPath);	
		if (BreadCrumbElement != null & BreadCrumb != "")
			BreadCrumbElement.innerHTML = BreadCrumb;		
			
	
	}	
	catch(e)
	{
		//alert("ERRO:" + e.message);
	}
	
}


/// Loads an element from screen based on its partial name and TagName
/// It can narrow the search by provding a masterTag section
function LoadElementByPartialName(masterTag, masterName, tagName, elementName)
{
	var arDivs;
	var mainDiv;
	var arElems;
	var elem;	
	var bFound;
	
	mainDiv = null;
	elem = null;	
	bFound = false;
	
	try
	{
		if (masterName != "" && masterTag != "")
		{	
			//obtains all Divs from screen
			arDivs = document.getElementsByTagName(masterTag);
			
			for (var i=0; i< arDivs.length; i++)
			{
				if (arDivs[i].id.indexOf(masterName) != -1)
				{
					mainDiv = arDivs[i];
					break;	
				}
			}
		}
		
		
		if (mainDiv != null)	
			//obtains all inputs into an array
			arElems = mainDiv.getElementsByTagName(tagName);					
		else
			arElems = document.getElementsByTagName(tagName);
			
		for (var i=0; i< arElems.length; i++)
		{
			elem = arElems[i];
						
			//verifies if it's the Active menu Hidden input
			if (elem.id.indexOf(elementName) != -1)
			{	
				//found
				bFound = true;
				break;
			}
		}
		
		if (!bFound)
			elem = null;
		return elem;
	}
	
	catch (e)
	{
		return null;
	}

}


/// Jumps to the url of the selected option in a dropdown box
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  var optionValue;
  var jaRedireccionou =false;
  try
  {
	optionValue = selObj.options[selObj.selectedIndex].value;	
	
	if (optionValue.length > 0)
	{
		// se a opcao for do tipo "abcdef;1", a opcao depois de ';' indica se e' 
		// para abrir numa nova janela ou nao
	
		var args = optionValue.split(";");
		if(args.length>1)
		{
			if(args[1] == "1")
			{
				targ = window.open();
				targ.location = args[0];
				jaRedireccionou = true;
			}
		}			
		
		if(!jaRedireccionou)
		{	
			eval(targ+".location='"+args[0]+"'");
		}
		if (restore) selObj.selectedIndex=0;
	}
  }
  catch(e)
  {
  }
}


/// Performs the contact search with full text
function SearchContact()
{
	var searchInput 
	var searchStringElem;
	
	try
	{		
		searchInput = "SearchString";		
		searchStringElem = document.getElementById(searchInput);
	
		location.href = '../Site/WhereWeAre.aspx?TotalText=' + searchStringElem.value;
	}
	catch(e)
	{
		//alert(e.message);
	}
	
}

function SearchSite()
{
	var searchInput;
	var searchStringElem;
	
	try
	{		
		searchInput = "SiteSearch";
		searchStringElem = document.getElementById(searchInput);
	
		location.href = '../Site/Search.aspx?all=True&query=' + searchStringElem.value;
	}
	catch(e)
	{
		//alert(e.message);
	}
	
}

// Sets default buttons.
// Compatibilidade: -Mozilla, IE...
function fnTrapKD(btn, event)
{
	try
	{
		if (document.all){
			if (event.keyCode == 13)
			{
				event.returnValue=false;
				event.cancel = true;
				btn.click();
			}
		}
		else if (document.getElementById || document.layers)
		{		
			if (event.which == 13)
			{
				event.returnValue=false;
				event.cancel = true;
				btn.onclick();				
			}
		}
	}
	catch(e)
	{
		//alert(e.message);
	}
}


// Validate BirthDay field - current age must be between 18 and 45
// The age is calculated only using the years, as in the BSG service
function ValidateBirthdate( object, args ) 
{ 
	var birthDateString;
	var birthYearString = new String();
	var birthYearInt;
	var yearNow = new Date();
	var age;
	
	try
	{
		args.IsValid = true;

		birthDateString = String(args.Value);
		
		// gets the year from date. The format validator does not allow a date in another format
		birthYearString = birthDateString.substring(6, 10);
		
		// year not well formed - The format validator will give the error message,
		// this validator doesn't need to show its own
		if (birthYearString.length != 4)
		{
			args.IsValid = false;
			return;
		}
		
		// trick to convert a string to int	
		birthYearInt = birthYearString * 1;
		age = yearNow.getYear() - birthYearInt;

		if (age < 18 || age > 45)
		{
			args.IsValid = false;
		}
		else
		{
			args.IsValid = true;
		}
	}
	catch(e)
	{
		// there was an error getting the year. The format validator will give the error message,
		// this validator doesn't need to show its own
		args.IsValid = true;
	}

} 







var dtCh= "-";
var minYear=1800;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("O formato da data deverá ser: dd-mm-aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Por favor, indique um mês válido.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Por favor, indique um dia válido.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Por favor, indique um ano válido (entre"+minYear+" e "+ maxYear + ")")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Por favor, indique uma data válida.")
		return false
	}
return true
}

function ValidaData(source){
	var dt= document.getElementById(source.id);
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }

function SetValueToTextInputs(prefix, value)
{
	var aTextInputs;
	var textInput;
	var i;
	
	try
	{
		aTextInputs = document.getElementsByTagName("INPUT");
		
		for (i=0; i < aTextInputs.length; i++)
		{
			textInput = aTextInputs[i];

			if (textInput.id.indexOf(prefix) != -1)
			{
				textInput.value = value;
			}
		}
	}
	catch(e)
	{
	}	
}
