/*--------------------------------------------------------------------------*
 *  
 *  footerFixed.js
 *  
 *  MIT-style license. 
 *  
 *  2007 Kazuma Nishihata [to-R]
 *  http://blog.webcreativepark.net
 *  
 *--------------------------------------------------------------------------*/

new function(){
	
	var footerId = "footer";
	//メイン
	function footerFixed(){
		// 15/01/2008 OB: add test if footer exist before manipulating it
		var footerEl = null;
		footerEl = document.getElementById(footerId);
		if (footerEl) {
			//ドキュメントの高さ
			var dh = document.getElementsByTagName("body")[0].clientHeight;
			//フッターのtopからの位置
			footerEl.style.top = "0px";
			var ft = footerEl.offsetTop;
			//フッターの高さ
			var fh = footerEl.offsetHeight;
			//ウィンドウの高さ
			if (window.innerHeight){
				var wh = window.innerHeight;
			}else if(document.documentElement && document.documentElement.clientHeight != 0){
				var wh = document.documentElement.clientHeight;
			}
			if(ft+fh<wh){
				footerEl.style.position = "relative";
				footerEl.style.top = (wh-fh-ft-1)+"px";
			}
		}
	}
	
	//文字サイズ
	function checkFontSize(func){
	
		//判定要素の追加	
		var e = document.createElement("div");
		var s = document.createTextNode("S");
		e.appendChild(s);
		e.style.visibility="hidden"
		e.style.position="absolute"
		e.style.top="0"
		document.body.appendChild(e);
		var defHeight = e.offsetHeight;
		
		//判定関数
		function checkBoxSize(){
			if(defHeight != e.offsetHeight){
				func();
				defHeight= e.offsetHeight;
			}
		}
		setInterval(checkBoxSize,1000)
	}
	
	//イベントリスナー
	function addEvent(elm,listener,fn){
		try{
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent("on"+listener,fn);
		}
	}

	addEvent(window,"load",footerFixed);
	addEvent(window,"load",function(){
		checkFontSize(footerFixed);
	});
	addEvent(window,"resize",footerFixed);
	
}

// clear default value in forms
function doClear(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "";
	}
}
// undo clear of default value
function undoClear(theText) {
	if (theText.value == "") {
		theText.value = theText.defaultValue;
	}
}

// ----------------------------------------------------------------------------
// general url
// ----------------------------------------------------------------------------

// goto an url
function gotoUrl(u) {
	document.location=u;
}

// open the link in a new window
// return false if the popup is shown, true otherwise
function openWindow(oLink, name, width, height, params) {
	var href = '';
	if (oLink.getAttribute) href = oLink.getAttribute('href');
	if (href=='') href = oLink.href;
	if (!href || href=='') href = oLink;
	var all_params = '';
	all_params += 'width=' + width + ',height=' + height;
	if (params && params!='') all_params += ','+params;
	// all is ready, open the popup and focus on it
	var oPopup = window.open(href, (name && name!='' ? name : 'popup'), all_params);
	if (oPopup) oPopup.focus();
	return (oPopup?false:true);
}

// bedsheets: goto page
function bookGotoPage(num) {
    if (parent)
        var domSwf = parent.document.getElementById("bedsheetsmagazine");
    else
        var domSwf = document.getElementById("bedsheetsmagazine");
    if (domSwf)
        domSwf.swfGotoPage(num);
}

// open subscription page
function openSubscribe(f) {
	var u = f.action+"?m_email="+f.elements["m_email"].value;
	var anchor = this.document.createElement('a');
	anchor.setAttribute('href', u);
	anchor.setAttribute('rel', 'lyteframe');
	anchor.setAttribute('title', '');
	anchor.setAttribute('rev', 'width: 715px; height: 450px; scrolling: auto;');
	myLytebox.start(anchor, false, true);
	return false;
}

// show an element
function show(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.display = "block";
	}
}
// hide an element
function hide(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.display = "none";
	}
}


// trim the left and right spaces of a string
function trim(str) {
	return str.replace(/^\s+|\s+$/, '');
};

// returns true if the string is empty
function isEmpty(str) {
	return (str == null) || (str.length == 0);
}

// returns true if the string is a valid email
function isEmail(str) {
	if (isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}


function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function daysElapsed(date1,date2) {
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}

// TODO check this
function checkDate(f) {
	a_d = f.elements['booking_date'].split("/");
	if (a_d.length == 3) {
		var d = a_d[0];
		var m = a_d[1];
		var y = a_d[2];
		var days = daysElapsed(new Date(d+" "+m+" "+y), new Date());
		if (days < 2) {
			alert("Unfortunately we cannot take reservations online within 48 hours of the time requested.");
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function checkTime(f) {
	var h_min = 19; var m_min = 30; // change these 4 constants to modify the time boundaries
	var h_max = 22; var m_max = 00; 
	var h = f.elements['hour'].value;
	var m = f.elements['minute'].value;
	if (h == "" || m == "") {
		// Don't warn; the formmail will warn about the missing fields.
		return true;
	}
	h = parseInt(h); // to avoid the + operator on strings (see h*60+m below)
	m = parseInt(m);
	if (((h*60+m) < (h_min*60+m_min)) || ((h*60+m) > (h_max*60+m_max))) {
		alert("Unfortunately we can only take reservations online between "+h_min+":"+m_min+" and "+h_max+":"+m_max+".");
		return false;
	} else {
		return true;
	}
}

function checkMandatory(f) {
	// get the required fields and split them to check them
	var i, s, rf, a_rf, retval;
	rf = f.elements['require'].value;
	a_rf = rf.split(",");
	retval = true;
	
	// loop the fields to check them
	i=0;
	while (i < a_rf.length && retval) {
		// get type of field
		if (f.elements[a_rf[i]].type == 'text' || 
			f.elements[a_rf[i]].type == 'textarea' ||
			f.elements[a_rf[i]].type == 'radio' ||
			f.elements[a_rf[i]].type == 'checkbox' ||
			f.elements[a_rf[i]].type == 'select-one') {
			
			s = '';
			if (f.elements[a_rf[i]].type == "select-one") {
				var si = f.elements[a_rf[i]].selectedIndex;
                if (si >= 0) {
                    s = f.elements[a_rf[i]].options[si].value;
                }
			} else if (f.elements[a_rf[i]].type == 'radio') {
				if (f.elements[a_rf[i]].checked) {
					s = f.elements[a_rf[i]].value;
				}
			} else if (f.elements[a_rf[i]].type == 'checkbox') {
				if (f.elements[a_rf[i]].checked) {
					s = '1';
				}
			} else {
				s = f.elements[a_rf[i]].value;
			}
			s = trim(s);
			if (isEmpty(s)) {
				f.elements[a_rf[i]].value = s;
				f.elements[a_rf[i]].focus();
				retval = false;
			}
			
		} else if ((f.elements[a_rf[i]].length > 0) && 
				(f.elements[a_rf[i]][0].type == 'radio' || 
				f.elements[a_rf[i]][0].type == 'checkbox')) {
			
				var loop, isChecked=-1;
            for (loop=0;loop < f.elements[a_rf[i]].length;loop++) {
                if (f.elements[a_rf[i]][loop].checked) {
                    isChecked=loop;
                    break; // only one needs to be checked
                }
            }
            if (isChecked < 0) {
                f.elements[a_rf[i]][0].focus();
					retval = false;
            }
			
		}
		i++;
	}
	// warn the user
	if (!retval)
		alert("We cannot process your reservation if you don't fill all mandatory fields.");
	
	return retval;	
}

function checkReservationFields(f) {
	return checkMandatory(f) && checkDate(f) && checkTime(f);
}

// check if the field is filled to enable the button
// params: form object, field name, button name
function checkActSearch(f, fn, bn) {
	if (f.elements[fn] && f.elements[bn]) {
		var s = f.elements[fn].value.trim();
		if (s.length < 2) {
			f.elements[bn].disabled = true;
			f.elements[bn].setStyle('background', '#cccccc');
			return false;
		} else {
			f.elements[bn].disabled = false;
			f.elements[bn].setStyle('background', '#cb4f55');
			return true;
		}
	} else
		return false;
}

// open the djprofile page
function openDJ(dj_id) {
	document.location = "/acts/djprofile.php?id="+dj_id+"&fr=1";
}

