var leapdays = new Array(31,29,31, 30,31,30, 31,31,30, 31,30,31); 
var yeardays = new Array(31,28,31, 30,31,30, 31,31,30, 31,30,31); 

function js_enumerate_dates_value( thisday, thismonth , thisyear ) {
	
	date_value_population = new Array;
    array_months = new Array("01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10" , "11" , "12" );
    array_dates = new Array ("01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10" , "11" , "12" , "13" , "14" , "15" , "16" , "17" , "18" , "19" , "20" , "21" , "22" , "23" , "24" , "25" , "26" , "27" , "28" , "29" , "30" , "31");
    
	var myYear  = parseInt(thisyear);
	var myMonth = parseInt(thismonth,10);
    var cmDate  = parseInt(thisday,10);
    var calDate = new Date( thisyear,  thismonth, thisday, 0,0,0,0 ); 
    var mDays   = isLeapYear(thisyear) ? leapdays[thismonth]: yeardays[thismonth]; 
    
    for (var idate =0 ; idate < 366 ; cmDate++, idate++) {//diff_date
        if(cmDate>mDays) { 
            cmDate=1;
            myMonth = (myMonth +1)%12;
            mDays  = isLeapYear(myYear) ? leapdays[myMonth]: yeardays[myMonth]; 
            myYear = (myMonth == 0)? (myYear + 1): myYear;
        }
        
        var get_date = cmDate; 
        var get_month = myMonth; 
        var get_year = myYear; 
        
        var date_in_array = get_date-1;
        var date_value_format = get_year + "-" + array_months[get_month] + "-" + array_dates[date_in_array];
        date_value_population.push(date_value_format);
    }   

    return date_value_population;
}

function js_enumerate_dates_description( thisday, thismonth , thisyear ) {
    
    description_date_population = new Array;
    desc_short_month = new Array("Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" );
    desc_shot_date   = new Array("Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" );

    var myYear  = parseInt(thisyear);
	var myMonth = parseInt(thismonth,10);
    var cmDate  = parseInt(thisday,10);

    var calDate = new Date( thisyear,  thismonth, thisday, 0,0,0,0 ); 
    var mDays = isLeapYear(thisyear) ? leapdays[thismonth]: yeardays[thismonth]; 
    var wkDay = calDate.getDay(); 

    for (var idate =0 ; idate < 366 ; cmDate++, idate++) {//diff_date   
        var get_day = desc_shot_date[((idate +wkDay)%7)]  ;
        
        
        if(cmDate>mDays) { 
            cmDate=1;
            myMonth = (myMonth +1)%12;
            mDays = isLeapYear(myYear) ? leapdays[myMonth]: yeardays[myMonth]; 
            myYear    = (myMonth == 0)? myYear + 1: myYear;
        }        
        var get_date = cmDate; 
        var get_month = myMonth; 
        var get_year = myYear; 
        
        var date_desc_format =  get_day  + " " + get_date + " " + desc_short_month[get_month] + " " + get_year;  
        description_date_population.push( date_desc_format );
    }   

    return description_date_population;
}
function isLeapYear( year ){
    return ( (0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))); 
}

function create_date_object_from_string( input_date_string ) {

    var split_date_string = input_date_string.split("-");

    var year = split_date_string[0];
    var month = split_date_string[1]-1;
    var ndate = split_date_string[2]
    
    date_object = new Date( year , month , ndate );
    
    return date_object;
}

function js_enumerate_dates_value_old( start_date_value, end_date_value ) {
    
    date_value_population = new Array;
    
    array_months = new Array("01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10" , "11" , "12" );
    array_dates = new Array ("01" , "02" , "03" , "04" , "05" , "06" , "07" , "08" , "09" , "10" , "11" , "12" , "13" , "14" , "15" , "16" , "17" , "18" , "19" , "20" , "21" , "22" , "23" , "24" , "25" , "26" , "27" , "28" , "29" , "30" , "31");
    
    var epoch_start_date_value = Date.parse(start_date_value);
    var epoch_end_date_value   = Date.parse(end_date_value);
    var total_epoch_in_one_day = (24*60*60*1000);
    var diff_date              = Math.round( (epoch_end_date_value - epoch_start_date_value) / total_epoch_in_one_day );

    for (var idate =0 ; idate <= diff_date ; idate++) {
        
        var epoch_next_date = epoch_start_date_value + (total_epoch_in_one_day * idate);
        next_date_object = new Date(epoch_next_date);
        
        var get_date = next_date_object.getDate();
        var get_month = next_date_object.getMonth();
        var get_year = next_date_object.getFullYear();
        
        var date_in_array = get_date-1;
        var date_value_format = get_year + "-" + array_months[get_month] + "-" + array_dates[date_in_array];

        date_value_population.push(date_value_format);
        
    }   

    return date_value_population;
}

function js_enumerate_dates_description_old( start_date_desc, end_date_desc ) {
    
    description_date_population = new Array;
    desc_short_month = new Array("Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec" );
    desc_shot_date   = new Array("Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" );

    var epoch_start_date_desc  = Date.parse( start_date_desc);
    var epoch_end_date_desc    = Date.parse( end_date_desc  );
    var total_epoch_in_one_day = (24*60*60*1000);
    var diff_date              = Math.round( (epoch_end_date_desc - epoch_start_date_desc) / total_epoch_in_one_day )

    for(var sdate =0 ; sdate <= diff_date ; sdate++) {
        
        var epoch_next_date  = epoch_start_date_desc + (total_epoch_in_one_day * sdate);
        next_date_object = new Date(epoch_next_date);
        
        var get_day   = next_date_object.getDay();
        var get_date  = next_date_object.getDate();
        var get_month = next_date_object.getMonth();
        var get_year  = next_date_object.getFullYear();
        
        var date_desc_format = desc_shot_date[get_day] + " " + get_date + " " + desc_short_month[get_month] + " " + get_year; 
        description_date_population.push( date_desc_format );
    }   

    return description_date_population;
}

function do_create_option_dropdown( value_population , desc_population , default_value , tag_id ) {
    
    var option_dropdown;
    if(tag_id){
    var tags_id = tag_id;

    var tag_object = document.getElementById( tag_id );
    
    if( tag_object.hasChildNodes() == true )
	{
	  	tag_object.innerHTML = '';
	}else{
	}

    for(var i = 0; i < value_population.length ; i++) {  
        
        var tag_option  = document.createElement("option");
        var att_value   = document.createAttribute("value");
        att_value.value = value_population[i];
        tag_option.setAttributeNode(att_value);
        
        if (value_population[i] == default_value )
        {
            var att_selected = document.createAttribute("selected");
            att_selected.value = "selected" ;
            tag_option.setAttributeNode(att_selected);
        }
        
        var label_value = document.createTextNode( desc_population[i] )
        tag_option.appendChild(label_value)
        
       // option_dropdown =   document.getElementById( tag_id );
       document.getElementById(tags_id).appendChild( tag_option );  
    }   
    
    return option_dropdown;
  }
  else {
  	return false;
  }
}

function create_calendar( calendar_name , calendar_output_id  , calendar_title , start_date , end_date , default_date ) {
	
	var split_default_date = default_date.split("/");
	var month  = split_default_date [0];
    var ndate = split_default_date [1]-1;
    var year = split_default_date [2];
    var default_page = month + '/' + year;
	
	YAHOO.namespace('Directrooms.calendar');
	YAHOO.Directrooms.calendar.calContainer = new YAHOO.widget.CalendarGroup( 	
																							calendar_name 		, 
																							calendar_output_id 	, 
																									{ pages   		:	2	, 
																							          title			: 	calendar_title	, 
																							          close			:	true 			, 
																							          iframe 			: 	true		, 
																							          locale_weekdays	:	"1char" 	,  
																							          navigator		: 	false			,
																							          selected		: 	default_date	,
																							          pagedate      :   default_page    ,
																							          mindate		: 	start_date 	    , 
																							          maxdate		: 	end_date    
																									});
										
	return ( YAHOO.Directrooms.calendar.calContainer );
}

function create_next_date_string( begin_date_string , number_next_date ) {
    
    var split_date_string = begin_date_string.split("-");
    var year  = split_date_string[0];
    var month = split_date_string[1]-1;
    var ndate = split_date_string[2];
    
    next_date_object = new Date(year, month, ndate);
    next_date_object.setDate( next_date_object.getDate() + number_next_date );
    
    var array_month = (next_date_object.getMonth()+1);
    var array_day   = next_date_object.getDate();
    
    var next_month      = ( ( array_month < 10) ? "0" : "") + array_month;
    var next_day        = ( ( array_day   < 10) ? "0" : "") + array_day;
        
    var next_date_string = ( next_date_object.getFullYear() ) + "-" + next_month  + "-" + next_day;

    return next_date_string;
}

function dom_create_dropdown( selector_name , selector_id ,  option_value , option_description , default_value , class_name ) {

    /**TAG SELECT**/
    var tag_select = document.createElement("select");
    
    var att_name = document.createAttribute("name");
    att_name.value = selector_name ;
    tag_select.setAttributeNode(att_name);

    var att_id = document.createAttribute("id");
    att_id.value = selector_id;
    tag_select.setAttributeNode(att_id);
    
    var att_class = document.createAttribute("class");
    att_class.value = class_name;
    tag_select.setAttributeNode(att_class);
    
    var att_size = document.createAttribute("size");
    att_size.value = "1";
    tag_select.setAttributeNode(att_size);
    
    var option_count = option_value.length;


    for( var current_option = 0; current_option < option_count ; current_option++ ) {   

        /**TAG OPTION**/
        var tag_option = document.createElement("option");
        var att_value  = document.createAttribute("value");
        att_value.value = option_value[current_option];
        tag_option.setAttributeNode(att_value);
        
        if (option_value[current_option] == default_value) {
            
            var att_selected = document.createAttribute("selected");
            att_selected.value = "selected" ;
            tag_option.setAttributeNode(att_selected);
        }
    
        var label_value = document.createTextNode( option_description[current_option] );

        tag_option.appendChild(label_value);
        tag_select.appendChild(tag_option);

     }
     
    return tag_select;
}

/*
# # # #											# # # #
# #		Check exist and correct format value	 	# #
# # # #											# # # #
*/

function check_exist_date_value( date_value , start_default_date_value ){
	

	var default_date_value;
	var dateStrRegExp = /^\d{4}(-)\d{2}(-)\d{2}$/;
	
	var end_date_string           = create_next_date_string( start_default_date_value , 365);
	var default_start_date_object = create_date_object_from_string( start_default_date_value);
	var default_end_date_object   = create_date_object_from_string( end_date_string );
	var new_default_date_value    = create_next_date_string( start_default_date_value , 3 );

	if (    (date_value == "")
	     || (date_value == null)   
		 || (date_value == "undefined" )
		 || (dateStrRegExp.test(date_value) == false)
		 || (new_default_date_value < default_start_date_object)
		 || (new_default_date_value > default_end_date_object)
	   )
	{
		default_date_value = new_default_date_value;
	}
	else{
		default_date_value = date_value;
	}
	return default_date_value;
}

function check_exist_room_value( room_value ){
	
	var exist_room_value;
	var roomIntRegExp = /^\d{1}$/
	
	if (    ( room_value == "")
		 || ( room_value == null)  
		 || ( room_value == "undefined" )
		 || ( roomIntRegExp.test(room_value) == false)
		 || ( (room_value < 1) || (room_value > 5 )) 
	   )
	{
		exist_room_value = false;
	}
	else{
		exist_room_value = true;
	}
	
	return exist_room_value;
}

function check_exist_room_adult_value( room_value , array_adult_value ){

	var exist_room_adult_value = true;
	var adultIntRegExp = /^\d{1}$/
	
	for (var room=1 ; room <= room_value ; room++){
		if(   (array_adult_value[room-1] == null)
		   || (array_adult_value[room-1] == '')
		   || (array_adult_value[room-1] == 'undefined')
		   || (adultIntRegExp.test(array_adult_value[room-1]) == false)
		   || ((array_adult_value[room-1] < 1) || ( array_adult_value[room-1] > 4) )
		  )
		{
			exist_room_adult_value =  false;
			return exist_room_adult_value;
			
		}	
		else
		{
			exist_room_adult_value = true;
		}
	}
	
	return exist_room_adult_value;
}

function check_exist_room_child_value( room_value , array_child_value ){

	var exist_room_child_value = true;
	var childIntRegExp = /^\d{1}$/
	
	for (var room=1 ; room <= room_value ; room++){
		if(   (array_child_value[room-1] == null)
		   || (array_child_value[room-1] == '')
		   || (array_child_value[room-1] == 'undefined')
		   || (childIntRegExp.test(array_child_value[room-1]) == false)
		   || ((array_child_value[room-1] < 0) || ( array_child_value[room-1] > 2) )
		  )
		{
			exist_room_child_value =  false;
			return exist_room_child_value;
			
		}	
		else
		{
			exist_room_child_value = true;
		}
	}
	
	return exist_room_child_value;
}
function initial_new_room_child_value( default_room_value , child_value ){
	
	array_new_child_value = new Array();
	
	for(var room = 1 ; room <= default_room_value ; room ++ ){
	
		var room_child_value = child_value[room-1];
		var childIntRegExp = /^\d{1}$/
		
		if(   (room_child_value == null)
		   || (room_child_value == '')
		   || (room_child_value == 'undefined')
		   || (childIntRegExp.test(room_child_value) == false)
		   || ((room_child_value < 0) || (room_child_value > 2)) 
		 ){
		 	array_new_child_value.push('0');
		 }
		else
		{
			array_new_child_value.push(room_child_value);
		}			
	}

	return 	array_new_child_value;
}


function initial_new_room_adult_value( default_room_value , adult_value ){
	
	array_new_adult_value = new Array();
	
	for(var room = 1 ; room <= default_room_value ; room ++ ){
	
		var room_adult_value = adult_value[room-1];
		var adultIntRegExp = /^\d{1}$/
		
		if(   (room_adult_value == null)
		   || (room_adult_value == '')
		   || (room_adult_value == 'undefined')
		   || (adultIntRegExp.test(room_adult_value) == false)
		   || ((room_adult_value < 1) || (room_adult_value > 4)) 
		 ){
		 	array_new_adult_value.push('2');
		 }
		else
		{
			array_new_adult_value.push(room_adult_value); 
		}			
	}

	return 	array_new_adult_value;
}	

function check_exist_date_calendar( default_date , start_date ){


	var new_default_date_calendar;
	var invalidDateRegExp = /^01\/01\/1970$/;
	
	if(    (default_date == "") 
		|| (default_date == null)
		|| (invalidDateRegExp.test(default_date) == true)
	  )
	{
		
		var split_date = start_date.split("-");

		var year  = split_date[0];
		var month  = split_date[1];
		var ndate = split_date[2];
		
		new_default_date_calendar = 	month + "/" + ndate + "/" + year;
	}
	else{
		new_default_date_calendar = default_date;
	}
	

	return new_default_date_calendar;
}
function create_date_cal_format( date_string )
{

    var split_date = date_string.split("-");
    var year    = split_date[0];
    var month   = split_date[1];
    var ndate   = split_date[2];
    
    var date_cal_format = 	month + "/" + ndate + "/" + year;
    return date_cal_format;
}		
		
