
// Submit request to the server
// Param1: Action flag
function senddata(flag){

  var data = dataget();
  var tempAry  = new Array();
  var tempAry2 = new Array();
  var temp = '';
  var mymethod = '';

  // Check and special handling for each enterance //
  if (flag == 'itineraryList' || flag == 'earlybirdList' || flag == 'hotdealList' || flag == 'cruisetourList' || 
      flag == 'itineraryDetails' || flag == 'itineraryList_re' || flag == 'passcruisetourList' ||
      flag == 'asiacruisetourList' || flag == 'holidayList' || flag == 'staticTourPhoto'){
    document.getElementById('submitForm').method = "GET";
  }

  // Re-do the URL to make sure it post correctly //
  temp = location.href.replace(/\#/g,'');
  tempAry = temp.split('?');

  // Submit request !! //
  if (flag !== null && flag != ''){
      data[0] += '<input type=hidden name=e value="'+flag+'">';
      if (data[1]==''){data[1] += 'e='+flag;}else{data[1] += '&e='+flag;}
  }

  mymethod = document.getElementById('submitForm').method;
  if (mymethod.toLowerCase() == 'get'){
     location.href = tempAry[0] + '?' + data[1];
  }else{
    document.getElementById('submitFlag').innerHTML = data[0];
    document.getElementById("submitForm").submit();
  }
  return false;
}



// Submit the itinerary detail request to the server
// Param1: Deal Number
// Param2: The Tab name, default will use itinerary
// Note: current tab name support itinerary, pricing, photo, others
function req_itineraryDetails(dealNumber, tabname){

  var data = '';
  var tempAry = new Array();
  var temp = '';

  if (tabname == undefined || tabname == null || tabname == ''){
    tabname = 'itinerary';
  }

  // Re-do the URL to make sure it post correctly //
  temp = location.href.replace(/\#/g,'');
  tempAry = temp.split('?');

  // Submit request !! //
  data = 'e=itineraryDetails&dealNumber='+dealNumber+'&tabname='+tabname;
  location.href = tempAry[0] + '?' + data;
  return false;
}


// Submit the request by url "get" method, and replace the paramater to the new value
// This function ignore all form data, only use the original URL with new requested parameter
// Ex: ?a=10&b=20&c=30, by calling this function with param:b, this function expect a onscreen value with
// id:b and replace to the new url, if b is 80, the new submission will be ?a=10&b=80&c=30
// Param1: The id of the value you wish to replace, id will be use as the parameter name with onscreen value
// Return: boolean false always
function submitgetp(id, idary){

  var data = dataget();
  var tempAry  = new Array();
  var tempAry2 = new Array();
  var temp = '';var value = '';
  var mymethod = '';
  var myid = '';
  if (idary === null || idary == undefined || idary == ''){idary = new Array();}
  idary[idary.length] = id;

  // Re-do the URL to make sure it post correctly //
  temp = location.href.replace(/\#/g,'');
  tempAry = temp.split('?');

  if (document.getElementById(id) === null){return false;}

  for (key in idary){
      myid = idary[key];
      tempAry2 = data[1].split('&');
      for (var i=0; i < tempAry2.length; i++){
	  if (tempAry2[i].indexOf(myid+'=') == 0){
	      value = tempAry2[i];
	  }
      }
      
      tempAry2 = tempAry[1].split('&');
      tempAry[1] = '';
      for (var i=0; i < tempAry2.length; i++){
	  if (tempAry2[i].indexOf(myid+'=') != 0){
	      if (tempAry[1] == ''){tempAry[1] = tempAry2[i];}
	      else {tempAry[1] = tempAry[1] + '&' + tempAry2[i];}
	  }
      }
      if (tempAry[1] == ''){tempAry[1] = value;}
      else{tempAry[1] = tempAry[1] + '&' + value;}
  }
  location.href = tempAry[0] + '?' + tempAry[1];

  return false;
}



function submitgetpv(valueary){

  var myvalue  = '';
  var tempAry  = new Array();
  var tempAry2 = new Array();
  var value    = '';

  if (valueary === null || valueary == undefined || valueary == ''){return false;}

  // Re-do the URL to make sure it post correctly //
  temp = location.href.replace(/\#/g,'');
  tempAry = temp.split('?');

  for (key in valueary){
      value = key +'='+ valueary[key];
      if (tempAry.length > 1){tempAry2 = tempAry[1].split('&');}
      tempAry[1] = '';
      for (var i=0; i < tempAry2.length; i++){
	  if (tempAry2[i].indexOf(key+'=') != 0){
	      if (tempAry[1] == ''){tempAry[1] = tempAry2[i];}
	      else {tempAry[1] = tempAry[1] + '&' + tempAry2[i];}
	  }
      }
      if (tempAry[1] == ''){tempAry[1] = value;}
      else{tempAry[1] = tempAry[1] + '&' + value;}
  }
  location.href = tempAry[0] + '?' + tempAry[1];

  return false;
}




// Control the sidebar display (last search result) in itinerary details
// This function use javascript to hidden the data, and use AJAX to submit to server for future record
// Note: add id + 'b' in the button / image of the html id who use to control the minimize / maximize
// Param1: Turn On Last Search Result Sidebar (true: turn on sidebar, false: turn off sidebar)
// Return: boolean false always
function minmaximizecontrol(id, flag){

    if (flag === null || flag == undefined){return false;}
    if (id === null || id == undefined || id == ''){return false;}
    if (document.getElementById(id) === null){return false;}

    url = location.href;
    url = url.replace(/\#/g,'');
    tempAry = url.split('?');
    url = tempAry[0];

    if (flag === true){
	if (document.getElementById(id) !== null){document.getElementById(id).style.display  = 'inline';}
	if (document.getElementById(id+'b') !== null){
	    if (id == 'qrsidebar'){
		document.getElementById(id+'b').style.display = 'none';
	    } else {
		document.getElementById(id+'b').src = mintabimg2.src;
		document.getElementById(id+'b').onmouseover = function(){this.src=mintabimg1.src;}
		document.getElementById(id+'b').onmouseout = function(){this.src=mintabimg2.src;}
		document.getElementById(id+'b').onclick = function(){minmaximizecontrol(id, false);}
	    }
	} 
	if (id == 'qrsidebar'){ajax_go(url,'e=setmypreference&key=noquickresult&value=1',true);}
	else{ajax_go(url,'e=setmypreference&key='+id+'&value=1',true);}
    }else {
	if (document.getElementById(id) !== null){document.getElementById(id).style.display  = 'none';}
	if (document.getElementById(id+'b') !== null){
	    if (id == 'qrsidebar'){
		document.getElementById(id+'b').style.display = 'inline';
	    } else {
		document.getElementById(id+'b').src = maxtabimg2.src;
		document.getElementById(id+'b').onmouseover = "function onmouseover(event){this.src=maxtabimg1.src;}";
		document.getElementById(id+'b').onmouseout = "function onmouseover(event){this.src=maxtabimg2.src;}";
		document.getElementById(id+'b').onclick = function(){minmaximizecontrol(id, true);}
	    }
	}
	if (id == 'qrsidebar'){ajax_go(url,'e=setmypreference&key=noquickresult&value=0',true);}
	else{ajax_go(url,'e=setmypreference&key='+id+'&value=0',true);}
    }
    return false;
}



// Submit the quick deal number search by id (default id=fastdealsearch)
// Param1: (Opt.) The ID you wish to search
// Return: false always
function fastdealsearch(id){
    if (id === null || id === undefined || id == ''){id = 'fastdealsearch';}

    if (document.getElementById('fastdealsearch') === null){
	alert ("Error Getting Deal Number, Please contact Babysoft.");
	return false;
    }

    value = document.getElementById('fastdealsearch').value;
    if (value == '' || isNumber('fastdealsearch') === false || value.length < 7){
	document.getElementById('fastdealsearch').value = '';
	alert ("Invalid Deal Number, please try again.");
	return false;
    }

    req_itineraryDetails(value);
    return false;
}


// Apply filter to existing search result //
// Param: null
// Return: false
function applyfilter(){

    var myid = '';
    var myidary = new Array();
    
    var data = dataget();
    var tempAry  = new Array();
    var tempAry2 = new Array();
    var temp = '';var value = '';
    var mymethod = '';
    var myid = '';
    var myvalue = '';
    var myfilter = false;

    if (document.getElementById('price_filterid') !== null){
	myvalue = document.getElementById('price_filterid').options[document.getElementById('price_filterid').selectedIndex].value;
	if (myvalue != '' && myvalue !== null && myvalue != undefined){
	    myidary['price_id'] = myvalue; myfilter = true;
	}
    }
    if (document.getElementById('cruiseline_ship_filterid') !== null){
	myvalue = document.getElementById('cruiseline_ship_filterid').options[document.getElementById('cruiseline_ship_filterid').selectedIndex].value;
	if (myvalue != '' && myvalue !== null && myvalue != undefined){
	    myidary['cruiseline_ship_id'] = myvalue; myfilter = true;
	}
    }
    if (document.getElementById('destination_filterid') !== null){
	myvalue = document.getElementById('destination_filterid').options[document.getElementById('destination_filterid').selectedIndex].value;
	if (myvalue != '' && myvalue !== null && myvalue != undefined){
	    myidary['destination_id'] = myvalue; myfilter = true;
	}
    }

    if (myfilter === false){ 
	alert ("To use the result filter, please select your restriction(s) in the drop down list."); 
    } else {

	// Re-do the URL to make sure it post correctly //
	temp = location.href.replace(/\#/g,'');
	tempAry = temp.split('?');

	for (key in myidary){
	    myid = key;
	    tempAry2 = tempAry[1].split('&');
	    tempAry[1] = '';
	    for (var i=0; i < tempAry2.length; i++){
		if (tempAry2[i].indexOf(myid+'=') != 0){
		    if (tempAry[1] == ''){tempAry[1] = tempAry2[i];}
		    else {tempAry[1] = tempAry[1] + '&' + tempAry2[i];}
		}
	    }
	    if (tempAry[1] == ''){tempAry[1] = myid + '=' + myidary[myid];}
	    else{tempAry[1] = tempAry[1] + '&' + myid + '=' + myidary[myid];}
	}
	location.href = tempAry[0] + '?' + tempAry[1];
    }
    return false;

}


// View the current, updated Wish List with the first itinerary in list //
// Param1 / Return: Null
function viewwishlist(){

  var data = '';
  var tempAry = new Array();
  var temp = '';

  // Re-do the URL to make sure it post correctly //
  temp = location.href.replace(/\#/g,'');
  tempAry = temp.split('?');

  // Submit request !! //
  data = 'e=wishList';
  location.href = tempAry[0] + '?' + data;
  return false;

}


// Add / Remove all to wish list
// Param: boolean true: add all to wish list, boolean false: remove all from wish list
// Return: false always
function wishlistcontrolall(flag){
    
    if (flag === null){return false;}
    var spans = document.getElementsByTagName('span');
    var tempAry = new Array();
    var url = location.href; url = url.replace(/\#/g,'');
    var urlAry = url.split('?'); url = urlAry[0];
    var itid = ''; var query = '';
    var wlist; var obj;
    var template_add = WISHLIST_ORIGINAL;
    var template_rm  = WISHLIST_ADDED;

    for (i=0; i < spans.length; i++){
	if (spans[i].id !== null && spans[i].id !== undefined){
	    // Found what we need to do //
	    if (spans[i].id.indexOf('wishlistid_') == 0){
		tempAry = spans[i].id.split('_');
		itid = tempAry[1];
		wlist = document.getElementById('wishlistid_'+itid);
		if (flag === true){
		    wlist.innerHTML = template_rm.replace('{TEMPLATE_MYITID}', itid);
		    query = 'e=wishlistcontrol&mycontrol=3';
		}else{
		    wlist.innerHTML = template_add.replace('{TEMPLATE_MYITID}', itid);		    
		    query = 'e=wishlistcontrol&mycontrol=4';
		}


	    }
	}
    }
    ajax_go(url, query, true, '');
    return false;

}


// Add / Remove to Wish List (one by one)
// Param1: boolean true: add to list, boolean false: delete from list
// Param2: The itinerary id you wish to add / remove
function wishlistcontrol(flag, itid){

    var query = '';
    var wlist = '';
    var url = location.href; url = url.replace(/\#/g,'');
    var tempAry = url.split('?'); url = tempAry[0];

    if (itid === null){return false;}
    else if (flag === true){ // add to list //
	query = 'e=wishlistcontrol&mycontrol=1&myitid='+itid;
    }
    else if (flag === false){ // remove from list //
	query = 'e=wishlistcontrol&mycontrol=2&myitid='+itid;
    }

    wlist = document.getElementById('wishlistid_'+itid);
    wlist.innerHTML = '<img height=16 src="./others/images/myloading.gif" />&nbsp;&nbsp;' + wlist.innerHTML;
    ajax_go(url, query, true, 'wishlistid_'+itid);
    return false;

}
