/* Common JavaScript code used throughout theharborsideinn.com website *
 * by Mark Rosenstein - October, 2010
 */

setTimeout('refreshwebcam()', 60000);

function refreshwebcam() {
    if ($('webcam')) {
      var d = new Date();
      $('webcam').src = '/webcam/image.jpg?x='+d.valueOf();
      setTimeout('refreshwebcam()', 60000);
    }
}

function addattach(page,t) {
    new Ajax.Updater('addattachment','attachment.php?op=add&t='+t+'&p='+page);
}

function loadattach(page,t) {
    window.open('attachment.php?op=load&t='+t+'&p='+page);
}

function selectAttach(id,page,t) {
    if (t == 'd') {
	$('attachment').value = id;
    } else {
      new Ajax.Updater('attachlist', 'attachment.php?op=add2&t='+t+'&id='+id+'&p='+page, {insertion: Insertion.Bottom});
    }
    $('addattachment').innerHTML = '';
}

function deleteattach(id,page,t) {
    new Ajax.Updater('attachlist', 'attachment.php?op=del&id='+id+'&p='+page);
    Sortable.create('attachlist', {tag:'div',only:'attachbox'});
}

var _slide = 0;
var _lastslide = 0;
function slideshow() {
    var e, e2;
    if (_lastslide) {
      e = $('slideimg0');
      d = $('slidediv0');
      d2 = $('slidediv1');
      c = $('slidecap0');
    } else {
      e = $('slideimg1');
      d = $('slidediv1');
      d2 = $('slidediv0');
      c = $('slidecap1');
    }
    _lastslide = 1-_lastslide;
    e.src = slides[_slide];
    e.alt = keywords[_slide];
    c.innerHTML = captions[_slide];
    d.style.opacity = 0;
    d.style.filter = "alpha(opacity = 0)";
    d2.style.zIndex = 1;
    d.style.zIndex = 2;
    new Effect.Opacity(d, { from: 0.0, to: 1.0, duration: 2.0 });
    new Effect.Opacity(d2, { from: 1.0, to: 0.0, duration: 2.0 });
    _slide++;
    if (_slide >= slides.length) {
	_slide = 0;
    }
    setTimeout('slideshow()', 4*1000);
}

function addpic(t) {
    new Ajax.Updater('addpic', 'addpic.php?t='+t);
    $('addpic').style.display = 'block';
}

function apload(t) {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    window.open("addpic.php?op=load&t="+t+"&e="+id, "loadpic", "width=500,height=450");
}

function apadd(t,p) {
    var e = $('id');
    var id = 0;
    var div = 'addpic';
    if (t > 0 || t == 'd' || t == 'p' || t == 'c1' || t == 'c2' || t == 'c3') {
	div = 'pic_'+t;
    }
    if (e) {
	id = e.value;
    }
    if (t > 0 || t == 'd' || t == 'p' || t == 'c1' || t == 'c2' || t == 'c3') {
      if (t == 'p') {
	$('picture').value = p;
      } else if (t == 'c1' || t == 'c2' || t == 'c3') {
	$('pic'+t.substring(1)).value = p;
      } else if (t > 0) {
	$('pic'+t).value = p;
      }
      new Ajax.Updater('pic_'+t, 'addpic.php?op=add&t='+t+'&p='+p+'&e='+id);
    } else {
      $(div).style.display = 'none';
      new Ajax.Updater('piclist', 'addpic.php?op=add&t='+t+'&p='+p+'&e='+id, {insertion: Insertion.Bottom, onComplete:function(){ Sortable.create('piclist', {tag:"div",only:'picbox',constraint:'horizontal',onUpdate:function(){picsort(t);}}); }});
    }
}

function apsearch(t) {
    var div = 'addpic';
    if (t > 0 || t == 'd' || t == 'p' || t == 'c1' || t == 'c2' || 4 == 'c3') {
	div = 'pic_'+t;
    }
    new Ajax.Updater(div, 'addpic.php?op=search&t='+t+'&kw='+$('apkw').value+'&desc='+$('desc').value+'&category='+$F('category'));
}

function picsort() {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    new Ajax.Request('pictool.php?op=sort&p='+id+'&'+Sortable.serialize('piclist'));
}

function sprm(t,p) {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    new Ajax.Request('addpic.php?op=del&t='+t+'&p='+p+'&e='+id);
    $('pic_'+p).style.display = 'none';
}

function ppreplace(t) {
    new Ajax.Updater('pic_'+t, 'addpic.php?t='+t);
}

function addcontact(b) {
    new Ajax.Updater('contactbox', 'contacttool.php?op=add&b='+b,
                     {asynchronus: false,
			     insertion: Insertion.Bottom});
}

function contactupdate(c, b, f) {
    var v = $F('contact'+c+f);
    contactmaybeclear(c);
    new Ajax.Request('contacttool.php?op=update&c='+c+'&b='+b+'&f='+f,
                     { postBody: 'v='+escape(v)});
}

function contactupdate2(c, b, i, t, f) {
    var v = $F('contact'+c+t+i+f);
    contactmaybeclear(c);
    new Ajax.Request('contacttool.php?op=update&c='+c+'&b='+b+'&f='+f+'&i='+i+'&t='+t,
                     { postBody: 'v='+escape(v)});
}

var contacttimers = new Array();

function contactmaybe(c, b, m) {
    var e = $('contact'+c+'info');
    e.className = 'contactoption';
    var n = document.createElement('div');
    n.className = 'drow';
    n.appendChild(document.createTextNode('Click to use existing contact:'));
    e.appendChild(n);
    for (i = 0; i < m.length; i++) {
        n = document.createElement('div');
        n.className = 'drow clickable';
        n.appendChild(document.createTextNode(m[i][1]));
        n.onClick = function() { contactselect(c, b, m[i][0]); };
        n.setAttribute('onclick', "contactselect("+c+", "+b+", "+m[i][0]+")");
        e.appendChild(n);
    }
    contacttimers[c] = window.setTimeout("contactmaybeclear("+c+")", 10000);
}

function contactmaybeclear(c) {
    var e = $('contact'+c+'info');
    if (contacttimers[c]) {
        window.clearTimeout(contacttimers[c]);
        contacttimers[c] = 0;
    }
    if (e.hasChildNodes()) {
        while (e.hasChildNodes()) {
            e.removeChild(e.firstChild);
        }
        e.className = '';
    }
}

function contactselect(c, b, n) {
    new Ajax.Updater('contact'+c, 'contacttool.php?op=replace&b='+b+'&c='+c+'&n='+n);
}

function addaddress(c, b) {
    new Ajax.Updater('contact'+c+'addresses', 'contacttool.php?op=addad&b='+b+'&c='+c,
                     {asynchronus: false,
			     insertion: Insertion.Bottom});
}

function deladdress(c,b,i) {
    new Ajax.Updater('contact'+c+'addresses'+i, 'contacttool.php?op=deladdress&c='+c+		     '&b='+b+'&i='+i);
}

function addphone(c, b) {
    new Ajax.Updater('contact'+c+'phones', 'contacttool.php?op=addph&b='+b+'&c='+c,
                     {asynchronus: false,
			     insertion: Insertion.Bottom});
}

function delphone(c,b,i) {
    new Ajax.Updater('contact'+c+'phone'+i, 'contacttool.php?op=delphone&c='+c+		     '&b='+b+'&i='+i);
}

function addemail(c, b) {
    new Ajax.Updater('contact'+c+'emails', 'contacttool.php?op=addem&b='+b+'&c='+c,
                     {asynchronus: false,
			     insertion: Insertion.Bottom});
}

function delemail(c,b,i) {
    new Ajax.Updater('contact'+c+'email'+i, 'contacttool.php?op=delemail&c='+c+		     '&b='+b+'&i='+i);
}

function rmcontact(c,b) {
    new Ajax.Updater('contact'+c, 'contacttool.php?op=rm&c='+c+'&b='+b);
}

function featured(i,v) {
    new Ajax.Request('setfeature.php?id='+i+'&v='+v);
}

function showmap(u) {
    mapwin = window.open('/map.php?'+u, "hkmap", "height=400,width=450", true);
    mapwin.focus();
}
function showprojpic(i, s) {
    $('bigpic').src="/files/pic"+i+"-m.jpg";
    $('caption').innerHTML = s;
}

var tw1 = null;
var tw2 = null;
var twp = 0;
var twbusy = 0;
var twwidth = 560;

function picmore(adj) {
    if (twbusy) { return; }
    twbusy = 1;
    if (!tw1) {
	tw1 = $('projpics1');
	tw2 = $('projpics2');
    }
    twp += adj;
    new Ajax.Updater(tw2, '/projpics.php?i='+twp, {onComplete: function() {
		if (adj > 0) {
		    tw2.style.left = ''+twwidth+'px';
		    offset = -twwidth;
		} else {
		    tw2.style.left = '-'+twwidth+'px';
		    offset = twwidth;
		}
		new Effect.Move(tw1, { x:offset, y:0, mode:'relative', transition: Effect.Transitions.sinoidal, afterFinish: function() { twbusy=0; }});
		new Effect.Move(tw2, { x:offset, y:0, mode:'relative', transition: Effect.Transitions.sinoidal});
		x = tw1;
		tw1 = tw2;
		tw2 = x;
	    }});
}

function callIRM() {
    var url = 'http://reservations.theharborsideinn.com/irmnet/res/resmain.aspx?';
    var now = new Date();
    var earliest = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    var arrival = document.qrform.wdcArrival_input.value;
    var departure = document.qrform.wdcDeparture_input.value;
    var nights = document.qrform.wcNights_input.value;
    var child = document.qrform.People3.value;
    var adults = document.qrform.People1.value;
    if (arrival != 'Select Date' && arrival != '') {
	if (nights <= 0 ) {
	    alert('Departure date must be later than arrival date.\nPlease change your calendar dates and try again.');
	    return false;
	}
	//additional checking
	if (nights > 30) {  // for example enforce a maximum stay
	    alert('Online Reservations cannot be made for more than a 30 night stay.\nPlease change your calendar dates, or call us about your extended stay.\nThank You');
	    return false;
	}
	var arrival = arrival.split('/');
	var arriveField = arrival[0]+'/'+arrival[1]+'/'+arrival[2];
	url = url + 'Arrival=' + arriveField;
	if (!departure) {
	  url = url + "&Nights=" + nights;
	} else {
	  var departure = departure.split('/');
	  var departField = departure[0]+'/'+departure[1]+'/'+departure[2];
	  url = url + '&Departure=' + departField;
	}
    } else {
	arrival = new Date(earliest.getFullYear(), earliest.getMonth(), earliest.getDate());
	var arriveField = (months[arrival.getMonth()]) + '/' +   
	    ((arrival.getDate()<10) ? '0' : '')+ arrival.getDate() + '/' + 
	    fourdigits(arrival.getFullYear());
	departure = new Date(earliest.getFullYear(), earliest.getMonth(), earliest.getDate() + 2);
	var departField = (months[departure.getMonth()]) + '/' +
	    ((departure.getDate()<10) ? '0' : '')+ departure.getDate() + '/' + 
	    fourdigits(departure.getFullYear());
	url = url + 'Arrival=' + arriveField;
	url = url + '&Departure=' + departField;
    }
    url = url + '&People1=' + adults;
    url = url + '&People2=' + child;
    window.location.assign(url);
}//end callIRM()


function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
  // otherwise, update 'characters left' counter
  else 
    countfield.value = maxlimit - field.value.length;
}

function picadd() {
    new Ajax.Updater('addpic', 'pictool.php?op=add');
    $('addpic').style.display = 'block';
}

function picrm(i) {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    new Ajax.Request('pictool.php?op=rm&id='+i+'&p='+id);
    $('pic_'+i).style.display = 'none';
}

function picload() {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    window.open("pictool.php?op=load&e="+id, "loadpic", "width=500,height=600,resizable");
}

function picloaded(i) {
    new Ajax.Updater('piclist', 'pictool.php?op=add2&id='+i);
}

function picset(i) {
    var e = $('id');
    var id = 0;
    if (e) {
	id = e.value;
    }
    if (id>0) {
      new Ajax.Updater('piclist', 'pictool.php?op=set&id='+i+'&p='+id, {insertion: Insertion.Bottom, onComplete:function(){ Sortable.create('piclist', {tag:"div",only:'picbox',constraint:'horizontal',onUpdate:function(){picsort();}});}});
    } else if (id<0) {
      new Ajax.Updater('piclist', 'pictool.php?op=set&id='+i+'&p='+id, {insertion: Insertion.Bottom, onComplete:function(){ Sortable.create('piclist', {tag:"div",only:'picbox',constraint:'horizontal',onUpdate:function(){picsort();}});}});
    } else {
	$('roomimg').src="/files/pic"+i+"-t.jpg";
	$('picture').value = i;
    }
    $('addpic').innerHTML = '';
    $('addpic').style.display = 'none';
}

function picsearch() {
    var d = escape($('desc').value);
    new Ajax.Updater('addpic', 'pictool.php?op=search&d='+d);
}

function docrop(w,h,r) {
    var img = $('theimage');
    var box = $('thecrop');
    if (w/h > r) {
	bt = 0;
	bh = h-2;
	bw = Math.round(h*r-2);
	bl = Math.round((w-bw)/2);
    } else {
	bl = 0;
	bw = w-2;
	bh = Math.round(w/r-2);
	bt = Math.round((h-bh)/2);
    }
    box.style.left = bl+'px';
    box.style.top = bt+'px';
    box.style.width = bw+'px';
    box.style.height = bh+'px';
    function cropdrag(e) {
	if (e.shiftKey) {
	    if (parseInt(box.style.left)<0) {
		box.style.left=bl+"px";
		box.style.width=parseInt(box.style.width)-1+"px";
		box.style.top=bt+"px";
		box.style.height=Math.round(parseInt(box.style.width)/r)+"px";
	    }
	} else {
	    if (parseInt(box.style.left)<0) {
		box.style.left="0px";
	    }
	    if (parseInt(box.style.left)+parseInt(box.style.width)>=w-2) {
		box.style.left=w-parseInt(box.style.width)-2+"px";
	    }
	    if (parseInt(box.style.top)<0) {
		box.style.top="0px";
	    }
	    if (parseInt(box.style.top)+parseInt(box.style.height)>=h-2) {
		box.style.top=h-parseInt(box.style.height)-2+"px";
	    }
	}
    }
    function cropend() {
	alert("Crop to: ("+box.style.width+","+box.style.height+") @ ("+box.style.left+","+box.style.top+")");
    }
    var drag = new Draggable('thecrop', {change:cropdrag,onEnd:cropend});
}

function validatejoin() {
    var msg = '';
    if (document.f.first.value.length == 0) {
	msg += '  Please enter your first name.';
    }
    if (document.f.last.value.length == 0) {
	msg += '  Please enter your last name.';
    }
    if (document.f.address1.value.length == 0) {
	msg += '  Please enter your address.';
    }
    if (document.f.city.value.length == 0) {
	msg += '  Please enter your city.';
    }
    if (document.f.state.value.length == 0) {
	msg += '  Please enter your state.';
    }
    if (document.f.zip.value.length == 0) {
	msg += '  Please enter your zip code.';
    }
    if (document.f.phone.value.length == 0) {
	msg += '  Please enter your phone.';
    }
    if (document.f.email.value.length == 0) {
	msg += '  Please enter your email.';
    }
    if (document.f.email.value != document.f.email2.value) {
	msg += '  You did not enter the same email address twice.';
    }
    if (msg) {
	alert(msg);
	return(false);
    }
    return(true);
}

