$(document).ready(function() {
	//insert Send Info content
	if ( $.browser.msie ) {
		return false;
	}
	$('#sendInfoContent').load('/include/sendInfoContent.html',function(){
		//attach close behavior for link in content loaded
		$('#hideSendInfo').click(function() {
			$('#sendInfoContent').slideUp('medium');
			return false;
		});
		
		//attach form validation
		$("#frmSendInfo").submit(function() {
			if(!validateSendInfo()){
				return false;
			}
			var data = "";
			for (var i=0; i<this.elements.length; i++) {
				data+= this.elements[i].name;
				data+= "=";
				data+= escape(this.elements[i].value);
				data+= "&";
			}
			return !sendData(data);
		});
	});
	
	//hide Send Info content
	$('#sendInfoContent').hide();
	
	//modify link to show (not link) Send Info content
	$('#linkToSendInfo').click(function() {
		$('#sendInfoContent').slideDown('medium',function(){
			$('#email').focus();
		});
		return false;
	});
		
	//attach form validation for LogIn
    $("#signin").submit(function() {
		return validateSignIn();
    });
	
	//labelify (add default input text)
	//$("#username").labelify({ text: function(input) { return "<enter username>"; } });
	//$("#password").labelify({ text: function(input) { return "<enter password>"; } });

});


function sendData(data) {
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			parseResponse(request);
		};
		request.open( "POST", "/sendInfoProcess.php", true );
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		request.send(data);
		return true;
	} 
	else {
		return false;
	}
}

function parseResponse(request) {
	var r = document.getElementById("msg");
	if (request.readyState == 4) {
		if (request.status == 200) {
			r.innerHTML = request.responseText;
			document.getElementById("btnSendInfoSubmit").disabled=false;
			document.getElementById("email").disabled=false;

			/*var strResult = request.responseText;

			var objCurrent = document.getElementById('msg');
			var objReplacement = document.createElement('h3');
			var objAnchor = document.createElement('a');

			objReplacement.setAttribute('id', 'msg');
			objAnchor.setAttribute('id', 'target');
			objAnchor.setAttribute('href', '#target');
			objAnchor.appendChild(document.createTextNode(strResult));
			objReplacement.appendChild(objAnchor);

			if (objCurrent)
				objCurrent.parentNode.replaceChild(objReplacement, objCurrent);
			else
			{
				var objContent = document.getElementById('content');
				objContent.appendChild(objReplacement);
			}
			objAnchor.focus();*/
		}
	}
	else {
		//l.innerHTML="<img src=\"images/loading2.gif\" alt=\"\" \/> submitting...";
		r.innerHTML = "<p class=\"error\">Submitting...<\/p>";
		document.getElementById("btnSendInfoSubmit").disabled=true;
		document.getElementById("email").disabled=true;
	}
}
