function random() {

	var randomInt;
	
	randomInt = parseInt(1 + (Math.random()*16));
		
	
	if (document.teaser_main.src) {
		document.teaser_main.src="Header"+randomInt+"_home_img.jpg";
		//document.teaser_main.src="home_image"+randomInt+".jpg";
	}

}

function randomJob() {

	var randomInt;
	randomInt = parseInt(1+(Math.random()*10));
	
	if (document.teaser_main.src) {
		document.teaser_main.src="jobs_foto"+randomInt+".jpg";
	}

}

function del_confirm(msg, url) {

  if (confirm("Soll " + msg + " wirklich gel&#246;scht werden?")) {
  	location.href = url;
  }
}

function win_open(href, width, height)
{
	if ((width == '') &&  (height == ''))  
	{
		window.open(href, '_blank', 'toolbar=no,location=no,status=no,menubar=no,personalbar=no,scrollbars=yes,resizable=yes,screenx=50,left=50,screenY=50,top=50');
	} 
	else
	{
		window.open(href, '_blank', 'toolbar=no,location=no,status=no,menubar=no,personalbar=no,scrollbars=no,width=' + width + ',height=' + height + ',resizable=yes,screenx=50,left=50,screenY=50,top=50')
	}
}


function print_open(href, width, height)
{
		
	if ((width == '') &&  (height == ''))  
	{
		window.open(href, '_blank', 'toolbar=no,location=no,status=no,menubar=no,personalbar=no,scrollbars=yes,resizable=yes,screenx=50,left=50,screenY=50,top=50');
	} 
	else
	{
		window.open(href, '_blank', 'toolbar=no,location=no,status=no,menubar=no,personalbar=no,scrollbars=yes,width=' + width + ',height=' + height + ',resizable=yes,screenx=50,left=50,screenY=50,top=50')
	}
}




function checkDelete() {

	if (confirm("")) {
		return true;
	}
	else {
		return false;
	}
}

function CheckMandatoryFields(oForm) {
	
	var i, j, sElementName, sElementType, bCheck;
	var bError = 0;
	for (i = 0; i < oForm.elements.length; i++) {
		
		oElement = oForm.elements[i];
		sElementName = oElement.name;
		sElementType = oElement.type;
				
		if (sElementName != "") {
			
			if (sElementName.length > 3) {

				if (sElementName.substr(sElementName.length - 3, 3) == "_mf") {
			
					switch (sElementType) {
						
						case "text":
							if (oElement.value == "") {
								bError = 1;
							}
							break;
							
						case "checkbox":
						
							if (oElement.checked == false) {
								bError = 1;
							}
							break;
							
						case "select-one":
						
							if (oElement[oElement.selectedIndex].value == "" || oElement[oElement.selectedIndex].value == "null") {
								bError = 1;
							}
							break;
							
						case "select-multiple":

							bCheck = 0;

							for (j = 0; j < oElement.options.length; j++) {
								if (oElement.options[j].selected) {
									bCheck++;
								}
							}

							if (iMultiple == 0) {
								bError = 1;
							}
							break;
							
						case "radio":
							var aRadioElements = document.getElementsByName(sElementName);
							if (!checkRadio(aRadioElements)) {
								bError = 1;
							}
							break;
					}
				}	
				
				if (sElementName == "email" && (oForm.newsletter_anmeldung || oForm.newsletter_abmeldung)) {
				
					if(oElement.value != "" && checkEmail(oElement.value) == false){
						bError = 3;
					}
					if (oElement.value == "") bError = 1;
				}	
				
				
				
				//validate only numeric fileds
				if (oElement.getAttribute('validate')) {
					if (oElement.getAttribute('validate').indexOf("numeric") != -1) {						
						if (!isNumeric(oElement.value)) {
							bError = 2;
							oElement.focus();
							oElement.select();
							break;
						}
					}	
				}	
				
				//validate phone fileds
				if (oElement.getAttribute('validate')) {
					if (oElement.getAttribute('validate').indexOf("phone") != -1) {						
						if (!isAllowedPhone(oElement.value)) {
							bError = 2;
							oElement.focus();
							oElement.select();
							break;
						}
					}	
				}	
				
				
				//validate email
				if (oElement.getAttribute('email')) {
					var splitted = oElement.value.match("^(.+)@(.+)$");
					if(splitted == null) {
						bError = 3;
					}
					else {
						if(splitted[1] != null ) {		
							var regexp_user=/^\"?[\w-_\.]*\"?$/;		
							if(splitted[1].match(regexp_user) == null) bError = 3;
						}
						else {
							bError = 3;
						}
						
						if(splitted[2] != null) {
							var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
							if(splitted[2].match(regexp_domain) == null) {
								var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
								if(splitted[2].match(regexp_ip) == null) bError = 3;
							}
						}
						else {
							bError = 3;
						}						
					}
				}
			}
		}
	}
	
	
	if (oForm.contact_country_mf && oForm.contact_nationality_mf) {
		
		var sCountries = "14,15,21,37,42,55,57,66,68,73,75,87,100,107,108,127,132,162,163,167,179,191,217";
		
		var sCountry = oForm.contact_country_mf.value;
		var sNationality = oForm.contact_nationality_mf.value;

		//check only if not swiss resident or something with usa
		if (sCountry != 42 || sCountry == 225 || sNationality == 225) {
								
			if (sCountries.indexOf(sCountry) == -1 || sCountries.indexOf(sNationality) == -1) {
				bError = 4;
			}
		}
	}
	
	// Only for Newsletter Anmeldung
	if (oForm.newsletter_anmeldung) {
		if (oForm.investor_relations && oForm.investor_relations.checked == false && oForm.money_bonus.checked == false && oForm.money.checked == false && oForm.trade.checked == false) bError = 5;
		if (oForm.email.value == "") bError == 1;
	}
	
	// Only for Newsletter Abmeldung
	if (oForm.newsletter_abmeldung && bError == 0) {
		var sAbNews = "";
		if (oForm.investor_relations && oForm.investor_relations.checked == true) sAbNews += "1";
		if (oForm.money_bonus && oForm.money_bonus.checked == true) sAbNews += "2";
		if (oForm.money && oForm.money.checked == true) sAbNews += "3";
		if (oForm.trade && oForm.trade.checked == true) sAbNews += "4";
		
		if (oForm.INXMAIL_HTTP_REDIRECT) {
			var sString = oForm.INXMAIL_HTTP_REDIRECT.value;
			if (sString.indexOf("&nl=") != -1) sString = sString.substring(0,sString.indexOf("&nl="));
			oForm.INXMAIL_HTTP_REDIRECT.value = sString + "&nl=" + sAbNews;
		}
		
		if (oForm.email.value == "") bError = 1;
	}
	
	if (document.getElementById("contact_schalterkontakt_ja")) {
		if (document.getElementById("contact_schalterkontakt_ja").checked) {
			if (oForm.contact_niederlassungen.value == "") {
				oForm.contact_niederlassungen.focus();
				bError = 1;
			}
		} 
	}


	switch (bError) {
		case 0:
			if (document.insertContact)	document.insertContact.submit();
			oForm.submit();
			break;
		
		case 1:
			alert("Veuillez s'il-vous plaît remplir tous les champs obligatoires!");
			break;
		
		case 2:
			alert("Nur numerische Werte erlaubt!");
			break;
		
		case 3:
			alert("Veuillez saisir une E-mail adresse valide.");
			break;
			
		case 4:
			alert("Merci de votre demande et de l'intérêt que vous portez à nos produits. Nous regrettons de vous informer que nous ne sommes pas autorisés de nouer des relations d'affaires avec vous en raison de votre domicile ou de votre nationalité.");
			break;
			
		case 5:
			alert("Vous devez enregistrer pour au moins un newsletter.");
			break;	
			
		default:
			if (document.insertContact)	document.insertContact.submit();
			oForm.submit();
		
	}
}



function calculate(){
 	
	var age = parseFloat(document.calculation.age_mf.value);
	var price = parseFloat(document.calculation.price_mf.value);
	var needFunds = 0;
	
	if (age == 0 || isNaN(age)) age = 1;
	
	if (age != 0 && price != 0) {
	
		if (age >= 65) {
			needFunds = roundDown1000(price * 0.34);
		}
		else {
			needFunds = roundDown1000(price * 0.2);
		} 
	}
	
	document.calculation.needFunds.value = needFunds;
}

/**
 * rounds the given value down to the nearest 1000
 */
function roundDown1000(value) {
	return Math.round(value / 1000 - 0.5) * 1000;
}


function isNumeric(x) {
	var numbers=".0123456789";  
    
    if(x.length>1) {
      // remove negative sign
      x=Math.abs(x)+"";
      for(j=0;j<x.length;j++) {        
        number=isNumeric(x.substring(j,j+1));
        if(!number) return number;
      }
      return number;
    }
    else {      
      if(numbers.indexOf(x)>=0) return true;
      return false;
    }
}


function isAllowedPhone(phoneString) {
	var numbers=" +0123456789";  
	
	if(phoneString.length > 1) {
		for(j=0; j < phoneString.length; j++) {        
			if(!isAllowedPhone(phoneString.substring(j,j+1))) return false;
		}
	}
	else {      
		return numbers.indexOf(phoneString) >= 0;
	}
	
	return true;
}


function replaceScriptingTags(oForm) {
	
	for (i = 0; i < oForm.elements.length; i++) {
		
		oElement = oForm.elements[i];
		
		if (oElement.name != "pwd" && oElement.value) {
			sText = oElement.value;
			sText = sText.replace(/#/g, "#");
			sText = sText.replace(/&/g, "&");
			sText = sText.replace(/</g, "&lt;");
			sText = sText.replace(/>/g, "&gt;");
			sText = sText.replace(/\(/g, "(");
			sText = sText.replace(/\)/g, ")");
			sText = sText.replace(/"/g, "&quot;");
			oElement.value = sText;
		}
	}
}


function checkEmail(emailAddress) {
	
	var re = /^(([^<>()[\]\\.,;:@\"]+(\.[^<>()[\]\\.,;:@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return re.test(emailAddress);
}

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function loadRolotecData(sTargetUrl, sContainerName) {

	var oAjax = new Ajax.Request(
			"/fr/" + sTargetUrl,
			{
				method:		'get', 
				requestHeaders: ["Timeout", "10", "Max-Forwards", "0"],
				onSuccess:	function(transport) {
						if (transport.responseText == "0") {
							loadErrorBox(sContainerName);
						} else {
							$(sContainerName).innerHTML = transport.responseText;
						}
					},
				onTimeout:	function(transport) {
						loadErrorBox(sContainerName);
					},
				onFailure:	function(transport) {
						loadErrorBox(sContainerName);
					}
			}
	);

}

function loadErrorBox(sContainerName) {

	var oAjax = new Ajax.Request(
			"/fr/error-rolotec",
			{
				method:		'get', 
				onSuccess:	function(transport) {
						$(sContainerName).innerHTML = transport.responseText;
					},
				onFailure:	function(transport) {
						$(sContainerName).innerHTML = "ERROR";
					}
			}
	);

}

function showHideBranch(iShow) {
	var bShowBranch = false;
	if (iShow == 1) {
		bShowBranch = true;
	}
	
	var oRow = document.getElementById("tr_branch");
	
	if (oRow) {
		if (bShowBranch) {
			oRow.style.display = '';
		}
		else {
			oRow.style.display = 'none';
		}
	}
}


function checkRadio(element) {
	var bReturn = false;
	for (var i = 0; i < element.length; ++i) {
		if (element[i].checked == true) bReturn = true;
	}
	return bReturn;
}

function replaceHoverPicture(element) {
	if (typeof element != "undefined") {
		var src = element.src;
		var rel = "";

		rel = element.getAttribute("rel");
		
		if (rel) {
			element.src = rel;
			element.setAttribute("rel", src);
		}
	}
}
