/***************************************************************************
 * $Id: webBooking.js,v 1.12 2009/04/30 00:37:13 kfawcett Exp $
 * -------------------
 * begin                : 4/15/2007 10:31PM
 * copyright            : (C) 2004-2007 Zaui Software Ltd.
 * application          :
 * email                : kfawcett@zaui.com
 * www                  : http://www.zaui.com
 * $Id: webBooking.js,v 1.12 2009/04/30 00:37:13 kfawcett Exp $
 * File History:
 * $Log: webBooking.js,v $
 * Revision 1.12  2009/04/30 00:37:13  kfawcett
 * fixed for the second calendar dates comparisions and update for the TSE
 *
 * Revision 1.11  2009/04/23 03:21:32  kfawcett
 * fixed the layout for the cart and confirmatiopn for the pickup and drop off times
 *
 * Revision 1.10  2009/04/09 23:16:01  kfawcett
 * added js check for the policies
 *
 * Revision 1.9  2009/03/10 00:08:04  kfawcett
 * fixed the 2 dates in js
 *
 * Revision 1.8  2009/03/06 01:49:27  kfawcett
 * added new changes for booking point to point online
 *
 * Revision 1.7  2008/12/06 08:10:43  kfawcett
 * added new processing image
 * cleaned up the spalsh screen for processing
 *
 * Revision 1.6  2008/09/14 00:04:42  kfawcett
 * complete code clean up
 * added the abiulity for voucher payments
 * split the themes for cc and invoice payments
 * added new jks callbacks for payments
 * added the ability for wholesalers to see their bookings online
 * added payment trans to the confirmation screen
 *
 * Revision 1.5  2008/06/03 16:23:42  kfawcett
 * All new web booking files for the front end
 *
 * Revision 1.4.2.3  2008/05/16 17:58:27  kfawcett
 * customized changes for POW
 *
 * Revision 1.4.2.2  2008/05/13 01:10:25  kfawcett
 * Add new changes to the new web booking engine public interface
 *
 * Revision 1.4.2.1  2008/01/23 18:56:08  kfawcett
 * New search component for the web bookings
 *
 * Revision 1.4  2007/04/17 15:58:25  kfawcett
 * Added in logic for the expiry date on contacts and affiliate login information
 *
 * Revision 1.3  2007/04/16 19:04:49  kfawcett
 * Added new csv files and images
 * added new js code for the data validation
 *
 * Revision 1.2  2007/04/16 17:00:00  kfawcett
 * New changes to the webbooking module.
 * added JS data validator
 * added new csv popup page.
 *
 *
 ***************************************************************************/

/*
-----------------------------------------
-- Routines for funky data validation
-- Zaui Software Ltd.
-- 4/16/2007 9:35AM
-----------------------------------------
*/
var inputsWithErrors = new Array();
function hex2dec(hex)
{
   return(parseInt(hex,16));
}
var hexDigit=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function dec2hex(dec)
{
   return(hexDigit[dec>>4]+hexDigit[dec&15]);
}
function fade_error_style(normalStyle, percent)
{
	errorStyle = 'c60c30';
	var r1 = hex2dec(errorStyle.slice(0,2));
	var g1 = hex2dec(errorStyle.slice(2,4));
	var b1 = hex2dec(errorStyle.slice(4,6));

	var r2 = hex2dec(normalStyle.slice(0,2));
	var g2 = hex2dec(normalStyle.slice(2,4));
	var b2 = hex2dec(normalStyle.slice(4,6));
	var pc = percent / 100;
	r= Math.floor(r1+(pc*(r2-r1)) + .5);
	g= Math.floor(g1+(pc*(g2-g1)) + .5);
	b= Math.floor(b1+(pc*(b2-b1)) + .5);
	for(var wp = 0; wp < inputsWithErrors.length; wp++)
	{
		inputsWithErrors[wp].style.backgroundColor = "#" + dec2hex(r) + dec2hex(g) + dec2hex(b);
	}
}
function add_error_style(formname, input, txt)
{
	inputHandle = eval("document." + formname + "['" + input + "']");

//	if(inputHandle.parentNode.innerHTML.search(txt) == -1)
	{
		errorTextNode = document.createElement('span');
		errorTextNode.className = 'required';
		errorTextNode.innerHTML = '<br /><font class=tiny>' + txt + '</font>';
		inputHandle.parentNode.appendChild(errorTextNode);
      inputHandle.focus();
   	style = get_current_bgcolor(inputHandle);
   	inputHandle.style.backgroundColor = "#FF0000";
   	inputsWithErrors.push(inputHandle);

   	for(wp = 1; wp <= 10; wp++)
   	{
   		window.setTimeout('fade_error_style(style, ' + wp * 10 + ')', 1000 + (wp * 50));
   	}
	}
}
function get_current_bgcolor(input)
{
	if(input.currentStyle)
	{// ie
		style = input.currentStyle.backgroundColor;
		return style.substring(1,7);
	}
	else
	{// moz
		style = '';
		styleRGB = document.defaultView.getComputedStyle(input, '').getPropertyValue("background-color");
		comma = styleRGB.indexOf(',');
		style += dec2hex(styleRGB.substring(4, comma));

		commaPrevious = comma;
		comma = styleRGB.indexOf(',', commaPrevious+1);
		style += dec2hex(styleRGB.substring(commaPrevious+2, comma));
		style += dec2hex(styleRGB.substring(comma+2, styleRGB.lastIndexOf(')')));
		return style;
	}
}
/*
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
*/



   function openCustomPopup(documentRoot, module_name, file_name, width, height)
   {
      URL = documentRoot + '/modules/' + module_name + '/' + file_name ;

      windowName = 'popup_window';
      windowFeatures = 'width=' + width
                   + ',height=' + height
                   + ',resizable=1,scrollbars=1,statusbar=1';
      win = window.open(URL, windowName, windowFeatures);

      if(window.focus)
      {
         win.focus();
      }
      return win;
   }

   function webBookingValidateData(name)
   {
   	var theForm = eval("document." + name);

      if(name == "affiliateLogin")
      {
         if(!theForm.username.value)
         {
            add_error_style(name, "username", "* required") ;
            return false;
         }
         if(!theForm.password.value)
         {
            add_error_style(name, "password", "* required") ;
            return false;
         }
      }

      if(name == "checkoutVoucher")
      {
         if(!theForm.firstName.value)
         {
            add_error_style(name, "firstName", "* required") ;
            return false;
         }
         if(!theForm.lastName.value)
         {
            add_error_style(name, "lastName", "* required") ;
            return false;
         }
         if(!theForm.telephone.value)
         {
            add_error_style(name, "telephone", "* required") ;
            return false;
         }
         if(!theForm.email.value)
         {
            add_error_style(name, "email", "* required") ;
            return false;
         }
         if(!theForm.address.value)
         {
            add_error_style(name, "address", "* required") ;
            return false;
         }
         if(!theForm.invoiceVochure.value)
         {
            add_error_style(name, "invoiceVochure", "* required") ;
            return false;
         }
      }

      if (name == "checkout")
      {
         if(!theForm.firstName.value)
         {
            add_error_style(name, "firstName", "* required") ;
            return false;
         }
         if(!theForm.lastName.value)
         {
            add_error_style(name, "lastName", "* required") ;
            return false;
         }
         if(!theForm.telephone.value)
         {
            add_error_style(name, "telephone", "* required") ;
            return false;
         }
         if(!theForm.email.value)
         {
            add_error_style(name, "email", "* required") ;
            return false;
         }
         if(!theForm.address.value)
         {
            add_error_style(name, "address", "* required") ;
            return false;
         }
         /*Credit card information */
         if(!theForm.ccNameOnCard.value)
         {
            add_error_style(name, "ccNameOnCard", "* required") ;
            return false;
         }
         if(!theForm.ccNumber.value)
         {
            add_error_style(name, "ccNumber", "* required") ;
            return false;
         }
         if(!theForm.ccCsv.value)
         {
            add_error_style(name, "ccCsv", "* required") ;
            return false;
         }
         if(theForm.agreeToTerms)
         {
            if(theForm.agreeToTerms.checked == false)
            {
               add_error_style(name, "agreeToTerms", "* required") ;
               return false;
            }
         }


//return false;

/*
         if (!isNumeric(theForm.retailValue.value))
         {
            alert("Invalid retail amount.");
            theForm.retailValue.focus();
            return false;
         }
*/
      }

   }
   function FEDisallowDate(selectedDate, y, m, d)
   {

      var today = new Date();
      today.setDate(today.getDate()-1)

      selectedDate.setFullYear(y,m,d);
      if(selectedDate < today)
         return true;
      else
         return false;

   }//function FEDisallowDate
   function FEAlterSecondDate(cal)
   {
      $('endDate_id').value = $('startDate_id').value;
      cal.hide();
      return true;
   }

   function FEAlterSecondDatePointToPoint(cal)
   {

//alert($('activityDate_id').value);
//alert($('activityDate2_id').value);

      //need to test the departure and return dates
      var myString = new String($('activityDate_id').value);
      var pieces = myString.split("-");
//      var d = new Date();

      var depDate = new Date();
      depDate.setFullYear(pieces[0]);
      depDate.setMonth((pieces[1]-1));
      depDate.setDate(pieces[2]);

      var depTime = depDate.getTime()
//      var mytime = d.getTime();


      var returnString = new String($('activityDate2_id').value);
      var returnPieces = returnString.split("-");
      var returnDate = new Date();

      returnDate.setFullYear(returnPieces[0]);
      returnDate.setMonth((returnPieces[1]-1));
      returnDate.setDate(returnPieces[2]);

      var returnTime = returnDate.getTime();


      if(depTime > returnTime)
      {
         $('activityDate2_id').value = $('activityDate_id').value;
      }
      if(cal)
         cal.hide();
      return true;
   }


   function jsMiscShowHideDiv(ptxDivname,pynShow)
   {
      obj = document.getElementById(ptxDivname);
      if (pynShow) {
         obj.style.display = "block";
      }
      else
      {
         obj.style.display = "none";
      }
      return;
   }//function



