function hover(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	current = current[0];
	
	if(what && current && current.search(/Hover$/) == -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current+'Hover");');
	} else if(!what && current && current.search(/Hover$/) != -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/Hover$/, '')+'");');
	}
}

function select(who, baseClass, what) {
	var current;
	
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);');
	if(current) {
		current = current[0];
		
		if(what && current && current.search(/^Sel/) == -1) {
			eval('who.className = who.className.replace(/'+current+'/, "Sel'+current+'");');
		} else if(!what && current && current.search(/^Sel/) != -1) {
			eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/^Sel/, '')+'");');
		}
	}
}

function parseStr(str) {
	var first = str.split('&');
	var data = new Array();
	
	for(var i = 0; i < first.length; i++) {
		var last = first[i].split('=');
		
		data[last[0]] = last[1];
	}
	
	return data;
}

/********* Used by the 'expirationDate' form field type *************/
function createExpirationDate(dateFieldID, monthFieldID, yearFieldID) {
	var dateField = document.getElementById(dateFieldID);
	var monthField = document.getElementById(monthFieldID);
	var yearField = document.getElementById(yearFieldID);

	if (dateField && monthField && yearField) {
		if (monthField.value != "" || yearField.value != "") {
			dateField.value = monthField.value + "/" + yearField.value;
		}
		else {
			dateField.value = "";
		}
		
	}

}	


function fixPNG(who) {
	var isIE = (navigator.appName == "Microsoft Internet Explorer" && navigator.userAgent.indexOf('Opera') == -1);
	if(isIE) {
		// filter for MSIE 6.x and 5.5
		var ie6xFilter = /^.*MSIE [6]\.[0-9].*$/;
		var ie55Filter = /^.*MSIE [5].*$/;
		
		if(ie6xFilter.test(navigator.userAgent) || ie55Filter.test(navigator.userAgent)) {
			who.onload = function(){}
			who.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+who.src+')';
			who.src = 'images/blank.gif';
		}
	}
}

function preload(imagePathArray) {
	var imageArray = new Array();
	for (var i = 0; i < imagePathArray.length; i++) {
		var nextImage = new Image();
		nextImage.src = imagePathArray[i];
		imageArray.push(nextImage);
	}
	return imageArray;
}

function evalScripts(data) {
	var index = data.indexOf('<script');
	var endIndex;
	
	while(index != -1) {
		index = data.indexOf('>', index) + 1;
		endIndex = data.indexOf('</script>', index);
		
		eval(data.substring(index, endIndex));
		
		index = data.indexOf('<script', endIndex);
	}
}

function processSlip(slip, skipShowSlip, slideTo) {
	if(!slip) {
		slip = 'detail';
	}

	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		if(slideTo && httpReq.responseText.indexOf('--skipSlide-->') == -1) {
			slideSlip(slip, slideTo, httpReq.responseText);
		} else {
			var el = document.getElementById(slip+'SlipContent');
			
			el.innerHTML = httpReq.responseText.replace(/\<script.+?\<\/script\>/g, '');
		}
	}
	
	evalScripts(httpReq.responseText);
	
	if(!skipShowSlip) {
		showSlip(slip);
	}
}

function processDiv(div) {
	if(httpReq.responseText.indexOf('--skipInnerHtml-->') == -1) {
		var slideIndex = httpReq.responseText.indexOf('--slideIn:');
		if(slideIndex != -1) {
			document.slideDiv(div, slideIndex, httpReq.responseText);
		} else {
			var el = document.getElementById(div);
			if(el) {
				el.innerHTML = httpReq.responseText.replace(/\<script.+?\<\/script\>/g, '');
			}
		}
	}
	
	evalScripts(httpReq.responseText);
}

function processEval() {
	eval(httpReq.responseText);
}

function scrollFix(el, event) {
	if(window.event) {
		el.scrollTop -= (window.event.wheelDelta / 120 * 60);
		return false;
	} else if(this.scrollTop == 0 && event.wheelDelta > 0) {
		return false;
	} else if((el.scrollHeight - this.scrollTop) == el.offsetHeight && event.wheelDelta < 0) {
		return false;
	}
}

function submitForm(theForm, queryString, proccessFunction) {
	var data = '';
	
	if(theForm) {
		if(theForm.jt_submitted) {
			theForm.jt_submitted.value = 'isValid';
		}
		var formElements = theForm.elements;
	
		for(i = 0; i < formElements.length; i++) {
			if(formElements[i].disabled || ((formElements[i].type == 'radio' || formElements[i].type == 'checkbox') && !formElements[i].checked))
				continue;
				
			if(i)
				data += "&";
			
			if(formElements[i].type == 'checkbox') {
				data += escape(formElements[i].name) + '[]=' + escape(formElements[i].value);
			} else if(formElements[i].options) {
				var valueAppend = '';
			
				for(k = 0; k < formElements[i].length; k++) {
					if(formElements[i].options[k].selected) {
						if(valueAppend != "") {
							valueAppend += ",";
						}
						
						valueAppend += formElements[i].options[k].value;
					}
				}
				
				data += escape(formElements[i].name) + '=' + escape(valueAppend);
			} else {
				data += escape(formElements[i].name) + '=' + escape(formElements[i].value);
			}
		}
	}
	
	sendRequest('view_controller.php', queryString, data, (proccessFunction ? proccessFunction : 'processSlip("detail", true);'));
}

function setLoadStatus(on) {
	var el = document.getElementById('loadStatus');
	var iframeMask = document.getElementById('loadStatusIFrame');
	
	if(el) {
		if(on) {
			el.style.top = '100px'; // we could maybe do some stuff for better vertical positioning...but for now, this is OK
			el.style.left = ((reservationFormWidth/2)-37)+'px';

			// loadStatusIFrame
			if (iframeMask && el.filters) {
				iframeMask.style.top = '100px'
				iframeMask.style.left = ((reservationFormWidth/2)-37)+'px';
				iframeMask.style.width = '84px'; 
				iframeMask.style.height = '84px'; 
				iframeMask.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			}
		
			el.style.display = 'block';
		} else {
			el.style.display = 'none';
			
			// loadStatusIFrame
			if (iframeMask && el.filters) {
				iframeMask.style.width = '0px'; 
				iframeMask.style.height = '0px'; 
			}

		}
	}
}

document.slideDiv = function(div, slideIndex, data) {
	var content = document.getElementById(div);
	
	if(content) {
		var scroller = content.parentNode;
		var container = scroller.parentNode;
		var newContent = document.createElement('div');
		
		if(scroller && container && newContent && !scroller.animation) {
			var direction = data.charAt(slideIndex+10);
			
			content.style.width = content.offsetWidth+'px';
			content.style.height = content.offsetHeight+'px';
			
			container.style.width = container.offsetWidth+'px';
			container.style.height = container.offsetHeight+'px';
			container.style.overflow = 'hidden';
			
			content.style.top = '0px';
			content.style.left = '0px';
			content.style.position = 'absolute';
			
			scroller.style.top = '0px';
			scroller.style.left = '0px';
			scroller.style.position = 'absolute';
			
			newContent.style.position = 'absolute';
			newContent.style.left = content.offsetWidth+'px';
			
			newContent.innerHTML = data.replace(/\<script.+?\<\/script\>/g, '');
			
			scroller.appendChild(newContent);
			
			if(direction == 'l') {
				newContent.style.left = (-newContent.offsetWidth)+'px';
			} else if(direction == 't') {
				newContent.style.left = (-(content.offsetWidth - newContent.offsetWidth) / 2)+'px';
				newContent.style.top = newContent.offsetHeight+'px';
			} else if(direction == 'b') {
				newContent.style.left = (-(content.offsetWidth - newContent.offsetWidth) / 2)+'px';
				newContent.style.top = content.offsetHeight+'px';
			}

			setSlipScrollers(container, 'hidden');

			if(container.offsetHeight != newContent.offsetHeight || container.offsetWidth != newContent.offsetWidth) {
				if(container.animation) {
					container.animation.cancel();
				}
				
				container.animation = new Fx.Morph(container.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
						if(!scroller.animation) {
							container.style.overflow = 'visible';
							container.style.width = 'auto';
							container.style.height = 'auto';
							
							if(Browser.Engine.trident) {
								setTimeout('document.getElementById("'+scroller.id+'").style.display = "none"; document.getElementById("'+scroller.id+'").style.display = "block";', 1);
							}
						}
				
						container.animation = null;
					}
				});
				container.animation.start({
					'height': [container.offsetHeight, newContent.offsetHeight],
					'width': [container.offsetWidth, newContent.offsetWidth]
				});
			}

			scroller.animation = new Fx.Morph(scroller.id, {duration: 750, transition: Fx.Transitions.Quart.easeOut, onComplete: function() {
					scroller.removeChild(content);
					newContent.id = div;
					setSlipScrollers(newContent, 'auto');
					if(data.indexOf('--evalScriptsOnSlide-->') != -1) {
						evalScripts(data);
					}
					
					scroller.style.position = 'static';
					newContent.style.position = 'static';
					
					if(!container.animation) {
						container.style.overflow = 'visible';
						container.style.width = 'auto';
						container.style.height = 'auto';
						
						if(Browser.Engine.trident) {
							setTimeout('document.getElementById("'+scroller.id+'").style.display = "none"; document.getElementById("'+scroller.id+'").style.display = "block";', 1);
						}
					}
					
					scroller.animation = null;
				}
			});
			
			scroller.animation.start({
				'left': [scroller.offsetLeft, -newContent.offsetLeft],
				'top': [scroller.offsetTop, -newContent.offsetTop]
			});
		}
	}
}

function toggleJTechComments() {
	var jtechComments = document.getElementById('jtechCommentsContainer');
	
	if(jtechComments) {	
		jtechComments.style.display = jtechComments.style.display == 'block' ? 'none' : 'block';
	}
}

function popupMedia(image, imageTitle, width, height, autosize) {
    // create the query string for the popup URL
	var mediaURL = "media_popup.php?image="+image+"&title="+imageTitle;
	// make sure the width is a decent size for us
	if (width < 525){
		width = 525;
	}
	
	if(autosize) {
		height += 5;
		
		if(height > screen.availHeight) {
			height = screen.availHeight - 20;
		}
	}
	
	var newWindow = open(mediaURL, "ImagePopup", "width="+width+",height="+height+",scrollbars="+(autosize ? 'yes' : 'no')+",resizable="+(autosize ? 'yes' : 'no')+",toolbar=no,menubar=yes,titlebar=no,status=no,screenX=0,screenY=0");
	//var newWindow = open(mediaURL, "ImagePopup", "width="+width+",height="+height+",scrollbars=no,resizable=yes,toolbar=no,menubar=yes,titlebar=no,status=no,screenX=0,screenY=0");
}

document.toggleDiv = function(sourceEl, toDiv, value, hideDivs, forceHide) {
	var el = document.getElementById(toDiv);
	
	if(!el || !sourceEl)
		return;

	if(sourceEl.nodeName.toLowerCase() == 'select') {
		el.style.display = sourceEl.options[sourceEl.selectedIndex].value == value ? 'block' : 'none';
		if(hideDivs && (forceHide || el.style.display == 'block')) {
			for(i = 0; i < hideDivs.length; i++) {
				var hideEl = document.getElementById(hideDivs[i]);
				if(hideEl) {
					hideEl.style.display = 'none';
				}
			}
		}
	} else if(sourceEl.nodeName.toLowerCase() == 'input') {
		if(sourceEl.type.toLowerCase() == 'radio') {
			el.style.display = sourceEl.value == value ? 'block' : 'none';
			if(hideDivs && (forceHide || el.style.display == 'block')) {
				for(i = 0; i < hideDivs.length; i++) {
					var hideEl = document.getElementById(hideDivs[i]);
					if(hideEl) {
						hideEl.style.display = 'none';
					}
				}
			}
		} else {			
			if(sourceEl.value == value) {
				el.style.display = sourceEl.checked ? 'block' : 'none';
				if(hideDivs && (forceHide || el.style.display == 'block')) {
					for(i = 0; i < hideDivs.length; i++) {
						var hideEl = document.getElementById(hideDivs[i]);
						if(hideEl) {
							hideEl.style.display = 'none';
						}
					}
				}
			}
		}
	}
}