/*	LOGIN SCRIPT FOR THE LOGIN SECTION	
--------------------------------------------------------------------------------------------------------------*/

function prepareLoginForm(){
	if($('loginForm')){
		var theForm = $('loginForm');
		theForm.onsubmit = function(){
			doRegister(theForm);
			return false;
		}		
	}	
}

window.addEvent('domready', function() {
	prepareLoginForm();
});


// Perform the query
function doRegister(myform){
	get(myform);
}

// AJAX request function
var http_request = false;
function makePOSTRequest(url, parameters) {

      http_request = false;
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            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) {
         //alert('Cannot create XMLHTTP instance');
         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);
      
   }

// write the new data to the page	 
  function alertContents() {	
     if (http_request.readyState==1||http_request.readyState==2||http_request.readyState==3) { 
           document.getElementById('loginMessage').innerHTML = "<p class=\"loading\">&nbsp;&nbsp;&nbsp;Please wait ...</p>";
     }  
	//toggleOpacity();
	
     if (http_request.readyState == 4)
     {
		
        if (http_request.status == 200)
        {	
			
			result = http_request.responseText;

			if(result == "Redirecting to full specifications page..."){				
				document.location.href='/the_building/full_specifications';
			}
			if(result == "Redirecting to full schedule page..."){
				document.location.href='/the_building/full_schedule_and_floorplans';
			}			
			if(result !== "Redirecting to full specifications page..." || result !== "Redirecting to full schedule page..."){			
				document.getElementById('loginMessage').innerHTML = result;
				prepareLoginForm();
			}
        }
        else
        {
           //alert('There was a problem with the request.'+result);
			prepareLoginForm();
        }
     }
  }
   
// Create the string to be sent via post
function get(obj) {	
		var poststr = 	"uname=" + encodeURI( document.getElementById("uname").value ) + "&upass=" + encodeURI( document.getElementById("uPass").value ) + "&ajaxrequest=1&page=" + encodeURI( document.getElementById("page").value );		
		//console.log(poststr);
		makePOSTRequest('/inc/doLogin.inc.php', poststr);
}

/* Fade in thand element
 	http://brainerror.net/scripts/javascript/blendtrans/
--------------------------------------------------------------------------------------------------------------*/


function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
}

// Toggle opactiy of an element

function toggleOpacity(){
	theForm = document.getElementById('loginWrapper');
	theForm.style.opacity = "0";
	shiftOpacity("loginWrapper", '1000');	
}

/* Change the intro text on the gallery page
--------------------------------------------------------------------------------------------------------------*/

window.addEvent('domready', function() {

	if($('intro-gallery')){
		$('intro-gallery').firstChild.data = "View the Heron Tower gallery for aerial, exterior and interior views of the building.";			
	}
});
