// JavaScript Document
(
	function(){
		var UA=navigator.userAgent;
		is_ie=/msie/i.test(UA)&&!(/opera/i.test(UA))&&!(/mac_powerpc/i.test(UA));
		is_khtml=/Konqueror|Safari|KHTML/i.test(UA);
		is_gecko=/gecko/i.test(UA)&&!is_khtml;
	}
)();

function roll(a){if (a.className.substr((a.className.length-1),1) == 'n') {b='o';}else {b='n';}a.className=a.className.substr(0,(a.className.length-1))+b;}

function rollId(a) {
	if (a.id.substr((a.id.length-1),1) == 'n') {b='o';}
	else {b='n';}
	a.id = a.id.substr(0,(a.id.length-1))+b;
}

function rollimg(a) {
	a.style.cursor = 'pointer';
	ext = a.src.substr((a.src.length-4),4);
	state = a.src.substr((a.src.length-6),2);
	if (state == '_o')
		a.src = a.src.substr(0,(a.src.length-6))+ext;
	else
		a.src = a.src.substr(0,(a.src.length-4))+'_o'+ext;
}

function delConfurm() {
	a = confirm('ARE YOU SURE YOU WANT TO DELETE THIS RECORDS?');
	if(a) {
		return true;
	}
	else {
		return false;
	}
}

function setInput(a,b) {
	document.getElementById(b).value = a;
	return true;
}

function request_ok(a) {
	var b = request_ok.arguments;
	var formname = a.form.name;
	for(var i=1;i<b.length;i++) {
		var c = '';
		c = eval('a.form.'+b[i]+'.value');
		if(c == '' || c == 'fill this' || c == '--') {
			if(eval('a.form.'+b[i]+'.tagName')=='SELECT')
				eval("a.form."+b[i]+".focus()");
			else {
				eval("a.form."+b[i]+".value = 'fill this'");
				eval("a.form."+b[i]+".select()");
			}
			return false;
		}
	}
	return true;
}

function check_numbers_input(a) {
	b = '';
	for (i=0;i<a.value.length;i++) {
		if ( (!isNaN(a.value.substr(i,1)) && a.value.substr(i,1) != ' ') || (a.value.substr(i,1) == '.' && a.value.indexOf('.')==i) ) {
			b += a.value.substr(i,1);
		}
	}
	if(a.value != b)
		a.value = b;
}

function check_dates(a,dateA,dateB,dateAtitle,dateBtitle,same) {
	data_A = document.getElementById(dateA).value.replace(/-/g, "/");
	dateObjA = new Date(data_A);
	data_B = document.getElementById(dateB).value.replace(/-/g, "/");
	dateObjB = new Date(data_B);
	if(same==true) {
		if(dateObjA > dateObjB) {
			alert('"'+dateAtitle+'" must be before or same as "'+dateBtitle+'" !');
			return false;
		}
	}
	else {
		if(dateObjA >= dateObjB) {
			alert('"'+dateAtitle+'" must be before "'+dateBtitle+'" !');
			return false;
		}
	}
	return true;
}

function define_one_day(a,dateA,dateB) {
	var today = new Date();
	var data_A = document.getElementById(dateA).value.replace(/-/g, "/");
	var dateObjA = new Date(data_A);
	if(dateObjA < today) {
		dateObjA = today;
		var A_month = dateObjA.getMonth()+1;
		A_month = A_month<=9 ? '0'+A_month.toString() : A_month.toString();
		var A_day = dateObjA.getDate();
		A_day = A_day<=9 ? '0'+A_day.toString() : A_day.toString();
		var A_year = dateObjA.getFullYear();
		document.getElementById(dateA).value = A_month +'-'+ A_day +'-'+ A_year;
	}
	data_B = document.getElementById(dateB).value.replace(/-/g, "/");
	dateObjB = new Date(data_B);
	if(dateObjA >= dateObjB) {
		var new_dateB = dateObjA.setDate(dateObjA.getDate()+1);
		var new_dateObjB = new Date(new_dateB);
		var B_month = new_dateObjB.getMonth()+1;
		B_month = B_month<=9 ? '0'+B_month.toString() : B_month.toString();
		var B_day = new_dateObjB.getDate();
		B_day = B_day<=9 ? '0'+B_day.toString() : B_day.toString();
		var B_year = new_dateObjB.getFullYear();
		document.getElementById(dateB).value = B_month +'-'+ B_day +'-'+ B_year;
	}
}

/*--------[ SELECT tag ]-------------*/
function addAmenity(a) {
	var listField = a.form.amenities_selected;
	var testField = a.form.amenities_list;
	for(var i=0;i<testField.length;i++) {
		if(a.form.amenities_list.options[i].selected==true) {
			var newValue = a.form.amenities_list.options[i].value;
			var newText = amenity_list[newValue];
			addToSelect(listField, newText, newValue);
		}
	}
}

function delAmenity(a) {
	listField = a.form.amenities_selected;
	removeFromSelect(listField);
}

function sendAmenities(a) {
	amenities_list='';
	select_tag = a.form.amenities_selected;
		for(i=0;i<select_tag.length;i++)
			amenities_list += select_tag.options[i].value + ',';
	a.form.new_amenity_list.value = amenities_list;
}

function addToSelect(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
//      alert("You cannot add blank values!");
   } else {
      var exist=false;
		for(var i=0;i<listField.length;i++) {
			if(newValue==listField.options[i].value)
				exist=true;
		}
		if(!exist) {
			var len = listField.length++; // Increase the size of list and return the size
			listField.options[len].value = newValue;
			listField.options[len].text = newText;
			listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
		}
   } // Ends the check to see if the value entered on the form is empty
}

function removeFromSelect(listField) {
   if ( listField.length == -1) {  // If the list is empty
//      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
//         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}
/*--------[ SELECT tag end ]-------------*/

/*--------[ bubble help ]-------------*/
var Xoffset = -80;
var Yoffset = 20;
var timedelay, skn, b_height, b_width; 
var bubble_start_working = false;


function bubble_set() {
//document.body.innerHTML += '<div id="bubble" style="visibility:hidden; display:none; left:400px; top:200px; position:absolute; z-index:1;"></div>';
document.onmousemove = bubble_move;
	if (is_gecko)
		skn=document.getElementById("bubble").style;
	else if (is_ie)
		skn=document.all.bubble.style;
	skn.visibility="visible";
	skn.display="none";
	bubble_start_working = true;
}

function bubble(msg,width,height) {
	if(bubble_start_working) {
		var content = msg;
		if(is_gecko) {
			document.getElementById("bubble").innerHTML=content;
			skn.display='';
		}
		if(is_ie) {
			document.all("bubble").innerHTML=content;
			skn.display='';
		}
		bubble_obj=document.getElementById('bubble');
		b_height = bubble_obj.offsetHeight;
		b_width = bubble_obj.offsetWidth;
	}
}

function bubble_move(e) {
	if(bubble_start_working) {
		if(is_ie)
			e=event;
		var xScroll = (document.all) ? (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) : window.pageXOffset;
		var yScroll = (document.all) ? (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) : window.pageYOffset;
		var x = e.clientX + xScroll;
		var y = e.clientY + yScroll;
		var xBorder = document.body.clientWidth + xScroll;
		var yBorder = document.body.clientHeight + yScroll;

		var temp_x = x + Xoffset;
		if(temp_x < 1)
			temp_x = 1;
		if( (temp_x+b_width) > xBorder )
			temp_x = document.body.clientWidth - b_width;
		skn.left = temp_x + 'px';
		var temp_y = y + Yoffset;
		if( (temp_y+b_height) > yBorder )
			temp_y = y - b_height - 10;
		skn.top = temp_y + 'px';
	}
}

function bubble_kill() {
	if(bubble_start_working) {
		clearTimeout(timedelay);
		skn.display="none"
	}
}
/*--------[ bubble help end ]-------------*/

function pic_bubble(file) {
	content='<table width="110" height="110" border="0" cellpadding="0" cellspacing="0" style="background-color:#FFF5D7;border:solid 1px #660000;">';
	content+='<tr><td align="center" valign="middle"><img src="../sys/pic.php?img='+file+'&h=100&w=100&q=90" style="border:solid 1px #FFFFFF;" /></td></tr></table>';
	bubble(content,110,110);
}
function thumb_pic_bubble(name, size, date) {
	content='<table border="0" cellpadding="0" cellspacing="2" style="background-color:#FFF5D7;border:solid 1px #660000;font-size:10px;">';
	content+='<tr valign="top"><td>name:</td><td><b>'+name+'</b></td></tr><tr valign="top"><td>size:</td><td><b>'+size+'</b></td></tr><tr valign="top"><td>date:</td><td><b>'+date+'</b></td></tr></table>';
	bubble(content,220,110);
}

/*--------[ filemanager ]-------------*/
var selected_count = 0;
function switch_state(a) {
	c_name = a.id+'_chk';
	c = document.getElementById(c_name);
	if (a.className == 'filelist_o') {
		b = 'selected_o';
		c.checked = true;
		selected_count++;
	}
	else {
		b='filelist_o';
		c.checked = false;
		selected_count--;
	}
	a.className=b;
	document.forms['file_operations'].f_rename.disabled=(selected_count!=1 ? true : false);
	document.forms['file_operations'].f_move.disabled=(!selected_count ? true : false);
	document.forms['file_operations'].f_delete.disabled=(!selected_count ? true : false);
}

function gatherInputs(name) {
	if(!name || name=='') return false;
	var inputs = document.getElementsByTagName("input");
	checkboxes = Array();
	for(i in inputs){
        if (inputs[i].name != null && inputs[i].name.indexOf(name) == 0 && inputs[i].checked)
        {
			checkboxes[checkboxes.length] = inputs[i].value;
        }
	}
	return checkboxes;
}

function del_confurm() {
	if(confirm('You\'re about to delete an important thing !\nAre You sure ?')) {
		return true;
	}
	return false;
}

function ask_for_name(the_name) {
	if(!the_name || the_name=='') the_name = 'type_new_name';
	if( !(name = prompt('Type the name for the new DIR', the_name)) ) { return false; }
	if(name!=''&&name!=the_name) {
		if(name.indexOf('*') > -1 ||
		   name.indexOf('"') > -1 ||
		   name.indexOf('<') > -1 ||
		   name.indexOf('>') > -1 ||
		   name.indexOf("'") > -1 ||
		   name.indexOf('?') > -1) {
			alert('The name can\'t contain [<], [>], ["], [\'], [*] or [?] symbols !');
			name = ask_for_name(the_name);
		}
		return name;
	}
	return false;
}

function add_dir() {
	name = ask_for_name();
	if(name!=false && name!='false') {
		document.forms['dir_operations'].add_new_dir.value = name;
		document.forms['dir_operations'].submit();
	}
}

function del_dir() {
	if(del_confurm()) {
		document.forms['dir_operations'].del_curr_dir.value = 'yes';
		document.forms['dir_operations'].submit();
	}
}

function ren_dir(old_name) {
	name = ask_for_name(old_name);
	if(name!=false && name!='false') {
		document.forms['dir_operations'].ren_curr_dir.value = name;
		document.forms['dir_operations'].submit();
	}
}

function ren_file() {
	checkboxes = gatherInputs('filelist_chk');
	old_name = checkboxes[0];
	name = ask_for_name(old_name);
	if(name!=false && name!='false') {
		document.forms['file_operations'].ren_curr_file.value = name;
		return true;
	}
	return false;
}
// for file browser
function select_pic(a, path) {
	if(a=='')
		return false;
	opener.pic_holder.value=a;
	var prop = select_pic.arguments;
	if(prop.length > 2)
		opener.pic_show(a, path, prop[2]);
	else
		opener.pic_show(a, path);
	window.close();
}
// for file browser
function pic_show(picture) {
	var picObj = document.getElementById('picture_container');
	if(picture==''){
		picObj.innerHTML = "";
		return false;
	}
	var pic_path = "";
	var prop = pic_show.arguments;
	if(prop.length > 1)
		pic_path = (prop.length>2 ? '../sys/pic.php?'+prop[2]+'&q=90&img=' : '')+prop[1]; // "../sys/pic.php?w=120&h=120&q=50&img=../UserFiles/Image/";
	picObj.innerHTML = '<img src="' + pic_path + picture + '" />';
	return true;
}

var pic_holder;
var picBrowser;
function get_picture(a) {
	if (typeof(picBrowser) != 'undefined' && !(picBrowser.closed)) 
		picBrowser.close();
	pic_holder = a;
	var width = 820;
	var height = 400;
	var dir = '';
	var prop = get_picture.arguments;
	if(prop.length > 1)
		var dir = prop[1];
	picBrowser = window.open('file_browser.php?dir='+dir+(prop.length>2?'&pro='+prop[2]:''),'picBrowser', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width='+width+',height='+height+'');
}
/*--------[ filemanager end ]-------------*/

var popRes;
function pop_reservation(a) {
	if (typeof(popRes) != 'undefined' && !(popRes.closed)) 
		popRes.close();
	popRes = window.open('pop_reservation.php?r='+a,'popRes');
}

function print_round(a) {
	b = a.toString();
	c = b.split('.');
	d = c[0] +'.'+ (c.length>1 ? c[1]+(c[1].length<2 ? '0' : '') : '00');
	return d;
}

//=====================================================================
// IE win memory cleanup
//=====================================================================

if (window.attachEvent) {
	var cearElementProps = [
		'data',
		'onmouseover',
		'onmouseout',
		'onmousedown',
		'onmouseup',
		'ondblclick',
		'onclick',
		'onselectstart',
		'oncontextmenu'
	];

	window.attachEvent("onunload", function() {
        var el;
        for(var d = document.all.length;d--;){
            el = document.all[d];
            for(var c = cearElementProps.length;c--;){
                el[cearElementProps[c]] = null;
            }
        }
	});
}

var in_the_end = "done";


//================================================================
// site functions
//================================================================

function resetForm( a ) {
		var workForm = a.form;
		for(i=0; i<workForm.length; i++) {
			if(workForm[i].type != 'button' && workForm[i].type != 'submit') {
				if(workForm[i].tagName == 'INPUT')
					workForm[i].value = '';
				if(workForm[i].tagName == 'SELECT')
					workForm[i].options[0].selected = true;
			}
		}
}
