<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 * Create a Datepicker input object 
 *
 * @author Maggie Magpie
 */
class DatePicker extends BootstrapInput {
	protected $output;
	
	/*
	 * String. Default: “mm/dd/yyyy”
	 * The date format, combination of d, dd, D, DD, m, mm, M, MM, yy, yyyy.
	 * d, dd: Numeric date, no leading zero and leading zero, respectively. Eg, 5, 05.
	 * D, DD: Abbreviated and full weekday names, respectively. Eg, Mon, Monday.
	 * m, mm: Numeric month, no leading zero and leading zero, respectively. Eg, 7, 07.
	 * M, MM: Abbreviated and full month names, respectively. Eg, Jan, January
	 * yy, yyyy: 2- and 4-digit years, respectively. Eg, 12, 2012.
	 */
	public $format = 'dd/mm/yyyy';
	
	/**
	 * 
	 * @param string $name name of Datepicker control
	 * @param mixed $value a string date (i.e. 23/07/1987 or a Unix Timestamp)
	 * @param string $label
	 */
	public function __construct( $name, $value = null, $label = '' ) {
		$this->name = $name;
		$this->id = $name;
		$this->label = $label;
		$this->format = _DEFAULT_DATEPICKER_DATE_FORMAT;
		
		
		if( is_null( $value ) == false && is_numeric( $value ) == true && is_int( (int)$value ) == true ){
			$this->setTimestamp( $value );
		}else{
			$this->setValue( $value );
		}
			
	}
	
	
	public function setTimestamp( $timestamp ){
		$date = Data::UnixTimestampToDate( $timestamp, _DEFAULT_DATE_SHORT_FORMAT );
		$this->setValue( $date );
	}
	

	public function output() {
		if( empty( $this->placeholder ) == true ){
			$this->placeholder =  $this->format;
		}
			
		$this->output = parent::output();

		$this->output .= "<script>
						$(document).ready(function(){
						  var date_input=$('input[name=\"" . $this->name . "\"]');
						  var container=$('.bootstrap-iso form').length>0 ? $('.bootstrap-iso form').parent() : \"body\";
						  var options={
							format: '" . $this->format . "',
							// container: container,
							todayHighlight: true,
							autoclose: true,
						  };
						  date_input.datepicker(options);
						});
						
						// add auto slashes to date
						$('input[name=\"" . $this->name . "\"]').keyup(function (e) {
							var textSoFar = $(this).val();
							if (e.keyCode != 191) {
								if (e.keyCode != 8) {
									if (textSoFar.length == 2 || textSoFar.length == 5) {
										$(this).val(textSoFar + '/');
									}
										//to handle copy & paste of 8 digit
									else if (e.keyCode == 86 && textSoFar.length == 8) {
										$(this).val(textSoFar.substr(0, 2) + '/' + textSoFar.substr(2, 2) + '/' + textSoFar.substr(4, 4));
									}
								}
								else {
									//backspace would skip the slashes and just remove the numbers
									if (textSoFar.length == 5) {
										$(this).val(textSoFar.substring(0, 4));
									}
									else if (textSoFar.length == 2) {
										$(this).val(textSoFar.substring(0, 1));
									}
								}
							}
							else {
								//remove slashes to avoid 12//01/2014
								$(this).val(textSoFar.substring(0, textSoFar.length - 1));
							}
						});
					</script>";
		
		return $this->output;
	}
}
