function printForm() {
   if (navigator.appName.indexOf("Microsoft") > -1 && navigator.appVersion.indexOf("5.") == -1) {
       // IE4
       OLECMDID_PRINT = 6;
       OLECMDEXECOPT_DONTPROMPTUSER = 2;
       OLECMDEXECOPT_PROMPTUSER = 1;
       WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 WEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
       document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
       WebBrowser1.ExecWB(OLECMDID_PRINT,   OLECMDEXECOPT_PROMPTUSER);
       WebBrowser1.outerHTML = "";
      }
    else {
      // N4 IE5
      window.print();
      }
}

function getSelectedRadio(buttonGroup) {
	   if (buttonGroup[0]!=undefined) {
	      for (var i=0; i<buttonGroup.length; i++) {
	         if (buttonGroup[i].checked) {
	            return buttonGroup[i].value;
	         }
	      }
	   } else {
	      if (buttonGroup.checked) {return buttonGroup.value; }
	   }
	   return "";
}

function FormatNumber(Number,Decimals,Separator)
	{
	 Number += ""          // Force argument to string.
	 Decimals += ""        // Force argument to string.
	 Separator += ""       // Force argument to string.
	 if((Separator == "") || (Separator.length > 1))
	  Separator = "."
	 if(Number.length == 0)
	  Number = "0"
	 var OriginalNumber = Number  // Save for number too large.
	 var Sign = 1
	 var Pad = ""
	 var Count = 0
	 // If no number passed, force number to 0.
	 if(parseFloat(Number)){
	  Number = parseFloat(Number)} else {
	  Number = 0}
	 // If no decimals passed, default decimals to 2.
	 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
	  Decimals = parseInt(Decimals,10)} else {
	  Decimals = 2}
	 if(Number < 0)
	 {
	  Sign = -1         // Remember sign of Number.
	  Number *= Sign    // Force absolute value of Number.
	 }
	 if(Decimals < 0)
	  Decimals *= -1    // Force absolute value of Decimals.
	 // Next, convert number to rounded integer and force to string value.
	 // (Number contains 1 extra digit used to force rounding)
	 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
	 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
	  return(OriginalNumber) // Number too large to format as specified.
	 // If length of Number is less than number of decimals requested +1,
	 // pad with zeros to requested length.
	 if(Number.length < Decimals +1) // Construct pad string.
	 {
	  for(Count = Number.length; Count <= Decimals; Count++)
	   Pad += "0"
	 }
	 Number = Pad + Number // Pad number as needed.
	 if(Decimals == 0){
	  // Drop extra digit -- Decimal portion is formatted.
	  Number = Number.substring(0, Number.length -1)} else {
	  // Or, format number with decimal point and drop extra decimal digit.
	 Number = Number.substring(0,Number.length - Decimals -1) +
	          Separator +
	          Number.substring(Number.length - Decimals -1,
	          Number.length -1)}
	 if((Number == "") || (parseFloat(Number) < 1))
	  Number="0"+Number // Force leading 0 for |Number| less than 1.
	 if(Sign == -1)
	  Number = "-" + Number  // Set sign of number.
	 return(Number)
}
 function isInteger (s)

    {
        var i;

        if (isEmpty(s))
             if (isInteger.arguments.length == 1) return 0;
             else return (isInteger.arguments[1] == true);

         for (i = 0; i < s.length; i++)
         {
              var c = s.charAt(i);

              if (!isDigit(c)) return false;
         }

         return true;
    }

    function isEmpty(s)
    {
        return ((s == null) || (s.length == 0))
    }

    function isDigit (c)
    {
        return ((c >= "0") && (c <= "9"))
    }  

function emailvalidation(entered,f)
		{
			apos=entered.value.indexOf("@"); 
			//dotpos=entered.value.lastIndexOf(".");
			//lastpos=entered.value.length-1;
			//if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
			if (apos<1) 
			{
				return false;
			}
			else
			{
				return true;
			}
}
function checkrb(rb){
	if(rb==1){
		if(document.formRegistration.extravalue1.value=="" || isInteger(document.formRegistration.extravalue1.value)==false){
			document.formRegistration.extraref1.checked=false;	
			document.formRegistration.extravalue1.value="";
		}
		else{
			document.formRegistration.extraref1.checked=true;	
		}
	}
	if(rb==2){
		if(document.formRegistration.extravalue2.value=="" || isInteger(document.formRegistration.extravalue2.value)==false){
			document.formRegistration.extraref2.checked=false;
			document.formRegistration.extravalue2.value="";
		}
		else{
			document.formRegistration.extraref2.checked=true;
		}
		
	}
	if(rb==3){
		if(document.formRegistration.txtNoites.value==""){
			document.formRegistration.chkOutros.checked=false;
		}
		else{
			document.formRegistration.chkOutros.checked=true;
		}
	}	
	if(rb==4){
		document.formRegistration.pay.checked=true;
	}	
}

function showTotal(){
	var tempMontante = 0;
	tempMontante = parseFloat(FormatNumber(document.formRegistration.montante.value.replace(',','.')));
	

	if(document.formRegistration.extraref1.checked == true){
		if(isNaN(document.formRegistration.extravalue1.value.replace(",","."))){
			document.formRegistration.extravalue1.value = "";
		}
		else{
			tempMontante = tempMontante + parseFloat(FormatNumber(parseInt(document.formRegistration.extravalue1.value.replace(',','.'))*parseFloat(document.formRegistration.extramontante1.value.replace(',','.')),2,"."));
		}
	} 
	if(document.formRegistration.extraref2.checked == true){
		if(isNaN(document.formRegistration.extravalue2.value.replace(",","."))){
			document.formRegistration.extravalue2.value = "";
		}
		else{
			tempMontante = tempMontante + parseFloat(FormatNumber(parseInt(document.formRegistration.extravalue2.value.replace(',','.'))*parseFloat(document.formRegistration.extramontante2.value.replace(',','.')),2,"."));
		}	
	}
	if(document.formRegistration.txtTotal1){
		document.formRegistration.txtTotal1.value=FormatNumber(tempMontante,2,".").replace(".",",");
	}
	if(document.formRegistration.txtTotal2){
		document.formRegistration.txtTotal2.value=FormatNumber(tempMontante,2,".").replace(".",",");
	}
	if(document.formRegistration.txtTotal3){
		document.formRegistration.txtTotal3.value=FormatNumber(tempMontante,2,".").replace(".",",");
	}
}

function submitForm(){	
	
	//verifica preenchimento obrigatorio
	var erroForm=0;
	
	if(document.formRegistration.firstname.value=="" && erroForm==0){
		alert("Fill Name please");
		erroForm=1;
		document.formRegistration.firstname.focus();
	}
	if(document.formRegistration.lastname.value=="" && erroForm==0){
		alert("Fill Last Name please");
		erroForm=1;
		document.formRegistration.lastname.focus();
	}
	if((document.formRegistration.radiosexo[0].checked==false) && (document.formRegistration.radiosexo[1].checked==false) && (erroForm==0)) {
		alert("Select Sex please");
		erroForm=1;
		document.formRegistration.lastname.focus();
	}
	if(document.formRegistration.mailingaddress.value=="" && erroForm==0){
		alert("Fill Address please");
		erroForm=1;
		document.formRegistration.mailingaddress.focus();
	}
	if(document.formRegistration.country.value=="" && erroForm==0){
		alert("Fill Country please");
		erroForm=1;
		document.formRegistration.country.focus();
	}	
	if(!emailvalidation(document.formRegistration.email1,document.formRegistration) && erroForm==0){
		alert("Email is not valid");
		erroForm=1;
		document.formRegistration.email1.focus();
	}
	if(document.formRegistration.entityname.value=="" && erroForm==0){
		alert("Fill Entity Name please");
		erroForm=1;
		document.formRegistration.entityname.focus();
	}	
	if(document.formRegistration.entityaddress.value=="" && erroForm==0){
		alert("Fill Entity Address please");
		erroForm=1;
		document.formRegistration.entityaddress.focus();
	}	
	if(document.formRegistration.entitycountry.value=="" && erroForm==0){
		alert("Fill Entity Country please");
		erroForm=1;
		document.formRegistration.entitycountry.focus();
	}		
	if(!emailvalidation(document.formRegistration.entityemail,document.formRegistration) && erroForm==0){
		alert("Entity Email is not valid");
		erroForm=1;
		document.formRegistration.entityemail.focus();
	}	
	if(document.formRegistration.extraref1.checked==true && document.formRegistration.extravalue1.value=="" && erroForm==0){
		alert("Please indicate the number of people attending the Welcoming Dinner");
		erroForm=1;
		document.formRegistration.extravalue1.focus();
	}
	if(document.formRegistration.extraref2.checked==true && document.formRegistration.extravalue2.value=="" && erroForm==0){
		alert("Please indicate the number of people attending the Social Dinner");
		erroForm=1;
		document.formRegistration.extravalue2.focus();
	}	
    if(getSelectedRadio(document.formRegistration.pay)=="" && erroForm==0){
		alert("Please choose Payment Method");
		erroForm=1;
    }

    if(getSelectedRadio(document.formRegistration.pay)=="3" && erroForm==0){
		if(document.formRegistration.creditcardnumber.value=="" && erroForm==0){
			alert("Fill Credit Card Number please");
			erroForm=1;
			document.formRegistration.creditcardnumber.focus();
		}
		if(document.formRegistration.creditcarddatemonth.value=="" && erroForm==0){
			alert("Fill Credit Card Number Expire Date please");
			erroForm=1;
			document.formRegistration.creditcarddatemonth.focus();
		}
		if(document.formRegistration.creditcarddateyear.value=="" && erroForm==0){
			alert("Fill Credit Card Number Expire Date please");
			erroForm=1;
			document.formRegistration.creditcarddateyear.focus();
		}
    }

	if(erroForm==0){
			document.formRegistration.extramontante3.value="";
			document.formRegistration.extravalue3.value="";
			document.formRegistration.extraref3.value="Conferência/Conference:";
			
			if(document.formRegistration.chkatt27.checked == true){
				document.formRegistration.extravalue3.value = document.formRegistration.extravalue3.value + " \n" + document.formRegistration.chkatt27.value
			}
			if(document.formRegistration.chkatt28.checked == true){
				document.formRegistration.extravalue3.value = document.formRegistration.extravalue3.value + " \n" + document.formRegistration.chkatt28.value
			}
			if(document.formRegistration.chkatt29.checked == true){
				document.formRegistration.extravalue3.value = document.formRegistration.extravalue3.value + " \n" + document.formRegistration.chkatt29.value
			}
		
	}	
	if(erroForm==0){
		return true;
	}
	else{
		return false;
	}

}
