var http_request = false;


function moveArrow(e) {
		var elem;
		if (e == "default"){
			elem = orig_location;
		}else{			
			elem = this.id;
		}
		if(elem == "nav_home"){
			var newLeft = 205;	
		}else{
			var curleft = 0;
			var obj = document.getElementById(elem);
			var curwidth = obj.offsetWidth;
			if (obj.offsetParent) {
			do {
					curleft += obj.offsetLeft;
				} while (obj = obj.offsetParent);
			}
			var newleft= (curwidth/2) + curleft-10;
			document.getElementById('selected').style.visibility = 'visible';
		}
		var attributes = { 
			opacity: { to: 1 },
			points: { to: [newleft, 7] }
		}; 
		var animOut = new YAHOO.util.Motion('selected', attributes, 2, YAHOO.util.Easing.elasticOut); 
		animOut.animate();
	}
function moveBack(){
			var position, movebackattributes;
			if (orig_location == "nav_home"){
				position = getX('hd') + 55;
				var movebackattributes = { 
						opacity: { to: 0 },
						points: { to: [position, 7] }
				}; 
			}else{
				moveArrow("default");
			}
			var animBack = new YAHOO.util.Motion('selected', movebackattributes, 1, YAHOO.util.Easing.bounceOut); 
			animBack.animate();
}	
function redirectEmail(form){
   var str = document.forms[form]['email'].value;
   if((str.indexOf(".") > 2) && (str.indexOf("@") > 0)){
		return true;
   }else{
     	// open a welcome message as soon as the window loads
		document.forms[form]['email'].value = "Your Email Address";
		Shadowbox.open({
			content:    '<div id="error-msg">Sorry, but the email address you provided does not appear to be valid.</div>',
			player:     "html",
			title:      "Sorry!",
			height:     100,
			width:      350
		});
		return false;
   }
}
function getY( elemID )
{
	oElement = document.getElementById(elemID);
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetTop;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
function getX( elemID )
{
	oElement = document.getElementById(elemID);
	var iReturnValue = 0;
	while( oElement != null ) {
	iReturnValue += oElement.offsetLeft;
	oElement = oElement.offsetParent;
	}
	return iReturnValue;
}
function windowWidthSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;
}
function hideSplashGuards(splashLeftDiv, splashRightDiv){
	document.getElementById(splashLeftDiv).style.display = "none";
	document.getElementById(splashRightDiv).style.display = "none";
}
function attachSplashGuards(splashDiv, splashLeftDiv, splashRightDiv){
	s_top = getY(splashDiv);
	sl_left = getX(splashDiv);
	newsl_left = (sl_left - document.getElementById(splashLeftDiv).childNodes[0].width);
	sr_left = sl_left + document.getElementById(splashDiv).width;
	sr_left2 = getX(splashDiv) + document.getElementById(splashDiv).offsetWidth;
	document.getElementById(splashLeftDiv).style.visibility = "visible";
	document.getElementById(splashRightDiv).style.visibility = "visible";
	document.getElementById(splashLeftDiv).style.display = "block";
	document.getElementById(splashRightDiv).style.display = "block";
	document.getElementById(splashLeftDiv).style.top = s_top + 'px';
	document.getElementById(splashLeftDiv).style.left = newsl_left + 'px';
	document.getElementById(splashRightDiv).style.top = s_top + 'px';
	document.getElementById(splashRightDiv).style.left = sr_left +'px';
	document.getElementById(splashRightDiv).style.overflow = "hidden";
	document.getElementById(splashRightDiv).style.overflowX = "hidden";

	widthFactor = windowWidthSize() - 995;
	if(widthFactor >0 && widthFactor){
		document.getElementById(splashRightDiv).style.width = (widthFactor/2) +"px";
	}else{
		hideSplashGuards(splashLeftDiv, splashRightDiv);
	}
}
function iterateMenuListeners(){
	var ids = ["nav_about", "nav_gallery", "nav_menus", "nav_events", "nav_vip", "nav_contact"];
	YAHOO.util.Event.addListener(ids, 'mouseover', moveArrow);
	YAHOO.util.Event.addListener(ids, 'mouseout', moveBack);
	if ((orig_location == "nav_home") || (orig_location == "nav_contact") || (orig_location == "nav_events")|| (orig_location == "nav_menus") || (orig_location == "nav_about")){
		var onclickaction = function(){if(this.value=='Your Email Address'){this.value=''};};
		var onbluraction = function (){if(this.value==''){this.value='Your Email Address'};};
		var submitform = function() {validateEmail('vip_form');};
		YAHOO.util.Event.addListener('emailadd_tf', 'click', onclickaction, document.getElementById('emailadd_tf'),true);
		YAHOO.util.Event.addListener('emailadd_tf', 'blur', onbluraction, document.getElementById('emailadd_tf'),true);
	}	
}
function pageSetup(){
	iterateMenuListeners();
	attachSplashGuards('splash_img','splash_left','splash_right');
	moveArrow("default");
	if (orig_location == "nav_vip"){
		document.getElementById('vipform1').elements['email'].value = getURLParam("email");
	}else if((orig_location == "nav_gallery") && (getURLParam('tour'))){
    // open a welcome message as soon as the window loads
		Shadowbox.open({
			content:    '../imgs/gallery/dungeon_pano_small.mov',
			player:     "qt",
			title:      "Dungeon Lounge - 360&deg; Tour",
			height:     600,
			width:      1024
		});
	
	};

}
function pageResize(){
	attachSplashGuards('splash_img','splash_left','splash_right');
	moveArrow("default");
}
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (
		aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function validateVIP(){
	var theForm = document.forms['vipform1'];
	var errors = "";
	errors += validateEmpty(theForm.name, "name");
	errors += validateEmail(theForm.email);
	errors += validatePhone(theForm.mobile1.value+theForm.mobile2.value+theForm.mobile3.value);
	errors += validateAge(parseInt(theForm.bday1.value), parseInt(theForm.bday2.value), parseInt(theForm.bday3.value));
	errors += validateEmpty(theForm.zipcode, "zipcode", 5);
	if (!(errors == "")){
		htmlErrors = errors.replace(/\n/g, "<br /><br />");
		Shadowbox.open({
				content:    '<div id="error-msg"><strong>Please correct the following item(s):</strong><br /><br />'+htmlErrors+'</div>',
				player:     "html",
				title:      "Oops! There appears to be some problems with your submission",
				height:     250,
				width:      600
		});
		return false;
	}
	else{
		return true;
	}
}
function validateEmpty(fld, name, minlength) {
	var error = "";
	if (!minlength) {
		minlength = 0;
	}
	if (fld.value.length < minlength){	
		error = "The "+name+" field should be "+minlength+" characters long";
	}
    if (fld.value.length == 0) {
        error = "The "+name+" field has not been filled in.\n"
    }
    return error;  
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        error = "The email address you've entered contains illegal characters.\n";
    }
    return error;
}
function validateAge(month, day, year)
{
	/* the minumum age you want to allow in */
	var min_age = 21;
	var error = "";
	var theirDate = new Date();
	theirDate.setFullYear((year + min_age), (month-1), day);
	var yearString = year.toString();
	if (yearString.length !== 4){
		error = "Please enter your birthday in the following format. (MM/DD/YYYY)\n";
	}else{
		var today = new Date;
		if ( (today.getTime() - theirDate.getTime()) < 0) {
			error = "You must be at least 21 years of age to join the VIP lounge.\n";
		}
	}
	return error;
}
function validatePhone(phone) {
    var error = "";
    var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');    

   if (phone == "") {
        error = "You didn't enter a phone number.\n";
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number you've entered contains illegal characters.\n";
    } else if (!(stripped.length == 10)) {
        error = "The phone number you've entered is the wrong length. Make sure you included an area code.\n";
    }
    return error;
}
function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	Shadowbox.open({
			content:    '<div id="error-msg"><strong>We were unable to process your request because the following errors have occured:</strong><br /><br />We could not create a XMLHTTP instance<br /><br />If the problem persists please <a href="mailto:info@dungeonlounge.com">contact us</a></div>',
			player:     "html",
			title:      "Sorry, but it appears an error has occured.",
			height:     250,
			width:      600
	}); 
	 return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		//alert(http_request.responseText);
		 result = http_request.responseText;
		 Shadowbox.open({
			content:    '<div id="error-msg"><strong>You are now a member of the VIP Lounge. You will now begin recieving exclusive promotions and upcoming event notifications from us. Thank you for your continued patronage.</strong><br /><br /><div width="100%" align="center">You may opt out of receiving communications from us at anytime by simply emailing <a href="mailto:unsubscribe@dungeonlounge.com">unsubscribe@dungeonlounge.com</a></div></div><br />'+result,
			player:     "html",
			title:      "Congratulations!",
			height:     250,
			width:      600
		}); 		
	 } else {
		 Shadowbox.open({
			content:    '<div id="error-msg"><strong>We were unable to process your request because the following errors have occured:</strong><br /><br />Error: 001A <br /><br />If the problem persists please <a href="mailto:info@dungeonlounge.com?subject=VIP+Form+Error&body=Error+001A">contact us</a></div>',
			player:     "html",
			title:      "Sorry, but it appears an error has occured.",
			height:     250,
			width:      600
		}); 
	 }
  }
}

function get(obj) {
  var poststr = "name=" + encodeURI( document.getElementById("name").value ) +
				"&email=" + encodeURI( document.getElementById("email").value ) +
				"&mobile=" + encodeURI( document.getElementById("mobile1").value+document.getElementById("mobile2").value+document.getElementById("mobile3").value  ) + "&bday=" + encodeURI( document.getElementById("bday1").value+document.getElementById("bday2").value+document.getElementById("bday3").value  ) + "&zipcode=" + encodeURI( document.getElementById("zipcode").value ) ;
  makePOSTRequest('vippost.php', poststr);
}
