//Constants
var invitationEmailCounter=0;


// Sets the form amount for deposit and withdraw functionality.
function setAmountForSubmission(amountFormId,transactionFormId,errorMessage){
	var amount=document.getElementById(amountFormId).amount.value;
	if(isNaN(amount) || amount <=0)
		alert(errorMessage);
	else{
		document.getElementById(transactionFormId).amount.value=amount;
		document.getElementById(transactionFormId).submit();
	}	
}

// Adds country flag from default images.
function setCountryFlagFromDefaultImages(flagFieldId,defaultFlagFieldId,defaultImageSelectedContentId,flagURL){
	document.getElementById(defaultFlagFieldId).value=flagURL;
	document.getElementById(flagFieldId).disabled=true;
	document.getElementById(defaultImageSelectedContentId).style.visibility='visible';
	
	//Collabse the library tree if this script was used to select an anthem.
	if(flagFieldId == 'edit_country_nationalanthem_box')
		hideShowDiv('default_anthem_list','H4');
	//Else was flag selector and we need to populate the current flag.
	else {
	    document.getElementById('current_flag_image').src="/main/images/default_flags/"+flagURL;
	}
}

// Enables the flag image upload form.
function enableFlagUploadForm(flagFieldId,defaultFlagFieldId,defaultImageSelectedContentId){
	document.getElementById(defaultFlagFieldId).value="null";
	document.getElementById(flagFieldId).disabled=false;
	document.getElementById(defaultImageSelectedContentId).style.visibility='hidden';
}

//This function selects a tab for for a data form with tabs.
//Pass in string "null" for the fckeditor field, if the div does not include FCKEditor.
//
//Other fields are:
// element = the element that should be displayed.
// container = the div that act as a container for the display.
function selectTab(element,container,fckeditor){
    var div = document.getElementById(container);
    var nodeList = div.childNodes;
	
	//Hide all divs.
	for(i=0;i<nodeList.length;i++){
		if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
			nodeList.item(i).style.display='none';
		}
	}
	
	//Make the selected div visible.
	document.getElementById(element).style.display='block';
	//Cope with a firefox bug and enable FCKEditor again if we display a text area.
	if(navigator.userAgent.indexOf("Firefox")!=-1 && fckeditor != "null"){
		var oEditor = FCKeditorAPI.GetInstance(fckeditor);

		if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG)
			oEditor.MakeEditable();
	}
	
	//Make all tab items inactive.
	var menuNodeList = document.getElementById('edit_menu_inner').childNodes;
	for(i=0;i<menuNodeList.length;i++){
		if(menuNodeList.item(i).nodeName=="LI" && menuNodeList.item(i).id!=element){
			menuNodeList.item(i).className='edit_menu_item_inactive';
		}
	}
	
	//Set the current tab active.
	document.getElementById('edit_menu_'+element).className='edit_menu_item_active';
}

//This function selects a tab for for a data form with tabs on the homepage.
//
// Fields are:
// element = the element that should be displayed.
// container = the div that act as a container for the display.
function selectHomepageIntroTab(element,container){
    var div = document.getElementById(container);
    var nodeList = div.childNodes;
	
	//Hide all divs.
	for(i=0;i<nodeList.length;i++){
		if(nodeList.item(i).nodeName=="DIV" && nodeList.item(i).id!=element){
			nodeList.item(i).style.display='none';
		}
	}
	
	//Make the selected div visible.
	document.getElementById(element).style.display='block';
	
	//Make all tab items inactive.
	var menuNodeList = document.getElementById('intro_menu').childNodes;
	for(i=0;i<menuNodeList.length;i++){
		if(menuNodeList.item(i).nodeName=="LI" && menuNodeList.item(i).id!=element){
			menuNodeList.item(i).className='intro_menu_item_inactive';
		}
	}
	
	//Set the current tab active.
	document.getElementById('intro_menu_'+element).className='intro_menu_item_active';
}

//This function hides or shows the given div.
//
// element = the element that should be hidden or shown.
// titleItem = the item that is used as title for the element and therefore should be always displayed.
function hideShowDiv(element,titleItem){
	var div = document.getElementById(element);
    var nodeList = div.childNodes;
	var titleNode;
	
	var isHideAction=true;
	
	//Hide or show all child elements.
	for(i=0;i<nodeList.length;i++){
		if(nodeList.item(i).nodeName!=titleItem && nodeList.item(i).id!=element && nodeList.item(i).style){
			if(nodeList.item(i).style.display=='none'){
				nodeList.item(i).style.display='block';
				isHideAction=false;
			} else
				nodeList.item(i).style.display='none';
		}else if(nodeList.item(i).nodeName==titleItem)
			titleNode=nodeList.item(i);
	}
	
	//Set the image for the open / close link.
	var titleChildren=titleNode.childNodes;
	for(i=0;i<titleChildren.length;i++){
		if(titleChildren.item(i).nodeName=='A'){
			if(isHideAction)
				titleChildren.item(i).innerHTML='<img src="../images/system/plus.png" alt="Maximize" title="Maximize" border="0"/>';
			else	
				titleChildren.item(i).innerHTML='<img src="../images/system/minus.png" alt="Minimize" title="Minimize" border="0"/>';
		}
	}	
}

// This function disables enter key for ajax search boxes.
function disableEnterKey(e){
	     var key;

	     if(window.event)
	          key = window.event.keyCode;     //IE
	     else
	          key = e.which;     //firefox

	     if(key == 13)
	          return false;
	     else
	          return true;
}

// Adds a new email input box to the invitation forms.
function addEmailInputBox(invitationEmailAddress){
	document.getElementById('invitation_send_submit').style.display='block';
	inputElement = Builder.node('input',{type:'text',name:'invitation_email'+invitationEmailCounter,id:'invitation_email'+invitationEmailCounter},'');
	spanElement = Builder.node('span',{className: 'form_caption'},invitationEmailAddress);
	labelElement = Builder.node('label',{htmlFor: 'invitation_email'+invitationEmailCounter},'');
	labelElement.appendChild(spanElement);
	inputWrapperElement = Builder.node('p',{},'');
	inputWrapperElement.appendChild(labelElement);
	inputWrapperElement.appendChild(inputElement);
	$('invitation_emails').appendChild(inputWrapperElement);
	invitationEmailCounter++;
}