  function compareCruises(formName, tunnel) {
    var form = document.forms[formName];
    var queryString = "";
    if(typeof(form.cruiseId.length)!="undefined") {
	    if(typeof(form.tourCruiseId.length)!="undefined") { //cruisetour
    	  for(i=0; i<form.cruiseId.length; i++) {
        	if(i!=0) {
	          queryString = queryString + "&";
    	    }
			
        	if((form.tourCruiseId[i].value == "") && (form.tourTourId[i].value == "") && (form.cruiseId[i].value != "undefined")) {
        		queryString = queryString+"voyageCode"+(i+1)
		        				+"="+form.cruiseId[i].value;
        	}else {
	        	queryString = queryString+"voyageCode"+(i+1)+"="+form.tourCruiseId[i].value+"&"
        					+"tourId"+(i+1)+"="+form.tourTourId[i].value;        		
        	}
	      }
	   } else {//cruise
	   			for(i=0; i<form.cruiseId.length; i++) {
        			if(i!=0) {
			          queryString = queryString + "&";
    			    }
		        	queryString = queryString+"voyageCode"+(i+1)+"="+form.cruiseId[i].value;
	      		}	
	   	}
    } else {
    		if(form.tourCruiseId.value == "" && form.tourTourId.value == "") {
				queryString = "voyageCode1="+form.cruiseId.value;
        	}
		    else  {
				queryString = "voyageCode1="+form.tourCruiseId.value+"&"
        					+"tourId1="+form.tourTourId.value;        		
		    }
    }
    queryString = queryString+"&source="+getCurrentURI();
    var url =  "pb/viewCompareList.do?"+queryString;
    if(typeof(tunnel)=="undefined") 
      location.href="/"+url;
    else 
      location.href="compareCruises.page?compare="+encodeURIComponent(url);
  }
  
  function goToPriceDetails(cruiseId){
    location.href="/pb/priceDetails.do?voyageCode="+cruiseId+"&source="+sourceURI();
  }
  
  function goToItineraryDetails(cruiseId){
    location.href="/pb/itineraryDetails.do?voyageCode="+cruiseId+"&source="+sourceURI();
  }

  function goToTourPriceDetails(cruiseId, tourId){
    location.href="/pb/priceDetails.do?voyageCode="+cruiseId+"&tourCode="+tourId+"&resType=T&source="+sourceURI();
  }
   
  function sourceURI(){  
		var url = location.pathname;
		url = ".."+url;
	    return url;
  }
   
  function findAnotherCruise(findLink){
//  alert(findLink+"&source="+getCurrentURI());
    location.href=findLink+"&source="+getCurrentURI();
  } 

  function getCurrentURI(){
    var uri = location.pathname;
    var url = location.href;
    firstpos=url.indexOf(uri)+uri.length;
    lastpos=url.length;
    uri = uri + url.substring(firstpos,lastpos);
    uri=encodeURIComponent(uri);
    return uri;
  }

  // check for space and alphabets (a-z and A-Z)
  function containsAlphabetAndSpace(val) {
    var regExp = /[^A-Za-z ]/;
    return !regExp.test(val);
  }

  function getImage(imgName, row){
    var children = row.getElementsByTagName("IMG");
    for (var i=0; i< children.length; i++){
        if(children[i].src.indexOf(imgName) != -1)
          return children[i];
    }
    return null;
  }

  function getField(fieldName, row){
    var children = row.getElementsByTagName("INPUT");
    for (var i=0; i< children.length; i++){
        if(children[i].name.indexOf(fieldName)===0)
          return children[i];
    }
    return null;
  }

  function createField(name, value, size, maxlength, style) {
     var type="INPUT";
     var element = null;
     try {
        element = document.createElement('<'+type+' type="text" name="'+name+'" value="'+value+'"  size="'+size+'" class="'+style+'" maxlength="'+maxlength+'">');
     } catch (e) {
     }
     if (!element || element.nodeName != type.toUpperCase()) {
        element = document.createElement(type);
        element.name = name;
        element.setAttribute("value", value );
        element.setAttribute("size", size );
        element.setAttribute("maxlength", maxlength );
        element.setAttribute("class", style );
     }
     return element;
  }

  function duplicateEmailsEntered(currForm) {
/*    var noOfInvitees = Number(currForm.noOfInvitees.value);
    alert("AAAAAAAA"+noOfInvitees );
    for (var i=0; i<noOfInvitees ; i++) {
      var benchmark = trim(currForm['email['+i+']'].value);
      for(var j = (i+1); j<noOfInvitees ; j++) {
        alert(i+".."+j);
        var compare = trim(currForm['email['+j+']'].value);
        if(benchmark == compare) {
          return true;
          break;
        }
      }
    }*/
    return false;
  }

  function addNewUser(formName,  addImg, rowId){
    var currForm = document.forms[formName];
    if(currForm.noOfInvitees.value < 20) {
      var firstName = currForm['firstName['+rowId+']'];
      var lastName = currForm['lastName['+rowId+']'];
      var email = currForm['email['+rowId+']'];
      if(trim(firstName.value) == "") {
        firstName.value = "";
        alert("Please enter the First Name before creating a new row");
        firstName.focus();
      }else if(!containsAlphabetAndSpace(firstName.value)){
        firstName.value = "";
        alert("Invalid First Name. It should contain only alphabets.");
        firstName.focus();
      }else if(trim(lastName.value) == "") {
        lastName.value = "";
        alert("Please enter the Last Name before creating a new row");
        lastName.focus();
      }else if(!containsAlphabetAndSpace(lastName.value)){
        lastName.value = "";
        alert("Invalid Last Name. It should contain only alphabets.");
        lastName.focus();
      }else if(trim(email.value)== "") {
        email.value = "";
        alert("Please enter the Email before creating a new row");
        email.focus();
      }else if(!isValidEmail(email.value)){
        email.value = "";
        alert("Please use a valid E-mail format.");
        email.focus();
      } else if(duplicateEmailsEntered(currForm)) {
        email.value = "";
        alert("Please enter different E-mail ids for the invitees");
        email.focus();
      } else{
        // Clone the last row and insert the new row before the last row
        //and rename the button to 'Remove' - START
        var addRow = addImg.parentNode.parentNode;
        table = addRow.parentNode;
        var newRow = addRow.cloneNode(true);
        newRow.removeAttribute("id");
        var button = getImage("btn_add.gif", newRow);
        button.src="/images/portfolio/btn_remove.gif";
        button.onclick = function (){ removeImageRow(button, currForm)};
        table.insertBefore(newRow, addRow);
        var spacerRow = document.getElementById("spacerRowInvities");
        spacerRow = spacerRow.cloneNode(true);
        spacerRow.id="";
        var spacer = getImage("divline.gif", spacerRow);
        spacer.height=11;
        table.insertBefore(spacerRow, addRow);
        // Clone the last row and insert the new row before the last row
        //and rename the button to 'Remove' - END

        //To create fresh counter for the last row.-- start
        var newRowId = rowId+1;

        firstNameField = getField("firstName", addRow);
        var newField = createField("firstName["+(newRowId)+"]", '','22', '30', 'graybodycopy');
        firstNameField.parentNode.replaceChild(newField, firstNameField);

        lastnameField = getField("lastName", addRow);
        newField = createField("lastName["+(newRowId)+"]", '', '22', '30', 'graybodycopy');
        lastnameField.parentNode.replaceChild(newField, lastnameField);

        emailField = getField("email", addRow);
        newField = createField("email["+(newRowId)+"]", '', '30', '30', 'graybodycopy');
        emailField.parentNode.replaceChild(newField, emailField);

        var newButton = getImage("btn_add.gif", addRow);
        newButton.onclick = function (){ addNewUser(formName, newButton,newRowId)};
        //To create fresh counter for the last row.- end

        currForm.noOfInvitees.value = Number(currForm.noOfInvitees.value)+1;

      }

    } else {
      alert("You can add only an maximum of 20 invitees.");
    }
  }


  function removeImageRow(removeImg, currForm){
    var removeRow = removeImg.parentNode.parentNode;
    remove(removeRow, currForm);
  }

  function remove(removeRow, form){
    var spacerRow = removeRow.nextSibling;
    //Coz' FF treats the space between elemnts as a text element.
    while (spacerRow.tagName != "TR")
      spacerRow = spacerRow.nextSibling;
    table = removeRow.parentNode;
    table.removeChild(removeRow);
    table.removeChild(spacerRow);
    form.noOfInvitees.value = Number(form.noOfInvitees.value)-1;
  }

  function showErrorMessage(divIdName,msg) {
    strMsg = '<table border="0" cellspacing="0" cellpadding="10" align="left"><tr><td><span class="orangeheader">'+
            msg+'</span></td></tr></table>';
    showMesg(strMsg,divIdName);
  }

  function removeUnfilledLastRow(form) {
    var lastInviteeRow = document.getElementById("inviteeLastRow");
    lastRowFirstNameField  = getField("firstName", lastInviteeRow);
    lastRowLastNameField = getField("lastName", lastInviteeRow);
    lastRowEmailField = getField("email", lastInviteeRow);

    if(!(trim(lastRowFirstNameField.value) != "" || trim(lastRowLastNameField.value) != "" || trim(lastRowEmailField.value) != "")) {
      remove(lastInviteeRow, form);
    }
  }

  // move the control to the top
  function moveToTop(location) {
    locationHref = window.location.href;
    if(locationHref.indexOf(location,0)==-1) {
      window.location.href=locationHref+location;
    } else {
      window.location.href=locationHref;
    }
  }

  // validating email address-should be in the format x@x.x
  function isValidEmail(sEmail) {
    sEmail=trim(sEmail);
    if( checkEmail(sEmail) ) {
      return true;
    } else {
      return false;
    }
  }
  
  function checkEmail(sEmail) {
  	var regFormat = /^[A-Za-z0-9]+((-\w+)|((\.|_)\w*))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  	//var regFormat = /^.+\@.+\..+$/;
  	var sPos = -1;
  	
  	sPos = sEmail.search(regFormat);
  	if (sPos != -1) {
  		return true;
  	}
  	else {
  		return false;
  	}
}
  
