var xmlHttpGetList = createXmlHttpRequestObject();
var msg = '<center><font color="Green"><b>Loading....</b></font></center>';
var sts=false;
var divPos = '';
var divID = '';
var iframep = '';
var editFrdDiv = '';

var yourURL = "mylifewall.com";
function outLinks()
{
	var outLink;
	if (document.getElementsByTagName('a')) 
	{
		for (var i = 0; (outLink = document.getElementsByTagName('a')[i]); i++) 
		{
			if (outLink.href.indexOf(yourURL) == -1) 
			{
				outLink.setAttribute('target', '_blank');
			}
		}
	}
}

window.onload = function() 
{
	outLinks();
}



function createXmlHttpRequestObject()
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	}
}
	catch(e)
	{
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
		{
			try
			{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest object.");
	else
		return xmlHttp;
}

/* initiate HTTP request to retrieve suggestions for the current keyword */
function LoadModule()
{
	name = document.getElementById("txt_name").value;
	pass = document.getElementById("txt_pwd").value;
/*
	var str='';
	    for(prop in document.getElementById("username")){
	    	str += prop + ":" + document.getElementById("username")[prop] + "\n"; 
	    }
    document.getElementById('bb').value=str;
*/    
	if(xmlHttpGetList)
	{
	        try
		{
			if (xmlHttpGetList.readyState == 4 || xmlHttpGetList.readyState == 0)
        		{//alert("login.php?name="+name+"&password="+pass);
        			xmlHttpGetList.open("GET","checkLoginStatus.php?name="+name+"&password="+pass, true);
				xmlHttpGetList.onreadystatechange  = ProcessLoadModule;
				xmlHttpGetList.send(null);
			}
		}
		catch(e)
		{
			displayError("Can't connect to server:\n" + e.toString());
		}
      }
}


function ProcessLoadModule()
{
	if(xmlHttpGetList.readyState == 1)
	{
		document.getElementById("loginError").innerHTML = msg;
	}
	else if(xmlHttpGetList.readyState == 4)
	{
		if (xmlHttpGetList.status == 200)
		{
	        return GetData();
	    }
	}
}


function GetData()
{
	response = xmlHttpGetList.responseText;
	//alert(response);
	var str = response;
	//alert(str);
	if(str=='ok')
	{//alert('y');
		sts = true;
		document.loginForm.submit();
	}
	else{	//alert('n');
		e = document.getElementById("loginError");
		e.innerHTML = "<font color='red'><center><b>"+str+"</b></center></font>";
		return false;
	}
}

function getResults(url, divName)
{
	//alert(url);
	//alert(divName);
	divPos = divName;
	if(xmlHttpGetList)
	{
	        try
		{
			if (xmlHttpGetList.readyState == 4 || xmlHttpGetList.readyState == 0)
        		{
        			xmlHttpGetList.open("GET",url, true);
				xmlHttpGetList.onreadystatechange = processResults;
				xmlHttpGetList.send(null);
			}
		}
		catch(e)
		{
			displayError("Can't connect to server:\n" + e.toString());
		}
      }
}


function processResults()
{
	if(xmlHttpGetList.readyState == 1)
	{
		if(divPos != '')
			document.getElementById(divPos).innerHTML = msg;
	}
	else if(xmlHttpGetList.readyState == 4)
	{
		if (xmlHttpGetList.status == 200)
		{
	        showResults();
	    }
	}
}


function showResults()
{
	response = xmlHttpGetList.responseText.split('|');//alert(response);
	if(response[1]){
	divID = response[1];
	//alert(divID);
	//alert(response[0]);
	e = document.getElementById(divID);
	e.innerHTML = response[0];
	if(divID != divPos)
		document.getElementById(divPos).innerHTML = "";		
	}
}
function editPic()
{
	document.getElementById('editPic').innerHTML = "<form enctype='multipart/form-data' name='uploadForm' method='post' action='uploader.php'><input type='hidden' name='MAX_FILE_SIZE' value=7000000 /><input name='uploadedfile' type='file' size='10' class='picupload' onChange='document.uploadForm.submit();'/></form><input type='button' value='cancel' onClick='document.getElementById(\"editPic\").innerHTML=\"\";'>";
}


function varificatioProcess(url)
{
	//alert(url);
	//alert(divName);
	//var dob = document.getElementById("birthday_year").value+"-"+document.getElementById("birthday_month").value+"-"+document.getElementById("birthday_day").value;
	//alert(dob);
	if(xmlHttpGetList)
	{
	        try
		{
			if (xmlHttpGetList.readyState == 4 || xmlHttpGetList.readyState == 0)
        		{
        			xmlHttpGetList.open("GET",url, true);
				xmlHttpGetList.onreadystatechange = varificatioProcessResults;
				xmlHttpGetList.send(null);
			}
		}
		catch(e)
		{
			displayError("Can't connect to server:\n" + e.toString());
		}
      }
}


function varificatioProcessResults()
{
	if(xmlHttpGetList.readyState == 1)
	{
		document.getElementById("wrongCode").innerHTML = msg;
	}
	else if(xmlHttpGetList.readyState == 4)
	{
		if (xmlHttpGetList.status == 200)
		{
	        varificationResults();
	    }
	}
}


function varificationResults()
{
	response = xmlHttpGetList.responseText;
	if(response=="correct")
	{
		//var dob = document.getElementById("birthday_year").value+"-"+document.getElementById("birthday_month").value+"-"+document.getElementById("birthday_day").value;
		//alert(dob);
		//getResults("registration.php?newUserName="+document.getElementById('r_username').value+"&password="+document.getElementById('password').value+"&firstName="+document.getElementById('name').value+"&useremail="+document.getElementById('useremail').value+"&dob="+dob, "wrongCode");
		document.registerForm.submit();
	}
	else{
		document.getElementById("wrongCode").innerHTML = response;		
		return false;
	}
}

function do_confirm()
{	
	//alert(str);
	var confirmMsg = "Are you sure to delete this?";
	var agree = confirm(confirmMsg);
	if(agree)
	{
		return true;
		//getResults(str, 'showBlog');
	}
	else
	return false;
}

function toggle_visibility(id) {
	var e = document.getElementById(id);
	if(e.style.display == 'block')
		  e.style.display = 'none';
	else
		  e.style.display = 'block';
}

function get_radio_value()
{
for (var i=0; i < document.form1.privacy.length; i++)
   {
   if (document.form1.privacy[i].checked)
      {
      	var rad_val = document.form1.privacy[i].value;
      	return rad_val;
      }
   }
}

function addFriend(id)
{
	document.getElementById("showBlog").innerHTML = "<form name='frdGroup'>Choose a Group for this person.<br><input type='radio' name='chooseGroup' id='chooseGroup' value='ff' CHECKED>Family and Friends<input type='radio' name='chooseGroup' id='chooseGroup' value='sg'>Sharewall group<br><input type='button' id='saveStatus' name='saveStatus' value='Save' onClick='saveFriendValue("+id+");' ></form>";
}

function saveFriendValue(id)
{
	group = get_group_value();//getResults(\"acceptFriend.php?id=$reqId\", \"frd_".$req."\");
	getResults("addFriend.php?id="+id+"&group="+group, "showBlog");
}

function get_group_value()
{
for (var i=0; i < document.frdGroup.chooseGroup.length; i++)
   {
   if (document.frdGroup.chooseGroup[i].checked)
      {
      var rad_val = document.frdGroup.chooseGroup[i].value;
      }
   }
   return rad_val;
}

function acceptFriend(id, divValue)
{
	document.getElementById(divValue).innerHTML = "<form name='frdGroup'>Choose a Group for this person.<br><input type='radio' name='chooseGroup' id='chooseGroup' value='ff' CHECKED>Family and Friends<input type='radio' name='chooseGroup' id='chooseGroup' value='sg'>Sharewall group<br><input type='button' id='saveStatus' name='saveStatus' value='Save' onClick='acceptFriendValue("+id+", \""+divValue+"\");' ></form>";
}

function acceptFriendValue(id, divValue)
{
	group = get_group_value();
	getResults("acceptFriend.php?id="+id+"&group="+group+"&div="+divValue, ""+divValue);
}

function checkSearch()
{
	if(document.searchF.searchVal.value=="")
	{
		alert("Please enter some value in search box.");
		return false;	
	}
	return true;
}

function checkIfBlank(ee)
{
	if(ee == "")
	{
		alert("Please enter some value in box.");
		return false;	
	}
	return true;
}
