/*
  - Author(s): Rajesh George
  - Date: 06/19/2008
  - Description: Inital version
*/

// Custom trim function for String objects

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

 
/*This function handles the pagination*/

function getNextPage(offset) { 

    document.SearchResultsCruiseForm.pageOffset.value = offset;
    document.SearchResultsCruiseForm.submit();
}

/*This function handles the sort by drop down box feature */

function getOrderBy(obj) {

    document.SearchResultsCruiseForm.orderBy.value = obj.options[obj.selectedIndex].value;
    document.SearchResultsCruiseForm.submit();
}
/*This function handles the ajax pricing feature */

function getPrice(obj){	
	
	var sectionId = obj.id.substring(0,obj.id.length-6);
	var voyageId = obj.options[obj.selectedIndex].value;	
	var ajxForm = eval(document.getElementById('ajaxForm'));
	ajxForm.voyageCode.value=voyageId;
	var params = new Array(sectionId,sectionId+'tbody',sectionId+'table');
    submitForm("getPrice.rdat", document.ajaxForm, "replaceChild", "err", params);
}

/*Call back function of ajax call in getPrice*/

function replaceChild(data,params) {
  
	var tableObject = document.getElementById(params[2]);
	var lenOfRows = document.getElementById(params[2]).rows.length;
	// Remove all rows except the first row  and last 2 rows
	for ( var i = 1; i < lenOfRows; i++ )  {
	  tableObject.deleteRow(1);
	}	
    //Get the span which will contain the table with the new rows
    var tempSpanForRowReplace = document.getElementById('tempSpanForRowReplace');
    // Create the new rows along with the table
    tempSpanForRowReplace.innerHTML = "<table id='"+params[2]+"'><tbody id='"+params[1]+"'>"+data;
   
    var browserName=navigator.appName; 
    if (browserName=="Microsoft Internet Explorer"){
   	 // Get the tbody which will be replaced
  	 var tableBody = document.getElementById(params[1]);
  	 // Move the new, invisible, rows to the visible table
  	 tableBody.parentNode.appendChild( tempSpanForRowReplace.firstChild.firstChild, tableBody);
   }
   else{    
  	 tempSpanForRowReplace.innerHTML = "<table id='"+params[2]+"'><tbody id='"+params[1]+"' class='"+params[1]+"'>"+data;
   	 var tableBody = document.getElementById(params[1]);//[0];    
  	 // Move the new, invisible, rows to the visible table
  	 tableBody.parentNode.appendChild( tempSpanForRowReplace.firstChild.firstChild, tableBody);
  	 removeExtrTbody(params[2],params[1]);
   }
}
function removeExtrTbody(tableName,classname)  {
	var node   =document.getElementById(tableName);
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("tbody");
    for(var i=0; i<els.length; i++)
        if(re.test(els[i].className)){
	        if(els[i].innerHTML.trim().length==0 || i<els.length-1 )		 
				els[i].parentNode.removeChild(els[i]);
        }
}
 
/*General function to call Cruise Deatils tabs*/

function showDetails(obj,type){

	var srcForm = eval(document.getElementById('SearchResultsCruiseForm'));
	var cdnForm = eval(document.getElementById('CruiseDetailsNavigationForm'));
	var drpDwnObj = eval(document.getElementById(obj+'drpDwn'));
	var voyageId = drpDwnObj.options[drpDwnObj.selectedIndex].value;
	
	cdnForm.ship.value=srcForm.ship.value;
	cdnForm.subTrade.value=srcForm.subTrade.value;
	cdnForm.date.value=srcForm.date.value;
	cdnForm.duration.value=srcForm.duration.value;
	cdnForm.orderBy.value=srcForm.orderBy.value;
	cdnForm.pageOffset.value=srcForm.pageOffset.value;
	cdnForm.voyageCode.value=voyageId;
	
	if(type=='P')
		cdnForm.action="/find/pb/cruiseDetailsPricing.do"; 
	else if(type =='I')
		cdnForm.action="/find/pb/cruiseDetailsItinerary.do";
	else if(type =='S')
	 	cdnForm.action="/find/pb/cruiseDetailsShip.do";
		
	cdnForm.submit();
}
/*Call back function of ajax call in getPrice*/

function err(){
}