// JavaScript Document
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function textDefaultValue(obj, str) {
	tvalue = obj.value.trim();
	if(tvalue=="")
	{
		obj.value = str;
	}
}
function textClearValue(obj,str) {
	tvalue = obj.value.trim();
	if(tvalue==str)
	{
		obj.value = "";
	}
}
function divClose(divid) {
	document.getElementById(divid).style.display = "none";
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
 
function getHTTPObject() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}
var http = getHTTPObject();
var loginerror = "";
function login()
{
	var username = document.loginfrm.username.value;
	var password = document.loginfrm.password.value;
	var url = "login.php";
	var params = "username="+username+"&password="+password;

	http.open("POST", url, true);
	
	//Send the proper header infomation along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 && http.status == 200) {
		response = http.responseText;
		if(response=="false") {
			if(loginerror != "")
			{
				document.getElementById("loginerror").innerHTML = loginerror;
			}
			document.getElementById("loginerror").style.display = "block";
		}
		else
		{
//			window.location.href="documents.php";
			document.getElementById("maincontentrightdiv").innerHTML = response;
		}
	}
};
	http.send(params);

}

function sendpassword()
{
	email = document.forgetpasswordfrm.email.value.trim();
	if(echeck(email)==false)
	{
		alert("Invalid Email Address");
	}
	else
	{
		if(loginerror == "" || loginerror == null )
		{
			loginerror = document.getElementById("loginerror").innerHTML;
		}
		var url = "sendpassword.php";
		var params = "email="+email;
		http.open("POST", url, true);
		//Send the proper header infomation along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
	
		http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			response = http.responseText;
			document.getElementById("loginerror").innerHTML = response;
		}
		};
		http.send(params);
	}
}
function subscribe()
{
	email = document.subscribeform.email.value.trim();
	if(echeck(email)==false)
	{
		alert("Invalid Email Address");
	}
	else
	{
		var url = "subscribe.php";
		var params = "email="+email;
		
		http.open("POST", url, true);
		
		//Send the proper header infomation along with the request
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
	
		http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			response = http.responseText;
			document.getElementById("sitresponse").style.display = "block";
			document.getElementById("sitresponse").innerHTML = response;
		}
		};
		http.send(params);
	}
}

function logout() {
		var url = "logout.php";
		http.onreadystatechange= function() {//Call a function when the state changes.
			if(http.readyState == 4 && http.status == 200) {
				response = http.responseText;
				window.location.href="index.html";
			}
		};
		http.open("GET",url,true);
		http.send(null);
}