
// formValidator_corp.js
// @developer : kathy.min@starwoodhotels.com

// ------------------------------------------------------------------------------------
				// I N I T
// ------------------------------------------------------------------------------------

function init(form)
{

		// load the dates
		loadDates(form);// in dateValidator.js

		//get state
		loadStates(form);

		// get country
		loadCountry(form);
}


// UTILITY FUNCTIONS : LOADERS
// ------------------------------------------------------------------------------------
// set the in & out dates with the hidden fields date values if any
// hidden fields date format is yyyy-mm-dd
function loadDates(fo)
{
		// load the dates from the hidden fields, or if they're empty, default the to today
		if (fo.arrivalDate != null && fo.departureDate != null){
   		var arv = fo.arrivalDate.value;
   		var dpt = fo.departureDate.value;

		if (arv != '' && dpt != '')
			loadDatesFromFields(fo, arv, dpt);
		}
}

function loadStates(form)
{
	if(form.state_province != null )
		{
				loadIntoListName(form.state_province, form.stateProvince);
		}
}

function loadCountry(form)
{
	if(form.nCountry != null)
		{
			loadIntoListName(form.nCountry, form.country);
		}
}

// load a val into drop down after finding its index
function loadIntoListName(fromVal, toField)
{
    	if(toField == null || fromVal == null)
  			return;
	    if(arguments[2] == null && fromVal.value == '')
			return;

	   for( var i =0; i < toField.options.length ; i++)
	  		 if( toField.options[i].value == fromVal.value )
			 {
			 		toField.selectedIndex = i;
					break;
			 }

	   //debug(toField.name + " field coming in: " + fromVal.value);
}

// check if date fields exist
// use this at the beginning of every function that operates on date fields
function datesExist(f)
{
		  if(
		    f.arrivalDateMonth == null || f.arrivalDateDay == null || f.arrivalDateYear == null
			|| f.departureDateMonth == null || f.departureDateDay == null
			|| f.departureDateYear == null
		    )
		 		return false;

			return true;
}


// load the hidden fields dates into the drop-down
function loadDatesFromFields(fo, arv, dpt)
{
	arr = new Array(3);
    offset = parseInt(fo.arrivalDateYear.options[1].value.toString().slice(2),10)-1;
   		// checkin
   		if(arv != '' && (arr = parser(arv)) != null)
   		{
	  	//debug('From parser, arrivalDate : ' + arr[0] + ' ' + arr[1] + ' ' + arr[2]);
	  	fo.arrivalDateYear.options[ getYearIdx( arr[0]-offset )].selected = true;
      		fo.arrivalDateMonth.options[ arr[1]].selected = true;
      		fo.arrivalDateDay.options[ arr[2]].selected = true;
	  		var chin = new Date( arr[0], arr[1]-1, arr[2] );
   		}

   		// checkout
   		if(dpt != '' &&  (arr = parser(dpt)) != null)
   		{
      		//debug('From parser, departureDate : ' + arr[0] + ' ' + arr[1] + ' ' + arr[2]);
      		fo.departureDateYear.options[ getYearIdx( arr[0]-offset )].selected = true;
      		fo.departureDateMonth.options[ arr[1]].selected = true;
      		fo.departureDateDay.options[ arr[2]].selected = true;
	  		var chout = new Date( arr[0], arr[1]-1, arr[2] );
   		}

   		// len of stay
   		if(arv != '' && dpt != '' && fo.lengthOfStay != null)
		{
			var diff = getDiffDaysNumber(chin,chout);
			if(diff > 31 || diff < 0)
				diff = 0;

			setStayValue(fo, diff);
			/*(fo.lengthOfStay.options != null) ?
     			(fo.lengthOfStay.options[ diff ].selected = true)
				: (fo.lengthOfStay.value = diff) ;*/

   		}

		//debug('Date fields coming in: ');
	        //debug('---------------');
	  	//debug('arrivalDate= ' + arv);
	  	//debug('departureDate= ' + dpt);
	  	//debug('lengthOfStay= ' + getStayValue(fo));
	  	//debug('---------------');
}


//--------------------------------------------------------
// diff in days between two dates
function getDiffDaysNumber(indate,outdate)
{
  		// this will give us a float i.e something like 7.9583333.. for 8 days
  		var df =  ( outdate.getTime()-indate.getTime() ) / (1000 * 60 * 60 * 24);

  		// convert to a whole number
  		return Math.round(df);
}

// set the lOfStay val, if exists
function setStayValue(f,val)
{
	//commented out to avoid displaying blank LOS
	//if(f.lengthOfStay == null)
	//	return ;

	(f.lengthOfStay.options != null) ?
     			(f.lengthOfStay.options[ parseInt(val,10) ].selected = true)
				: (f.lengthOfStay.value = val) ;

}


// parse dates
function parser(input)
{
  		if(input == null || input == '')
     		return null;

  		// input format is yyyy-mm-dd
		s1 = input.indexOf('-');
		s2 = input.lastIndexOf('-');
		if(s1 == -1 || s2 == -1)
		{
		   // debug('Dates are in wrong format !!');
		   return null;
		}
		y = input.substr(0,4);
		m = input.substr(s1 + 1 ,2);
		d = input.substr(s2 + 1,2);

   		// debug('From parser : ' + y + ' ' + m + ' ' +  d);

		// get rid of possible leading zero, otherwise in '0d' we'll get '0' instead of 'd'
		// when m & d will be converted to ints in Netscape(4 &6).
		m = stripZero(m), d = stripZero(d);

		return [ y, m, d ];
}

//-----------------------------------------------
// strip leading zero, if any.
function stripZero(s)
{
   if(s.charAt(0) == '0')
      s = s.substr(1);

   return s;
}

