function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}


function getCookie(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);

		if (begin == -1) {
			begin = dc.indexOf(prefix);

			if (begin != 0)
				return null;
		} else
			begin += 2;

		var end = document.cookie.indexOf(";", begin);

		if (end == -1)
			end = dc.length;

		return unescape(dc.substring(begin + prefix.length, end));
}

function initialiseDatePickers() {
   // Attempt to grab the datePicker objects
   var sd = datePickerController.datePickers["datefrom"];
   var ed = datePickerController.datePickers["dateto"];

   // For Internet Explorer: If they are not created then call this function 500 milliseconds later
   if(!sd || !ed) {
      setTimeout("initialiseDatePickers()", 200);
      return;
   }

   // Reset the low ranges to be today for both the datePickers
   var today = new Date();
   today = String(today.getFullYear()) + makeTwoChars(today.getMonth()+1) + makeTwoChars(today.getDate());

//   sd.setRangeHigh( today );
//   ed.setRangeHigh( today );

   // Clear any old values from the inputs (that might be cached by the browser after a page reload)
   //document.getElementById("datefrom").value = "";
   //document.getElementById("dateto").value = "";

   // Add the onchange event handler to the start date input
   document.getElementById("datefrom").onchange = setReservationDates;
}

function setReservationDates(e) {
   // Check the associated datePicker object is available (be safe)
   if(!("datefrom" in datePickerController.datePickers)) {
      return;
   }

   // Check the value of the input is a date of the correct format
   var dt = datePickerController.dateFormat(this.value, datePickerController.datePickers["datefrom"].format.charAt(0) == "m");

   // If the input's value cannot be parsed as a valid date then return
   if(dt == 0) return;

   // Grab the value set within the endDate input and parse it using the dateFormat method
   // N.B: The second parameter to the dateFormat function, if TRUE, tells the function to favour the m-d-y date format
   var edv = datePickerController.dateFormat(document.getElementById("dateto").value, datePickerController.datePickers["dateto"].format.charAt(0) == "m");

   // Grab the end date datePicker Objects
   var ed = datePickerController.datePickers["dateto"];

   ed.setRangeLow( dt );

   // If theres a value already present within the end date input and it's smaller than the start date
   // then clear the end date value
   if(edv < dt) {
      document.getElementById("dateto").value = "";
   }
}

function addZero(value)
{
	if (parseInt(value)<10) return '0'+value;
	else return value;
}
function makeTwoChars(inp) {
   return String(inp).length < 2 ? "0" + inp : inp;
}
function PresetDateRange(type, form, dp_from_id, dp_to_id)
{
	dt = new Date();
	var sd = $(dp_from_id);
	var ed = $(dp_to_id);
	switch(type)
	{
		case 'all':
		sd.value = ed.value = '';
		
		break;
		case 'today':
		sd.value = ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		
		break;
		case 'yesterday':
		dt  = dt.subtractDays(1);
		sd.value = ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		sd.value = ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		
		break;
		case '7days':
		ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		dt = dt.subtractWeeks(1);
		sd.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		
		break;
		case 'month':
		ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		dt.setDate(1);
		sd.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		
		break;
		case 'lastmonth':
		dt = new Date(dt.subtractMonths(1));
		dt.setDate(1);
		sd.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		dt = dt.lastDayOfMonth();
		ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		break;
		
		case 'year':
		sd.value = dt.getFullYear()+'-01-01';
		ed.value = dt.getFullYear()+'-'+addZero(dt.getMonth()+1)+'-'+addZero(dt.getDate());
		break;
		
		case 'lastyear':
		sd.value = parseInt(dt.getFullYear())-1+'-01-01';
		ed.value = parseInt(dt.getFullYear())-1+'-12-31';
		break;
	}
}

function alertWindow(message, w_width, w_height)
{
	if(typeof(w_width) == 'undefined') w_width=300;
	if(typeof(w_height) == 'undefined') w_height=100;
	Dialog.alert(message, 
             {width:w_width, height:w_height, okLabel: "Закрыть", className: "alphacube", 
              ok:function(win) {return true;}});
}

function confirmWindow(text, ok_callback)
{
	Dialog.confirm(text, 
               {width:300, okLabel: "Да", cancelLabel:"Нет", className: "alphacube",
               buttonClass: "myButtonClass",
               id: "myDialogId",
               cancel:function(win) { },
               ok:ok_callback
              });
}

function popUpWindow(URLStr, left, top, width, height)
{
	if(popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top);
}


function getRadioGroupValue(objects)
{
	for (var i=0; i<objects.length;i++)
		if (objects[i].checked)
			return objects[i].value;
	return false;
}

function setRadioGroupValue(objects, value)
{
	for (var i=0; i<objects.length;i++)
		if (objects[i].value==value)
			objects[i].checked=true;
}

Date.prototype.getAge = function()
{
	var d=Now();
	age=d.getFullYear()-this.getFullYear();
	if (d.getMonth()<this.getMonth() || (d.getMonth()==this.getMonth() && d.getDate()<this.getDate()) )
		age--;
	return age;
}

function swf_init(swf_path, files_id, url)
{
	var so4004 = new SWFObject(swf_path,"player"+files_id,"104","104","7");
	so4004.addParam("allowfullscreen","true");
	
	so4004.addVariable("file",url);
	so4004.addVariable("image",url);
	so4004.addVariable("displayheight","104");
	so4004.addVariable("autostart","false");
	so4004.addVariable("repeat","true");
	so4004.addVariable("showdigits","false");
	so4004.addVariable("showicons","false");
	so4004.addVariable("showvolume","false");
	so4004.write("player"+files_id);
}
function sendRate(mode,nrate, mat_id, blogs_id, files_id,things_id, objects_id, countries_id)
{	
	if(mode==4)
	{
		var vals=$H({'rate':nrate, 'mat_id':mat_id, 'blogs_id':blogs_id, 'files_id':files_id, 'things_id':things_id, 'objects_id':objects_id, 'countries_id':countries_id}).toQueryString();
		getRequest({form:'rate', vals: vals},'forms.php');
	}
} 

function sendFriend(type, id, title, url)
{
	$('sendfriend_title').innerHTML=title;
	if(typeof(url) == 'undefined') $('sendfriend_url').value=document.location.href;
	else $('sendfriend_url').value=url;
	/*Dialog.confirm($('sendfriend_div').innerHTML, 
               {width:400, okLabel: "Сохранить", cancelLabel:"Отмена", className: "alphacube",
               buttonClass: "myButtonClass",
               id: "myDialogId",
               cancel:function(win) { },
               ok:function (win) {
               		var name=$('newfolder_name').value;
               		if (name=='')
               		{
               			$('newfolder_name_required').style.visibility='visible';
               			return;
               		}
               		var vals = $H({'id':gal_id, 'name': name}).toQueryString();
					var res=getRequest({'form': 'gallery.edit', 'vals': vals }); 
					return true;
					//document.location.href='index.users.php?action=users.edit&'+getTypeByTab(tab)+'_folder='+gal_id+'&tab='+tab;
              }
              });*/
	Dialog.info($('sendfriend_div').innerHTML,
        {width:400, height:230, className: "alphacube"});
        
     forForms['sendfriend'] = new Validation('sendfriend',{stopOnFirst:false, immediate : false, onFormValidate: submitForm});
              
	
	//document.location.href='index.php?action=sendfriend&id='+id+'&type='+type+'&url='+escape(document.location.href);
}

		function myButtonOnClick(){
			$$('select').each(function (el) {
				el.style.visibility='hidden';
			});
    	    var so = new SWFObject("flash/flashmap_final.swf", "map", "100%", "100%", "6");
    	    so.addParam("scale", "noscale");
			so.addParam("wmode", "transparent")
			$('flashcontent').style.visibility='visible';
    	    so.write("flashcontent");
}

function clearFlash(){
			$$('select').each(function (el) {
				el.style.visibility='visible';
			});
			$('flashcontent').style.visibility='hidden';
		}
		
function SearchByWord()
{
	var word=$('cl1').value;
	var country_id=$('search_countries_id').value;
	if (word.length!=0)
		document.location.href='index.php?action=search&word='+word+'&countries_id='+country_id;	
	else if (country_id)
	{
		document.location.href='index.php?action=countries.details&id='+country_id;
	}
}

