var submited = 0;

function random() {

	var randomInt;
	randomInt = parseInt(1+(Math.random()*10));
	
	if (document.teaser_main.src) {
		document.teaser_main.src="header"+randomInt+"_home_img.jpg";
	}

}

function del_confirm(msg, url) {

  if (confirm("Soll " + msg + " wirklich gel&#246;scht werden?")) {
  	location.href = url;
  }
}

function login() {
	if (submited == 0) {
		submited = 1;
		document.login_form.submit();
	}
	else {
		alert('You are logged in...');
		window.location.href='';
	}
}

function winOpen(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 win_open(href, width, height, sPopup)
{
	self.name = "BASE";
	var popName;
	
	if (!sPopup) {
		popName = 'POP';
	} 
	else {
		popName = sPopup;	
	}
		
	if ((width == '') &&  (height == ''))  
	{
		var pop = window.open(href, sPopup, 'toolbar=no,location=no,status=no,menubar=no,personalbar=no,scrollbars=yes,resizable=yes,screenx=50,left=50,screenY=50,top=50');
	} 
	else
	{
		var pop = window.open(href, sPopup, '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')
	}
	return pop.name;

}


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 showHide(oElement, sValueDE, sValueFR, sValueEN, sTarget, bReverse) {
	var target = document.getElementById(sTarget);
	if (!bReverse) {
		if(oElement.value == sValueDE || oElement.value == sValueFR || oElement.value == sValueEN) {
			target.style.display = '';
		} else {
			target.style.display = 'none';
		}
	} else {
		if(oElement.value != sValueDE && oElement.value != sValueFR && oElement.value != sValueEN) {
			if (oElement.value == "") {
				target.style.display = 'none';
			} else {
				target.style.display = '';
			}
		} else {
			target.style.display = 'none';
		}

	}
}

function checkBestReachable(oRadioButton, oForm) {
	var oElement;
	switch(oRadioButton.value) {
		case "1":
			oElement = oForm.contact_phone_private_mf;
			break;
		case "2":
			oElement = oForm.contact_phone_geschaeft;
			break;
		case "3":
			oElement = oForm.contact_phone_mobil;
			break;
		case "4":
			oElement = oForm.contact_fax;
			break;
	}
	if (oElement) {
		if (oElement.value == "") {
			alert("You have not indicated any phone number for this.");
			oElement.focus();
			oRadioButton.checked = false;
		}
	}
}

function checkAufenthaltsbewilligung(oForm) {
	var trAb = document.getElementById('trAufenthaltsbewilligung');
	var oCountry = oForm.contact_country_mf;
	var oNationality = oForm.contact_nationality_mf;
	
	if (oNationality.value == "958" || oNationality.value == "959" || oNationality.value == "961" || oNationality.value == "") {
		trAb.style.display = 'none';
	} else {
		if (oCountry.value == "958" || oCountry.value == "959" || oCountry.value == "961") {
			trAb.style.display = '';
		} else {
			trAb.style.display = 'none';
		}
	}
}

function doNationalityTasks(oNationality, oForm) {
	showHide(oNationality, "958", "959", "961", "trHomeCity", false);
	checkPolitAmt(oForm);
	checkAufenthaltsbewilligung(oForm);
}

function doCountryTasks(oCountry, oForm) {
	checkPolitAmt(oForm);
	checkAufenthaltsbewilligung(oForm);
}

function checkPolitAmt(oForm) {
	var oCountry = oForm.contact_country_mf;
	var oNationality = oForm.contact_nationality_mf;
	var oPolitAmtTr = document.getElementById("trPolitAmt");
	
	if ((oCountry.value == "958" || oCountry.value == "959" || oCountry.value == "961") && (oNationality.value == "958" || oNationality.value == "959" || oNationality.value == "961")) {
		oPolitAmtTr.style.display = 'none';
	} else {
		oPolitAmtTr.style.display = '';
	}
}

function CmfMoneyNetKonto(oForm) {
	var i, j, sElementName, sElementType, iCheck;
	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;
								oElement.focus();
								oElement.select();
							}
							break;
							
						case "checkbox":
						
							if (oElement.checked == false) {
								bError = 1;
								oElement.focus();
							}
							break;
							
						case "select-one":
						
							if (oElement[oElement.selectedIndex].value == "" || oElement[oElement.selectedIndex].value == "null") {
								bError = 1;
								oElement.focus();
							}
							break;
							
						case "select-multiple":

							iCheck = 0;

							for (j = 0; j < oElement.options.length; j++) {
								if (oElement.options[j].selected) {
									iCheck++;
								}
							}

							if (iCheck == 0) {
								bError = 1;
								oElement.focus();
							}
							break;
						
						case "radio":				
							iCheck = 0;
							var aRadioElements = document.getElementsByName(sElementName);
							var oRadioElement;

							for (j = 0; j < aRadioElements.length; j++) {
								oRadioElement = aRadioElements[j];
								if (oRadioElement.checked) {
									iCheck++;
								}
							}
							if (iCheck == 0) {
								bError = 1;
								oElement.focus();
							}
							break;
					}
				} else if (sElementName == "contact_politamt") {
					if ((oForm.contact_nationality_mf.value != "958" && oForm.contact_nationality_mf.value != "959" && oForm.contact_nationality_mf.value != "961") || (oForm.contact_country_mf.value != "958" && oForm.contact_country_mf.value != "959" && oForm.contact_country_mf.value != "961")) {
						iCheck = 0;
						var aRadioElements = document.getElementsByName(sElementName);
						var oRadioElement;
							for (j = 0; j < aRadioElements.length; j++) {
							oRadioElement = aRadioElements[j];
							if (oRadioElement.checked) {
								iCheck++;
							}
						}
						if (iCheck == 0) {
							bError = 1;
							oElement.focus();
						}
							
					}
				} else if (sElementName == "contact_aufenthaltsbewilligung") {
					if ((oForm.contact_nationality_mf.value != "958" && oForm.contact_nationality_mf.value != "959" && oForm.contact_nationality_mf.value != "961") && (oForm.contact_country_mf.value == "958" || oForm.contact_country_mf.value == "959" || oForm.contact_country_mf.value == "961")) {
						iCheck = 0;
						var aRadioElements = document.getElementsByName(sElementName);
						var oRadioElement;
							for (j = 0; j < aRadioElements.length; j++) {
							oRadioElement = aRadioElements[j];
							if (oRadioElement.checked) {
								iCheck++;
							}
						}
						if (iCheck == 0) {
							bError = 1;
							oElement.focus();
							break;
						}		
					}
				} else if (sElementName == "contact_homecity") {
					if (oForm.contact_nationality_mf.value == "958" || oForm.contact_nationality_mf.value == "959" || oForm.contact_nationality_mf.value == "961") {
						if (oElement.value == "") {
							bError = 1;
							oElement.focus();
							oElement.select();
							break;
						}
					}
				}

				//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;
						}
					}	
				}	
				
				//email check
				if (oElement.getAttribute('validate')) {
					if (oElement.getAttribute('validate').indexOf("email") != -1) {
						if(checkEmail(oElement.value) == false){
							bError = 6;
							oElement.focus();
							oElement.select();
							break;
						}
					}
				}	
				
				if (sElementName == "contact_sonstige_vermeogensherkunft" && (oForm.contact_vermoegensherkunft_mf.value == "1" || oForm.contact_vermoegensherkunft_mf.value == "6" || oForm.contact_vermoegensherkunft_mf.value == "11")) {
					if (oElement.value == "") {
						bError = 1;
						oElement.focus();
						oElement.select();
						break;
					}
				}								
			}
		}
		if (bError > 0) {
			break;
		}
	}

	
	// If every field is correctly validated	
	if (bError == 0) {

		if (oForm.contact_country_mf && oForm.contact_nationality_mf) {
			var aDepots = document.getElementsByName("depot");
			var bDepot = false;
			var bReturn = true;
			for (var iDepotCount = 0; iDepotCount < aDepots.length; iDepotCount++) {
				if (aDepots[iDepotCount].value == "150500") {
					bDepot = true;
				}
			}
		
			var aCountryIds = new Array(75,25,26,894,895,896,938,940,941,785,786,788,901,903,904,629,630,816,966,969,967,962,963,965,822,823,824,829,830,832,825,826,828,931,932,933,950,951,892,631,632,948,372,374,373,914,915,916,868,870,869,917,919,920,910,912,909,958,959,961,779,780,777,921,980,978,871,872,873);
			var aNotAllowedDoimicilIds = new Array(372,373,374,375,25,26,74,75,785,786,787,788,921,978,979,980);
				
			var iSelectedCountryId = oForm.contact_country_mf.value;
			var iSelectedNationality = oForm.contact_nationality_mf.value;
			
			
			
			//usa is never ok
			if (iSelectedCountryId == "781" || iSelectedCountryId == "782" || iSelectedCountryId == "783" || iSelectedNationality == "781" || iSelectedNationality == "782" || iSelectedNationality == "783") {
				bReturn = false;
			}
				
				
			if (bDepot && bReturn) {			
				//domizil Kanada, GB, Australien, Neuseeland is not ok
				for(var j=0; j < aNotAllowedDoimicilIds.length; j++){
					if (aNotAllowedDoimicilIds[j]==iSelectedCountryId) {
						bReturn = false;
					}
				}				
			}

			
			if (bReturn) {
				// wohnsitz schweiz immer ok
				if (iSelectedCountryId == "958" || iSelectedCountryId == "959" || iSelectedCountryId == "961") {
					bReturn = true;
				}
				else {
					bReturn = false;
				}
				
				if (!bReturn) {
					var bCountryOk = false;
					var bNationalityOk = false;
					
					for(var i=0; i<aCountryIds.length; i++){
						if(aCountryIds[i]==iSelectedCountryId){
							bCountryOk = true;
						}
						if(aCountryIds[i]==iSelectedNationality){
							bNationalityOk = true;
						}
					}
					
					bReturn = bCountryOk && bNationalityOk;
				}
			}

			if (iSelectedCountryId == "" || iSelectedNationality == "") bReturn = true;						
			if (!bReturn) bError = 4;
		}
		
		if (oForm.contact_dob_day_mf && oForm.contact_dob_month_mf && oForm.contact_dob_year_mf) {
			
			var iDay = oForm.contact_dob_day_mf.value;
			var iMonth = oForm.contact_dob_month_mf.value;
			var iYear = oForm.contact_dob_year_mf.value;
			iMonth--;
			
			var dDate = new Date(iYear, iMonth, iDay);
			if (dDate.getDate()==iDay) {
				var dNow = new Date();
				if (dNow < dDate) {
					bError = 7;
				}
			} else {
				bError = 8;
			}
		}
	}
	
	
	switch (bError) {
		case 0:
			if (document.insertContact)	document.insertContact.submit();
			if (bProduct) {
				oForm.submit();
			} else {
				alert("You have not ordered any products.");
			}
			break;
			
		case 1:
			alert("Please fill out all the required fields!");
			break;
			
		case 2:
			alert("Only numeric allowed!");
			break;
			
		case 3:
			alert("")
			break;
			
		case 4:
			alert("Thank you for your enquiry and the interest you have expressed in our services. Regretfully we must inform you that, on the basis of your place of domicile or your nationality, we are not permitted to offer you any facility for a business relationship.");
			break;
		
		case 5:
			alert("You must register for at least one newsletter.");
			break;
			
		case 6:
			alert("Please enter a valid e-mail adress.");
			break;
		case 7:
			alert("Please insert a valid birthdate.");
			break;
		case 8:
			alert("Please insert a valid date.");
			break;
			
		default:
			if (document.insertContact)	document.insertContact.submit();
			oForm.submit();
	}


}

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;
							
					}
				}

				//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;
						}
					}	
				}	
				
				//check email for Newsletter
				if (sElementName == "email" && (oForm.newsletter_anmeldung || oForm.newsletter_abmeldung)) {
					if(oElement.value != "" && checkEmail(oElement.value) == false){
						bError = 6;
					}
					if (oElement.value == "") bError = 1;
				}
												
			}
		}
	}
	
	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.checked == false && oForm.money_bonus.checked == false && oForm.money.checked == false && oForm.trade.checked == false) bError = 5;
		
		if (document.insertContact) {
			if (oForm.contact_naming.value == "m") document.insertContact.contact_naming.value = "Herr";
			if (oForm.contact_naming.value == "f") document.insertContact.contact_naming.value = "Frau";
			document.insertContact.contact_firstname_mf.value = oForm.contact_firstname_mf.value;
			document.insertContact.contact_lastname_mf.value = oForm.contact_lastname_mf.value;
			document.insertContact.contact_email_mf.value = oForm.email.value;
			document.insertContact.contact_sprache.value = oForm.contact_sprache.value;
		}
		
		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;
	}
	
	
	switch (bError) {
		case 0:
			var iSaveData = 1;
			var oRadio = oForm.contact_savedata;
			if (oRadio) {
				for(var i=0;oRadio[i];i++) {
	    			if(oRadio[i].checked) {
						iSaveData = oRadio[i].value;
						break;
					}
				}
			}
			if (document.insertContact && iSaveData == "1")	document.insertContact.submit();
			oForm.submit();
			
			break;
			
		case 1:
			alert("Please fill out all the required fields!");
			break;
			
		case 2:
			alert("Only numeric allowed!");
			break;
			
		case 3:
			alert("")
			break;
			
		case 4:
			alert("Thank you for your enquiry and the interest you have expressed in our services. Regretfully we must inform you that, on the basis of your place of domicile or your nationality, we are not permitted to offer you any facility for a business relationship.");
			break;
		
		case 5:
			alert("You must register for at least one newsletter.");
			break;
			
		case 6:
			alert("Please enter a valid e-mail adress.");
			break;
			
		default:
			var iSaveData = 1;
			var oRadio = oForm.contact_savedata;
			if (oRadio) {
				for(var i=0;oRadio[i];i++) {
	    			if(oRadio[i].checked) {
						iSaveData = oRadio[i].value;
						break;
					}
				}
			}
			if (document.insertContact && iSaveData == "1")	document.insertContact.submit();
			oForm.submit();
			
			break;
	}

}



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 > 59) {
			needFunds = price / 100 * 34;
		}
		else {
			needFunds = price * 0.2;
		} 
	}
	
	document.calculation.needFunds.value = needFunds;
}

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;
}

Number.prototype.toDecimals=function(n){
    n=(isNaN(n)) ? 2 : n;
    var nT=Math.pow(10,n);
	
	if (isNaN(this)){
		return this;
	}
	else{
		var s = new String(Math.round(this*nT)/nT);
		if (s.lastIndexOf('.') == -1) s = s + '.';
		
		while(s.length - n - 1 < s.lastIndexOf('.')) s = s + '0';
		
		return s;
	}
}


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 oNav = document.getElementById("nav");
	if (oNav) {
			
		var sfEls = oNav.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"), "");
			}
		}
	}
}



