var componentIdList = new Array();
var dlg = "";

$(document).ready(function() {

		$("#nextbtn2").click(function(){
			gostep3('');
		});

		$.ajaxSetup({
					// Disable caching of AJAX responses */
    	cache: false,
		error:function(x,e){
			if(x.status==0){
			alert('You are offline!!\n Please Check Your Network.');
			}else if(x.status==404){
			alert('Requested URL not found.');
			}else if(x.status==500){
			alert('Internal Server Error.');
			}else if(e=='parsererror'){
			alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
			alert('Request Time out.');
			}else {
			alert('Unknow Error.\n'+x.responseText);
			}
			$.unblockUI();
		}
	});

});

function customalert(msg)
{
	$.blockUI({ message: $('#question'), css: { width: '275px' } }); 
}


function cpfvalidation(cpf) {

    var notvalidcpf = "x00000000000,x11111111111,x22222222222,x33333333333,x44444444444," +
                        "x55555555555,x66666666666,x77777777777,x88888888888,x99999999999";
    if (notvalidcpf.indexOf(cpf) != -1) return false;

	var v1 = 10 * parseInt(cpf.charAt(1),10) + 9 *  parseInt(cpf.charAt(2),10) + 8 * parseInt(cpf.charAt(3),10);
	v1 +=  7 * parseInt(cpf.charAt(4),10) + 6 * parseInt(cpf.charAt(5),10) + 5 * parseInt(cpf.charAt(6),10);
	v1 +=  4 * parseInt(cpf.charAt(7),10) + 3 * parseInt(cpf.charAt(8),10) + 2 * parseInt(cpf.charAt(9),10);
	v1 = 11 - (v1 % 11);
	v1 = (v1 >= 10)?0:v1;
	
	
	//Note: compute 2nd verification digit.
	var v2 = 11 * parseInt(cpf.charAt(1),10) + 10 * parseInt(cpf.charAt(2),10) + 9 * parseInt(cpf.charAt(3,10));
	v2 +=  8 * parseInt(cpf.charAt(4),10) +  7 * parseInt(cpf.charAt(5),10) + 6 * parseInt(cpf.charAt(6),10);
	v2 +=  5 * parseInt(cpf.charAt(7),10) +  4 * parseInt(cpf.charAt(8),10) + 3 * parseInt(cpf.charAt(9),10);
	v2 +=  2 * parseInt(v1,10);
	v2 = 11 - (v2 % 11);
	v2 = (v2 >= 10)?0:v2;
	return ((v1 ==  cpf.charAt(10)) && (v2 ==  cpf.charAt(11)))
}

function _block2UI()
{
	$.blockUI({  
			 message : '<h2><img src="/images/loader.gif" align="absmiddle"/> Loading...</h2>',
			 css: { 
			 border: 'none', 
            padding: '15px', 
            backgroundColor: '#000035', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .75, 
			color: '#FFFFFF'
        	} ,
			overlayCSS:  { 
        		backgroundColor: '#000000', 
        		opacity:         0.2
    		}
			}); 
}


function _blockUI()
{
	$.blockUI({  
			 message : '<h2><img src="/images/loader.gif" align="absmiddle"/> Loading...</h2>',
			 css: { 
			 border: 'none', 
            padding: '15px', 
            backgroundColor: '#000035', 
            '-webkit-border-radius': '10px', 
            '-moz-border-radius': '10px', 
            opacity: .95, 
			color: '#FFFFFF'
        	} ,
			overlayCSS:  { 
        		backgroundColor: '#000000', 
        		opacity:         0.8
    		}
			}); 
}

function gostep2back()
{
			_blockUI();
			try
			{
				if (mapstep2['anno'] != undefined)
				{
					
					mapstep3 = {"cruise":"",
					   "cruiseonly":"",
					   "paxbar":"",				
					   "themecd":""};
					  // console.log("2" + mapstep3["cruise"]);
				        $.get("/booking_step2.asp", 
						{	anno        :   mapstep2['anno'],
							mese        :   mapstep2['mese'],
							giorno      :   mapstep2['giorno'],
							nadult      :   mapstep2['adults'],
							children    :   mapstep2['children'] ,
							childrenAge :   mapstep2['childrenAge'],
							ship        :   mapstep2['ship'] ,
							region      :   mapstep2['region'],
							portofcall  :   mapstep2['portofcall'],
							paxtype     :   mapstep2['paxtype'],
							cruiseid    :   mapstep2['cruiseid'],
							discpos     :   mapstep2['discpos'],
							cardtypeval :   mapstep2['cardtypeval'],
							isdiscount  :   mapstep2['isdiscount'],
							itemcatglist   :   mapstep3['itemcatglist']},
					   function(data){	
									
							  $("#prova").html(data);
								//$.unblockUI();
					   });
				}
				else
				{
					$.post("/booking_step2.asp",
						{
						    themecruise: 'y'	
						},
					    function(data){	
									
							  $("#prova").html(data);
								//$.unblockUI();
					   });
				}
			}
			catch(ex)
			{
				location.href = "cruiseCalendar.asp";
			}
			
}


function gostep2backforAddcruise()
{
	_blockUI();
	if (mapstep2['anno'] != undefined)
	{
	    $.get("/booking_step2forAddcruise.asp", 
			{	anno:mapstep2['anno'],
				mese:mapstep2['mese'],
				giorno:mapstep2['giorno'],
				adults:mapstep2['adults'],
				children:mapstep2['children'] ,
				childrenAge:mapstep2['childrenAge'],
				ship:mapstep2['ship'] ,
				region:mapstep2['region'],
				portofcall:mapstep2['portofcall'],
				paxtype:mapstep2['paxtype'],
				cruiseid:mapstep2['cruiseid']	},
		   function(data){	
		   				
		          $("#prova").html(data);
		      		$.unblockUI();
		   });
	}
	else
	{
		$.get("/booking_step2forAddcruise.asp", 
			{	themecruise: 'y'	},
		   function(data){	
		   				
		          $("#prova").html(data);
		      		$.unblockUI();
		   });
	}
			
}


function gostep2forAddcruise()
{
	/*_blockUI();
			$.post("../booking_step2forAddcruise.asp", 
				   $("#myForm").serialize(),
				   function(data){	
				           $("#prova").html(data);
				      		$.unblockUI();
				   });*/
	gostep2();
}

function gostep2()
{
			_blockUI();
			/*var paxbar = ""
			var paxstr = "";
		    var currPaxIdx = -1;
			var childredAges = [];
			if ($("#paxadded").val() != "")
			{
				for (var i = 0; i <= parseInt($("#paxadded").val()); i++)
				{
					if (i == 0)
					{
						for(var ipax = 0; ipax < parseInt($("#adults").val()); ipax++)
						{	
							currPaxIdx++;
							paxbar += "A,";
							paxstr += currPaxIdx + ",";
						}
						childredAges = $("#childrenAge").val().split(",");
						for(var ipax = 0; ipax < parseInt($("#children").val()); ipax++)
						{
							currPaxIdx++;
							paxbar += childredAges[ipax] + ","; //"C,";
							paxstr += currPaxIdx + ",";
						}
					}
					else
					{
						paxstr = paxstr.substring(0,paxstr.length - 1) + "@";
						for(var ipax = 0; ipax < parseInt($("#adults"+i).val()); ipax++)
						{
							currPaxIdx++;
							paxbar += "A,";
							paxstr += currPaxIdx + ",";
						}
						childredAges = $("#childrenAge"+i).val().split(",");
						for(var ipax = 0; ipax < parseInt($("#children"+i).val()); ipax++)
						{
							currPaxIdx++;
							//paxbar += "C,";
							paxbar += childredAges[ipax] + ","; //"C,";
							paxstr += currPaxIdx +",";
						}
						paxstr = paxstr.substring(0,paxstr.length - 1) + "@";
					}
				}
				paxbar = paxbar.substring(0,paxbar.length - 1);
				paxstr = paxstr.substring(0,paxstr.length - 1);
				paxbar += ":" + paxstr;
			}
			
			//alert("pb: " + paxbar);
			$("#paxbar").val(paxbar);*/
			if ($("#porto").val() == "")
			   $("#portofcall").val("");
			$.post("/booking_step2.asp", 
				   $("#myForm").serialize(),
				   function(data){	
				           $("#prova").html(data);
				      		//$.unblockUI();
				   });
}

function gostep2_1()
{
			_blockUI();
			$.post("/booking_step2_1.asp", 
				   $("#myForm").serialize(),
				   function(data){	
				           $("#prova").html(data);
				      		$.unblockUI();
				   });
}


function gostep3(cruiseid)
	{
		_blockUI();
		$(".cruiserow").die('click');
		$("#backbtn").die('click');
		$("#fakebtn").die('click');
		$("#nextbtnstep2").die('click');
		
		$(".cabinrow").die('click');
		$(".cabinrowphycha").die('click');
		$(".cabinrow").die('mouseout');
		$(".cabinrowphycha").die('mouseout');
		$(".cabinrow").die('mouseover');
		$(".cabinrowphycha").die('mouseover');
		
		$(".categoryrow").die('click');
		$(".categoryrowYC").die("click");
		$(".contentitemcategory").die("mouseover");
		$(".contentitemcategory").die("mouseout");
		$("#tblcategory tr").die("each");
		$(".categoryrowdisabled").die("click");

	    if (mapstep3['cruise'] != undefined && mapstep3['cruise'] != '') {
 
			$.get("/booking_step3.asp",
			    {
			        ship        :   mapstep3['ship'],
		            cruise      :   mapstep3['cruise'],
	                cruiseonly  :   mapstep3['cruiseonly'],
	                themecd     :   mapstep3['themecd'],
	                paxbar      :   mapstep3['paxbar'],
	                discount    :   mapstep3['discount'],
					netprice    :   mapstep3['netprice'],
					priceid     :   mapstep3['priceid'],
					plancd      :   mapstep3['plancd'],
					demogarea   :   mapstep3['demogarea'],
					bingo   :   mapstep3['bingo'],
					resetsoci   :   mapstep3['resetsoci'],
					itemcatglist   :   mapstep3['itemcatglist']
	            },
	            function(data){	
				    $("#prova").html(data);
		        }
			);
		}
		else
		{
			if (cruiseid == "") cruiseid = $("#cruise").val();
				
			var cruiseonly = "";
			var themecd = "";

			if ($("#cruiseonly") != null) {
			    switch (off_cd) {
			        case "DEU":
			            cruiseonly = "-999";
			            break;
			        case "IRL":
			            cruiseonly = "1";
			        default:
			            cruiseonly = $("#cruiseonly").val();
			    }    
			}

			var themecd = "";
			
			if ($("#themecd") != null) themecd = $("#themecd").val();

			if (cruiseonly != "0" && cruiseonly != "1" && cruiseonly != "-999") cruiseonly = "0";
				
			if (cruiseid == "")
			{
				alert("An error has occured\n Please repeat you search");
				location.href = "/booking.asp";
            }

			$.get("/booking_step3.asp",
		       {
		            cruise: cruiseid,
		            cruiseonly: cruiseonly,
		            themecd: themecd,
		            paxbar:$("#txtHiddPaxbar").val(),
		            discount:$("#discount").val(),
								netprice:$("#netprice").val(),
								priceid:$("#priceid").val(),
								plancd:$("#plancd").val(),
								demogarea:$("#demogarea").val(),
								bingo   :$("#bingo").val(),
								resetsoci   :$("#resetsoci").val(),
								itemcatglist : $("#itemTypeCtgList").val()
					
					},
		            function(data){	
			            $("#prova").html(data);				      		
			        });
                }       
	}
	
	function gostep3forAddcruise(cruiseid)
	{
		_blockUI();
		if (cruiseid == "")
			cruiseid = $("#cruise").val();
		var cruiseonly = "";
		var themecd = "";
		if ($("#cruiseonly") != null)
	    cruiseonly = $("#cruiseonly").val();
		var themecd = "";
		if ($("#themecd") != null)
		themecd = $("#themecd").val();
		if (cruiseonly != "0" && cruiseonly != "1")
			cruiseonly = "1";
	    if (cruiseid == "")
		{
			alert("An error has occured\n Please repeat you search");
			location.href = "CRUISE.asp";
		}
		
		$.get("/booking_step3forAddcruise.asp", 
			   {cruise: cruiseid,
			   cruiseonly: cruiseonly,
			   themecd: themecd,
			   samepax: $("#samepax").val(),
			   paxbar: $("#paxbar").val()},
			   function(data){	
				           $("#prova").html(data);
				      		$.unblockUI();
				   });
				
	}

function gostep4(ctg)
	{
		_blockUI();
	    $.post("/booking_step4.asp", 
	       $("#myForm").serialize(),
	        function(data){	
               //alert(data);
               $("#prova").html(data);
      		    //$.unblockUI();
	    });

	}
	
	function gostep4forAddcruise(ctg)
	{
		_blockUI();
		//alert($("#compid").val());
			$.post("/booking_step4forAddcruise.asp", 
				   $("#myForm").serialize(),
				    function(data){	
				           //alert(data);
				           $("#prova").html(data);
				      		$.unblockUI();
				   });

	}

    function gostep5gar()
	{
		_blockUI();
		
		$.post("/booking_step4.asp", 
				   $("#myForm").serialize(),					
				   function(data){	
				   });
		
		$.post("/booking_step5_to_recap.asp", 
				   $("#myForm").serialize(),					
				   function(data){	
				   		   $("#prova").html(data);
				      		$.unblockUI();
				   });
	}

	function gostep5(cabinno,catgcode)
	{
		_blockUI();
		if (cabinno == 'G00000')
			$("#myForm").append($("<input type='hidden' name='CabinNo' id='CabinNo' value=''>"));
		$.post("/booking_step5_to_recap.asp", 
		   
	       $("#myForm").serialize(),					
	       function(data){	
	   		       $("#prova").html(data);
		           if (off_cd == "CHE") {
	               $("#step5_transferWayMode").val("R");
	               $("#step5_transferFrom").val("C");
	               $("#step5_transferTo").val("P");
	               $("#step5_transferFrom").attr('disabled', 'disabled');
	               $("#step5_transferTo").attr('disabled', 'disabled');
	           }
      	 	   // $.unblockUI();
	       });
	}
	
	function gostep5forAddcruise(cabinno,catgcode)
	{
		_blockUI();
		
		$.post("/booking_step5_to_recapforAddcruise.asp", 
				   $("#myForm").serialize(),					
				   function(data){	
				   		   $("#prova").html(data);
				      		$.unblockUI();
				   });
	}
	
	 function finalizebooking()
	{
			_blockUI();
			$("input[type='text']").each(function(i){
             if ($(this).attr("id").match("themecd"))
                 $(this).attr("readonly","");
				 $(this).attr("disabled","");
     		});
			$.post("/booking_step7_to_recap_2.asp", 
				   $("#myForm").serialize(),
				    function(data){	
					       //console.log(data);
					      if (data.substring(0,3) == "err")
						  {
							  var mylink = "booking.asp";
/*							  $("#prova").html(<input type='button' class='buttonPrevious' value='Indietro' onclick='javascript:location.href=booking.asp'></div>");*/
							  $("#prova").html("<div style=\"width:756px;height:300px\"><span class=\"testo\" style=\"position:relative;top:150px\"><h2>" + data.substring(3) + "</b></h2><br><br><input type=\"button\" class=\"buttonPrevious\" value=\"Indietro\" onclick=\"javascript:location.href=\'booking.asp'\">");
						  }
						  else
						  {
							  if ($("#tipo").val() == "H")
								  	window.open('/reqdocs.asp','','')
							  	 location.href = "/b2b/ricerca_pratiche_risultati_to_detail.asp?bookingnr=" + data;
						  }
				          $.unblockUI();
				   });
	}
	
	
	function finalizebookingforAddcruise()
	{
			_blockUI();
			$("input[type='text']").each(function(i){
             if ($(this).attr("id").match("themecd"))
                 $(this).attr("readonly","");
				 $(this).attr("disabled","");
     		});

     		$.post("addbooking4AddCruise.asp",
				   $("#myForm").serialize(),
				    function (data) {
				        // console.log(data);
				        //alert(data);
				        /*if (data.substring(0,3) == "err")
				        {
							  
				        var mylink = "../booking.asp";
				        //						  $("#prova").html(<input type='button' class='buttonPrevious' value='Indietro' onclick='javascript:location.href=booking.asp'></div>");
				        $("#prova").html("<div style=\"width:756px;height:300px\"><span class=\"testo\" style=\"position:relative;top:150px\"><h2>" + data.substring(3) + "</b></h2><br><br><input type=\"button\" class=\"buttonPrevious\" value=\"Indietro\" onclick=\"javascript:location.href=\'../booking.asp'\">");
				        }
				        else
				        {
				        location.href = "/b2b/ricerca_pratiche_risultati_to_detail.asp?bookingnr=" + data;
				        }*/
				        if (data.substring(0, 3) == "err") {
				            var mylink = "booking.asp";
				            /*							  $("#prova").html(<input type='button' class='buttonPrevious' value='Indietro' onclick='javascript:location.href=booking.asp'></div>");*/
				            $("#prova").html("<div style=\"width:756px;height:300px\"><span class=\"testo\" style=\"position:relative;top:150px\"><h2>" + data.substring(3) + "</b></h2><br><br><input type=\"button\" class=\"buttonPrevious\" value=\"Indietro\" onclick=\"javascript:location.href=\'../booking.asp'\">");
				        }
				        else {
				            if ($("#tipo").val() == "H")
				                window.open('/reqdocs.asp', '', '')
				            location.href = "/b2b/ricerca_pratiche_risultati_to_detail.asp?bookingnr=" + data;
				            //$("#prova").html(data);
				        }
				        $.unblockUI();
				    });
	}
	
	function gofwd(pi,panno,pmese,pgiorno,pnadult,pchildrenage,pship,pport,pnchildren,pregion,pthemecruise,paxtype,discpos,cardtypeval,isdiscount)
	{
		_blockUI();
 
		$.get("/booking_step2.asp",
            { 
                page: pi,
                anno: panno,
                mese: pmese,
                giorno: pgiorno,
                nadult: pnadult,
                childrenage:pchildrenage,
                ship:pship,
                portofcall:pport,
                nchildren:pnchildren,
                region:pregion,
                themecruise:pthemecruise,
                paxtype:paxtype,
                discpos:discpos,
                cardtypeval:cardtypeval,
                isdiscount:isdiscount
	        },
            function(data){		   
                $("#prova").html(data);
                //$.unblockUI();
            }
        );
 
	}

	function gofwdAddCruise(pi, panno, pmese, pgiorno, pnadult, pchildrenage, pship, pport, pnchildren, pregion, pthemecruise, paxtype, discpos, cardtypeval,isdiscount) {
	    _blockUI();


	    $.get("../booking_step2forAddcruise.asp",
	        { 
	            page: pi,
	            anno: panno,
	            mese: pmese,
	            giorno: pgiorno,
	            nadult: pnadult,
	            childrenage: pchildrenage,
	            ship: pship,
	            portofcall: pport,
	            nchildren: pnchildren,
	            region: pregion,
	            themecruise: pthemecruise,
	            paxtype: paxtype,
	            discpos: discpos,
	            cardtypeval: cardtypeval,
	            isdiscount: isdiscount
	    },
	   function (data) {
	       $("#prova").html(data);
	       $.unblockUI();
	   }
	  );
 
	}
 
	 function ceccisModal(url,title)
	 {
	 
		 dlg = viewModalnew(url, title,750,300);	
	 }
	 
	 
	 
	 function parseXml(xml) {  
     if (jQuery.browser.msie) {  
      var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
       xmlDoc.loadXML(xml);  
       xml = xmlDoc;  
     }  
     return xml;  
	}
	
	function xmldatatype(){

	dtype ="xml";
	if(jQuery.browser.msie) {
	    dtype = "text/xml";  
		   
 }  
 return dtype;  

}

function CurrencyFormatted(amount)
	{
		amount = "" + amount;	
		var dotidx = amount.indexOf(".");
		if (dotidx!=-1)
		{
			var dpart = amount.substring(dotidx+1,dotidx+3);
			var ipart = amount.substring(0,dotidx);
            amount = ipart + "," + dpart;
		}
		else
			amount += ",00";
		var delimiter = ","; // replace comma if desired
		var a = amount.split(',',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if(isNaN(i)) { return ''; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if(n.length > 0) { a.unshift(n); }
		n = a.join(delimiter);
		if(d.length < 1) { amount = n; }
		else { amount = n + '.' + d.substring(0,2); }
		amount = minus + amount;
		
		return amount;
	}

