/*
 *
 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 2.2.4
 * Demo: http://www.texotela.co.uk/code/jquery/select/
 *
 * $LastChangedDate: 2008-06-17 17:27:25 +0100 (Tue, 17 Jun 2008) $
 * $Rev: 5727 $
 *
 */
;(function(h){h.fn.addOption=function(){var j=function(a,f,c,g){var d=document.createElement("option");d.value=f,d.text=c;var b=a.options;var e=b.length;if(!a.cache){a.cache={};for(var i=0;i<e;i++){a.cache[b[i].value]=i}}if(typeof a.cache[f]=="undefined")a.cache[f]=e;a.options[a.cache[f]]=d;if(g){d.selected=true}};var k=arguments;if(k.length==0)return this;var l=true;var m=false;var n,o,p;if(typeof(k[0])=="object"){m=true;n=k[0]}if(k.length>=2){if(typeof(k[1])=="boolean")l=k[1];else if(typeof(k[2])=="boolean")l=k[2];if(!m){o=k[0];p=k[1]}}this.each(function(){if(this.nodeName.toLowerCase()!="select")return;if(m){for(var a in n){j(this,a,n[a],l)}}else{j(this,o,p,l)}});return this};h.fn.ajaxAddOption=function(c,g,d,b,e){if(typeof(c)!="string")return this;if(typeof(g)!="object")g={};if(typeof(d)!="boolean")d=true;this.each(function(){var f=this;h.getJSON(c,g,function(a){h(f).addOption(a,d);if(typeof b=="function"){if(typeof e=="object"){b.apply(f,e)}else{b.call(f)}}})});return this};h.fn.removeOption=function(){var d=arguments;if(d.length==0)return this;var b=typeof(d[0]);var e,i;if(b=="string"||b=="object"||b=="function"){e=d[0];if(e.constructor==Array){var j=e.length;for(var k=0;k<j;k++){this.removeOption(e[k],d[1])}return this}}else if(b=="number")i=d[0];else return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return;if(this.cache)this.cache=null;var a=false;var f=this.options;if(!!e){var c=f.length;for(var g=c-1;g>=0;g--){if(e.constructor==RegExp){if(f[g].value.match(e)){a=true}}else if(f[g].value==e){a=true}if(a&&d[1]===true)a=f[g].selected;if(a){f[g]=null}a=false}}else{if(d[1]===true){a=f[i].selected}else{a=true}if(a){this.remove(i)}}});return this};h.fn.sortOptions=function(e){var i=h(this).selectedValues();var j=typeof(e)=="undefined"?true:!!e;this.each(function(){if(this.nodeName.toLowerCase()!="select")return;var c=this.options;var g=c.length;var d=[];for(var b=0;b<g;b++){d[b]={v:c[b].value,t:c[b].text}}d.sort(function(a,f){o1t=a.t.toLowerCase(),o2t=f.t.toLowerCase();if(o1t==o2t)return 0;if(j){return o1t<o2t?-1:1}else{return o1t>o2t?-1:1}});for(var b=0;b<g;b++){c[b].text=d[b].t;c[b].value=d[b].v}}).selectOptions(i,true);return this};h.fn.selectOptions=function(g,d){var b=g;var e=typeof(g);if(e=="object"&&b.constructor==Array){var i=this;h.each(b,function(){i.selectOptions(this,d)})};var j=d||false;if(e!="string"&&e!="function"&&e!="object")return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;var a=this.options;var f=a.length;for(var c=0;c<f;c++){if(b.constructor==RegExp){if(a[c].value.match(b)){a[c].selected=true}else if(j){a[c].selected=false}}else{if(a[c].value==b){a[c].selected=true}else if(j){a[c].selected=false}}}});return this};h.fn.copyOptions=function(g,d){var b=d||"selected";if(h(g).size()==0)return this;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;var a=this.options;var f=a.length;for(var c=0;c<f;c++){if(b=="all"||(b=="selected"&&a[c].selected)){h(g).addOption(a[c].value,a[c].text)}}});return this};h.fn.containsOption=function(g,d){var b=false;var e=g;var i=typeof(e);var j=typeof(d);if(i!="string"&&i!="function"&&i!="object")return j=="function"?this:b;this.each(function(){if(this.nodeName.toLowerCase()!="select")return this;if(b&&j!="function")return false;var a=this.options;var f=a.length;for(var c=0;c<f;c++){if(e.constructor==RegExp){if(a[c].value.match(e)){b=true;if(j=="function")d.call(a[c],c)}}else{if(a[c].value==e){b=true;if(j=="function")d.call(a[c],c)}}}});return j=="function"?this:b};h.fn.selectedValues=function(){var a=[];this.selectedOptions().each(function(){a[a.length]=this.value});return a};h.fn.selectedTexts=function(){var a=[];this.selectedOptions().each(function(){a[a.length]=this.text});return a};h.fn.selectedOptions=function(){return this.find("option:selected")}})(jQuery);


// Simulates PHP's date function
Date.prototype.format=function(format){var returnStr='';var replace=Date.replaceChars;for(var i=0;i<format.length;i++){var curChar=format.charAt(i);if(replace[curChar]){returnStr+=replace[curChar].call(this);}else{returnStr+=curChar;}}return returnStr;};Date.replaceChars={shortMonths:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],longMonths:['January','February','March','April','May','June','July','August','September','October','November','December'],shortDays:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],longDays:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],d:function(){return(this.getDate()<10?'0':'')+this.getDate();},D:function(){return Date.replaceChars.shortDays[this.getDay()];},j:function(){return this.getDate();},l:function(){return Date.replaceChars.longDays[this.getDay()];},N:function(){return this.getDay()+1;},S:function(){return(this.getDate()%10==1&&this.getDate()!=11?'st':(this.getDate()%10==2&&this.getDate()!=12?'nd':(this.getDate()%10==3&&this.getDate()!=13?'rd':'th')));},w:function(){return this.getDay();},z:function(){return"Not Yet Supported";},W:function(){return"Not Yet Supported";},F:function(){return Date.replaceChars.longMonths[this.getMonth()];},m:function(){return(this.getMonth()<9?'0':'')+(this.getMonth()+1);},M:function(){return Date.replaceChars.shortMonths[this.getMonth()];},n:function(){return this.getMonth()+1;},t:function(){return"Not Yet Supported";},L:function(){return(((this.getFullYear()%4==0)&&(this.getFullYear()%100!=0))||(this.getFullYear()%400==0))?'1':'0';},o:function(){return"Not Supported";},Y:function(){return this.getFullYear();},y:function(){return(''+this.getFullYear()).substr(2);},a:function(){return this.getHours()<12?'am':'pm';},A:function(){return this.getHours()<12?'AM':'PM';},B:function(){return"Not Yet Supported";},g:function(){return this.getHours()%12||12;},G:function(){return this.getHours();},h:function(){return((this.getHours()%12||12)<10?'0':'')+(this.getHours()%12||12);},H:function(){return(this.getHours()<10?'0':'')+this.getHours();},i:function(){return(this.getMinutes()<10?'0':'')+this.getMinutes();},s:function(){return(this.getSeconds()<10?'0':'')+this.getSeconds();},e:function(){return"Not Yet Supported";},I:function(){return"Not Supported";},O:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+'00';},P:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+':'+(Math.abs(this.getTimezoneOffset()%60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()%60));},T:function(){var m=this.getMonth();this.setMonth(0);var result=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,'$1');this.setMonth(m);return result;},Z:function(){return-this.getTimezoneOffset()*60;},c:function(){return this.format("Y-m-d")+"T"+this.format("H:i:sP");},r:function(){return this.toString();},U:function(){return this.getTime()/1000;}};


// Copyright 2009 Swiftrank
function removeOption(itemId,option)
{
	if(option == 'all')
	{
		$('#'+itemId).removeOption(/./);
	}
	else
	{
		$('#'+itemId).removeOption(option);
	}
}


function addOption(itemId,options)
{
	$('#'+itemId).addOption(options,false);
}

function selectOption(itemId, options)
{
	$('#'+itemId).selectOptions(options);
}

function getSelectedOptions(itemId)
{
	return $('#'+itemId).selectedValues();
}

function daysInMonth(month,year) {
	var m = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (month != 2) return m[month - 1];
	if (year%4 != 0) return m[1];
	if (year%100 == 0 && year%400 != 0) return m[1];
	return m[1] + 1;
} 

function changeDays(itemId, changeItem, limitType)
{
	
	var Inargs = ($('#'+itemId).val()).split('-');
	var lastDayOfMonth = daysInMonth(Inargs[1],Inargs[0]);
	var daysArray = new Array();
	for(var i=1;i<=lastDayOfMonth;i++)
	{
		daysArray[i] = i; 
	}
	var OldValue = $('#'+changeItem).val();
	
	removeOption(changeItem,"all");
	
	addOption(changeItem,daysArray);
	
	limitDays(changeItem,itemId,limitType);
	

	if($('#'+changeItem).containsOption(OldValue))
	{
		selectOption(changeItem,OldValue);
	}
	else
	{
		selectOption(changeItem,String(lastDayOfMonth));
	}
	
}

function limitDays(itemId,monthYearField, type)
{
	var now = new Date();
	var removeArray = new Array();
	if(now.format("Y-m") == $('#'+monthYearField).val())
	{
		var workTill = now.format("d");
		
		if(type == 1)
		{
			workTill++;
		}
		
		for(var i=0;i< workTill;i++)
		{	
			removeArray[i] = String(i);
		}
		removeOption(itemId,removeArray);
			
	}
}

function contains(item,option)
{
	if($('#'+item).containsOption(option))
	{
		return true;
	}
	return false;
}

function getCurrentDate(itemDay, itemMonth)
{
	var Inargs = ($('#'+itemMonth).val()).split('-');

	var returnDate = new Date(Inargs[0],Inargs[1]-1  ,$('#'+itemDay).val());

	return Date.parse(returnDate);
}


function changeSelectboxes(itemDay, itemMonth, date, limitType)
{
		
	selectOption(itemMonth,String(date.format("Y-n")));
	
	changeDays(itemMonth,itemDay,limitType);
	selectOption(itemDay,String(date.format("j")));
}


function calculatePreviousDate(date)
{
	var previous = date - 86400000;
	return new Date(previous);
}

function calculateNextDate(date)
{
	var next = date + 86400000;
	return new Date(next);
}

function changeCheckIn(checkInItemDay, checkOutItemDay,checkInItemMonth, checkOutItemMonth)
{
	var checkInDate = getCurrentDate(checkInItemDay,checkInItemMonth);
	var checkOutDate = getCurrentDate(checkOutItemDay,checkOutItemMonth);
	//changeSelectboxes(checkOutItemDay,checkOutItemMonth,new Date(checkOutDate),1);
	if(checkInDate >= checkOutDate)
	{
		var newInDate = calculatePreviousDate(checkOutDate);
		if(contains(checkInItemMonth,newInDate.format("Y-n")))
		{
			changeSelectboxes(checkInItemDay,checkInItemMonth,newInDate,false);
		}
		else
		{
			var newOutDate = calculateNextDate(checkOutDate);
			var newInDate = new Date(checkOutDate);
			changeSelectboxes(checkOutItemDay,checkOutItemMonth,newOutDate,1);
			changeSelectboxes(checkInItemDay,checkInItemMonth,newInDate,false);
		}
	}
	
	

}

function lastCheck(checkInItemDay, checkOutItemDay,checkInItemMonth, checkOutItemMonth)
{

	var checkInDate = getCurrentDate(checkInItemDay,checkInItemMonth);
	var checkOutDate = getCurrentDate(checkOutItemDay,checkOutItemMonth);
	
	if(checkInDate >= checkOutDate)
	{
		var newInDate = calculatePreviousDate(checkOutDate);
		changeSelectboxes(checkInItemDay,checkInItemMonth,newInDate,false);
	}

}

function changeCheckOut(checkInItemDay, checkOutItemDay,checkInItemMonth, checkOutItemMonth)
{
	var checkInDate = getCurrentDate(checkInItemDay,checkInItemMonth);
	var checkOutDate = getCurrentDate(checkOutItemDay,checkOutItemMonth);
	//changeSelectboxes(checkInItemDay,checkInItemMonth,new Date(checkInDate),false);
	if(checkInDate >= checkOutDate)
	{
		var newOutDate = calculateNextDate(checkInDate);
		if(contains(checkOutItemMonth,newOutDate.format("Y-n")))
		{
			changeSelectboxes(checkOutItemDay,checkOutItemMonth,newOutDate,false);
		}
		else
		{
			var newInDate = calculatePreviousDate(checkInDate);
			var newOutDate = new Date(checkInDate);
			changeSelectboxes(checkOutItemDay,checkOutItemMonth,newOutDate,1);
			changeSelectboxes(checkInItemDay,checkInItemMonth,newInDate,false);
		}
	}
}


