// JavaScript Document
// 2-20-2011 jason russo added some functions ..
var hh = {
	getAreas:function(e) {
		var id = $(e).options[$(e).selectedIndex].value;
		if(id != 0) {
			new Ajax.Request('./properties/list_areas', { 
				method: 'post',
				parameters: {island_id: id},
				onCreate: function() {
					$('SearchAreaId').options.length = 0;
					$('SearchAreaId').options[0] = new Option('Loading ...','');
				},
				onSuccess: function(transport,json) {
					if(json.data.length > 0) {
						count = 0;
						json.data.each(function(itemObj) {
							$('SearchAreaId').options[count] = new Option(itemObj.name.unescapeHTML(),itemObj.id);
							count += 1;
						});						
					} else {
						$('SearchAreaId').options[0] = new Option('Island Has No Areas!','0');
					}
				},
				onFailure: function() { alert('Something went wrong...'); },
				onComplete: function() { 
					$('SearchCityId').options.length = 0;
					$('SearchCityId').options[0] = new Option('Please Select a Area','0');
				}
			});
		} else {
			$('SearchAreaId').options.length = 0;
			$('SearchAreaId').options[0] = new Option('Please Select a Island','0');
			$('SearchCityId').options.length = 0;
			$('SearchCityId').options[0] = new Option('Please Select a Area','0');
		}
	},
	getCities:function(e) {
		var id = $(e).options[$(e).selectedIndex].value;
		if(id != 0) {
			new Ajax.Request('./properties/list_cities', { 
				method: 'post',
				parameters: {area_id: id},
				onCreate: function() {
					$('SearchCityId').options.length = 0;
					$('SearchCityId').options[0] = new Option('Loading ...','');
				},
				onSuccess: function(transport,json) {
					if(json.data.length > 0) {
						count = 0;
						json.data.each(function(itemObj) {
							$('SearchCityId').options[count] = new Option(itemObj.name.unescapeHTML(),itemObj.id);
							count += 1;
						});
					} else {
					    // 5-10-2011 - was "Island has No Areas!" thought that was kind of aggressive. 
						$('SearchCityId').options[0] = new Option('-','0');
					}
				},
				onFailure: function() { alert('Something went wrong...'); },
				onComplete: function() {  }
			});
		} else {
			$('SearchCityId').options.length = 0;
			$('SearchCityId').options[0] = new Option('Please Select a Area','0');
		}
	},
	updateMessage:function(div) {
		$(div).innerHTML = 'Loading ...';
	},
	showEditNotes:function(e, display, edit) {
		// Hide display
		$(display).hide();
		// Show Form
		$(edit).show();
	},
	hideEditNotes:function(e, display, edit, msg,jm) {
		// Show display
		$(display).show();
		// Hide form
		$(edit).hide();
		// Hide Updating message
		$(msg).innerHTML = '';
		
		/* added by jason russo 1-6-2010 */
		if(jm) {
		var jnotes = $F('PropertyHistoryRentalRenterNotes');//document.me.data[PropertyHistory][rental_renter_notes].value;
		var jproperty = $('jproperty').innerHTML;
		var username = $('juser').innerHTML;
		username = escape(username);
		jnotes = escape(jnotes);
		jproperty = escape(jproperty);
		
		var pars = 'notes=' + jnotes + '&property=' + jproperty + '&username=' + username;
		
		var myAjax = new Ajax.Request(
			'/contact/jsend_mail.php', 
			{
				method: 'get', 
				parameters: pars
			});
			}
		
	},
	showService:function(e, id, info, list) {
		// Hide List
		$(list).hide();
		// Show info
		$(info).show();		
		// Update Info
		new Ajax.Updater(info,'../view_service_details', {
			parameters: {service_id: id},
			onCreate: function() {
				$(info).innerHTML = '<div class="red align-center pad-20"><strong>Loading ...</strong></div>';
			},
			onLoading: function(request) { },
			onComplete:function(request) { }
		});
	},
	hideService:function(e, info, list) {
		// Hide info
		$(info).hide();
		// Show List
		$(list).show();
	},
	removeRequest:function(e, id) {
		new Ajax.Request('../delete_request', { 
			method: 'post', 
			parameters: {request_id:id},
			onSuccess: function(transport){
				var response = transport.responseText;
				switch(response.charAt(0)) {					
					case '1':
						new Effect.DropOut($(e),{duration:1.0,afterFinish: function() { alert('The Request has been succesfully deleted!'); location.reload(); }});
						break;
					default:
						alert('Error while attempting to delete selected Request!.');
						break;
				}
			},
			onFailure: function() { alert('Something went wrong...'); }
		});
	},
	showRequestDetails:function(e, id, edit, list, form, msg) {
		// Hide List
		$(list).hide();
		// Show info
		$(edit).show();	
		// Update Loading msg
		$(msg).innerHTML = '<div class="red align-center pad-20"><strong>Loading ...</strong></div>';
		// Update Info
		new Ajax.Request('../edit_request_details', { 
			method: 'post', 
			parameters: {request_id: id},
			onSuccess: function(transport,json){
				if(json.data.length > 0) {
					json.data.each(function(itemObj) {
						$('PropertyHistoryRequestId').value = itemObj.id.unescapeHTML();
						$('PropertyHistoryRequestDetails').value = itemObj.request_details.unescapeHTML();
					});
				}
				$(form).style.visibility = 'visible';
			},
			onFailure: function() { alert('Something went wrong...'); },
			onComplete:function(request) { $(msg).innerHTML = ''; }
		});
	},
	hideRequestDetails:function(e, edit, list, form, msg) {
		// Show display
		$(list).show();
		// Hide form
		$(edit).hide();
		// Hide form
		$(form).style.visibility = 'hidden';
		$('PropertyHistoryRequestId').value = '';
		$('PropertyHistoryRequestDetails').value = '';
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	requestNotify:function(e, id) {
		new Ajax.Request('../request_notification', { 
			method: 'post', 
			parameters: {request_id: id},
			onSuccess: function(transport){
				var response = transport.responseText;
				switch(response.charAt(0)) {					
					case '1':
						alert('Your notification has been sent successfully!');
						break;
					default:
						alert('Error while attempting to notify Hawaii Hideaways of selected Request!.');
						break;
				}
			},
			onFailure: function() { alert('Something went wrong...'); },
			onComplete:function(request) { $(msg).innerHTML = ''; }
		});
	},
	showEditProfile:function(e, display, edit) {
		// Hide display
		$(display).hide();
		// Show Form
		$(edit).show();
	},
	hideEditProfile:function(e, display, edit, msg) {
		// Show display
		$(display).show();
		// Hide form
		$(edit).hide();
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	displayCalendar:function(m, y) {
		new Ajax.Updater('calendar-wrap','../calendar', {
			parameters: {month: m, year: y},
			onCreate: function() { },
			onLoading: function(request) { },
			onComplete:function(request) { }
		});
	},
	showAddDate:function(e, add) {
		$(add).show();
	},
	hideAddDate:function(e, add, msg) {
		$(add).hide();
	},
	addDate:function() {
		new Ajax.Updater('calendar-wrap','../add_date', {
			asynchronous:true, 
			evalScripts:true,
			onCreate:function() { $('update-msg').innerHTML = 'Loading ...'; },
			onComplete:function(request, json) {
				hh.hideAddDate(this,'add-date','update-msg');
				Form.reset('add_date-frm');
			}, 
			parameters:Form.serialize('add-date-frm'), 
			requestHeaders:['X-Update', 'calendar-wrap']
		})
	},
	editDate:function(update, form, display, edit) {
		new Ajax.Updater(update,'../edit_date_description', {
			asynchronous:true, 
			evalScripts:true,
			onCreate:function() { $('update-msg').innerHTML = 'Loading ...'; },
			onComplete:function(request, json) {
				hh.hideEditDateDescription(this, display, edit,'update-msg');
				Form.reset(form);
			}, 
			parameters:Form.serialize(form), 
			requestHeaders:['X-Update', update]
		})
	},
	showEditDateDescription:function(e, display, edit) {
		// Hide display
		$(display).hide();
		// Show edit
		$(edit).show();
	},
	hideEditDateDescription:function(e, display, edit, msg) {
		// Show display
		$(display).show();
		// Hide form
		$(edit).hide();
		// Hide Updating message
		$(msg).innerHTML = '';
	},
	deleteDate:function(id, m, y) {
		new Ajax.Request('../delete_date', { 
			method: 'post', 
			parameters: {date_id: id},
			onSuccess: function(transport){
				var response = transport.responseText;
				switch(response.charAt(0)) {					
					case '1':
						alert('Your availability date has been deleted successfully!');
						new Ajax.Updater('calendar-wrap','../calendar', {
							parameters: {month: m, year: y},
							onCreate: function() { },
							onLoading: function(request) { },
							onComplete:function(request) { }
						});
						break;
					default:
						alert('Error while attempting to delete your availability date!.');
						break;
				}
			},
			onFailure: function() { alert('Something went wrong...'); },
			onComplete:function(request) { $(msg).innerHTML = ''; }
		});
	},
	fbs_click:function(u,t) {
u=location.href;
t=document.title;
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
},
show_hide:function(div) {
if(!div.indexOf(',')) {
div += div + ',';
 } 
var div1 = div.split(',');
var divs = '';
    for(i=0;i<div1.length;i++) {
   if($(div1[i])) {
      if($(div1[i]).visible() == true) {
	  Effect.BlindUp(div1[i]);
	  //$('fadeintothewind').hide();
	  } else {
      Effect.BlindDown(div1[i]);
	 // $('fadeintothewind').show();
        }
     }
	 }
	  if($('fadeintothewind')) {
	    if($('fadeintothewind').visible() == true) {
	    $('fadeintothewind').hide();
	    } else {
        $('fadeintothewind').show();
        }
	    }
   //var x = document.body.scrollTop;
   //var y = document.body.scrollLeft;
   //document.getElementById(div).style.top = (x + -100) + "px";
   // $(div).style.left = (y + 300) + "px";
    },
send_mail:function(frmnme) {
 var formlength = frmnme.length;
 var pars = $(frmnme).serialize();
 var url = '/contact/contact.php';

 var myAjax = new Ajax.Request(
        url,
        {method: 'post', parameters: pars, onCreate: function(){
			$('results').innerHTML = '<img src="/img/ajax-loader.gif">';
			$('results').show();
		},onComplete: this.show_response}
    );
  },
show_response:function(request) {
  //alert(' o.k. ' + request.responseText);
  $('results').innerHTML = request.responseText;
  Effect.BlindDown('results');
   },
   clear_results:function(){
   $('results').innerHTML = "";
   $('results').hide();
    },
virtual_tour_yava:function(div,url,wid,hei) {
var left=0,top=0;
//wid=100;hei=1;
 if(!wid) {
 wid='100';
  }
 if(!hei) {
 hei='1';
  }
open(url,'VT','toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1, width=' + wid + ',height=' + hei + ', left=' + left + ', top=' + top); 

},
virtual_tour:function(div,url,wid,hei) {
if(!wid) {
wid='900';
 }
if(!hei) {
hei='500';
 }
//open(url,'HB','toolbar=0,location=0,status=1,menubar=0,scrollbars=1,resizable=1, width=' + width + ',height=' + height + ', left=' + left + ', top=' + top); 
 // if($('fadeintothewind').visible() == true) {
  //$('fadeintothewind').hide();
 //  } else {
  //$('fadeintothewind').show();
  //  }
 //var elt = $(div);
win = new Window({width:wid, height:hei, className: "alphacube", hideEffect:Element.hide});
		win.setContent(div);
		win.showCenter();

     /*	
      // retrieve required dimensions
      var eltDims = elt.getDimensions();
      var browserDims = $(document.body).getDimensions();
	  
      // calculate the center of the page using the browser and element dimensions
      var y = (browserDims.height - eltDims.height) / 6;
      var x = (browserDims.width - eltDims.width) / 2;
      // set the style of the element so it is centered
      var styles = { position : 'absolute',
      top : y + 'px',
      left : x + 'px' };
      elt.setStyle(styles);
      elt.scrollTo(); 
    */
  Effect.BlindDown(div);


 },
view_all:function(cnt,jafunk,isl) {
/* this fuction is for viewing all records. */
/* 10-17-2011 added islands view all search. */
if(!jafunk) {
jafunk = 'results';
}
if(!isl) {
isl = 'properties' 
}

  //if(confirm('Loading all the records may take a few minutes.\nContinue?')) {
   location.href='/' + isl + '/' + jafunk + '/page:1/limit:' + cnt;
  // }
 },
chk_unck:function(chkval) {
//alert($F('lastviewid'));
//alert(chkval.length);
//if array selection box and multi select
if(chkval.length >0 && $F('lastviewid')!='') {
 //if id is already checked the "uncheck it"
    var scnt=0;
	 for(i=0;i<chkval.length;i++) {
	   if(chkval.options[i].selected == true) {
	   scnt++;
	    }
	   }
	  
	  //only uncheck if we're using basic mouse click and not ctrl + mouse click.
	 // alert($F('lastviewid') + ' ' + chkval.options.selectedIndex);
   if($F('lastviewid') == chkval.options.selectedIndex && scnt ==1) {
	var t=$F('lastviewid');
	
	if(chkval.options[t].selected == true) {
	chkval.options[t].selected=false;
	 }
	
  }
  
  }
$('lastviewid').value = chkval.options.selectedIndex;
//var selc = $('SearchIslandId');

 },
 isSkypeInstalled:function() {
    try {
        /*@cc_on
        //The Microsoft way, thanks to the conditional comments only run in IE
        if (new ActiveXObject("Skype.Detection")) return true;
        @*/

        //Tested for firefox on win
        if (navigator.mimeTypes["application/x-skype"]) return true;
    }
    catch(e){}
    return false;
},
//check characters ...
 Check_Chars:function(jbox,x) {
var max_cars = 2000;

if(jbox.value == "Enter Questions/Comments.") {
jbox.value = '';
//max_cars = max_cars + jbox.length;
  }

var chars_left = max_cars - jbox.value.length;
if(chars_left < 10 && chars_left >= 0) {
chars_left = '<strong style="color:yellow;">' + chars_left + '</strong>';
  }
if(chars_left < 0) {
chars_left = '<strong style="color:red;">' + chars_left + '</strong>';
$('error_additional_comments').show();
  }
$('chars_left').innerHTML = chars_left;

  },

/* Form Sorting */
sort_form:function() {
 document.sortform.submit(); 
}

  }
