function GetXmlHttpObject()
 {
   var objXMLHttp=null;
   
   if (window.XMLHttpRequest)
      objXMLHttp=new XMLHttpRequest();
   else if (window.ActiveXObject)
      objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
   
   return objXMLHttp
 }
function genMesgError(divId,strMesg)
 {
	 
	divId.style.display = "";
	if(divId.style.display == "")
	{
		//document.getElementById("ErrorTitle").style.display="";
		//document.getElementById("Uname").focus();
	}
	divId.style.color = "#FF3333";
	divId.align = "left";
	divId.style.padding = "8px 8px 8px 8px";
	if (divId.innerHTML == "")
		{
		divId.innerHTML = "<div style='padding-bottom:5px;'></div>"; 
		}
	strMesg = "&nbsp;&nbsp;<b style='color:red'>-</b>&nbsp;<font size='2'>"+ strMesg +"</font><br>";

	divId.innerHTML+=strMesg;
	calcHeightChild();
 }
function ChangeUserName(uname,pass,div,oldusername)
{
	var username = document.getElementById(uname).value;
	var confirmpassword = document.getElementById(pass).value;
	var divGen = document.getElementById(div);
	var http=GetXmlHttpObject()
	if (http==null)
     {
		alert ("Browser does not support HTTP Request")
		return false;
     }
	
var url ="query/SaveAccountSettings.php";
url = url+"?uname="+username+"&confirmpass="+confirmpassword+"&olduname="+oldusername;

	http.open("GET",url,true);
	
    http.onreadystatechange = 
	 function()
	  {
		if (http.readyState == 4)
		 {
			if(http.status==200) 
			{
			
				var response = http.responseText;
		
				if(response==1)
				{
					divGen.style.border="1px solid green";
					divGen.style.backgroundColor = "white";
					divGen.style.color = "green";
					divGen.innerHTML="<b style=color:green>Username Successfully Saved.<br><i>Please reload the System in order to take effect changes you had made.</i></b>";
					setTimeout(function(){
										window.location='Settings.php';
										},1500);
					calcHeightChild();
				}
				else
				{
					divGen.style.border="1px solid red";
					divGen.style.backgroundColor = "white";
					divGen.style.color = "red";
					divGen.innerHTML="<b style=color:red>Incorrect Password or Username Already Exist.</b>";
					calcHeightChild();
				}
				
				
					
			}
		 }
	  }
    http.send(null);
	return false;


}




function ChangePassword(uname,opass,npass,cpass,div)
{
	var username=uname;
	var oldpassword = document.getElementById(opass).value;
	var newpassword = document.getElementById(npass).value;
	var cpassword = document.getElementById(cpass).value;
	var divGen = document.getElementById(div);
	if(oldpassword==""||newpassword==""||cpassword=="")
	{
		divGen.style.border="1px solid red";
		divGen.style.backgroundColor = "white";
		divGen.style.color = "red";
		divGen.innerHTML="<b style=color:red>Please don't leave blanks.</b>";
		calcHeightChild();
	}
	else if(newpassword!=cpassword)
	{
		divGen.style.border="1px solid red";
		divGen.style.backgroundColor = "white";
		divGen.style.color = "red";
		divGen.innerHTML="<b style=color:red>You new Password didn't match.</b>";
		calcHeightChild();
	}
	
	else
	{
	var http=GetXmlHttpObject()
	if (http==null)
     {
		alert ("Browser does not support HTTP Request")
		return false;
     }
	
var url ="query/SavePassword.php";
url = url+"?uname="+username+"&opass="+oldpassword+"&npass="+newpassword+"&cpass="+cpassword;

	http.open("GET",url,true);
	
    http.onreadystatechange = 
	 function()
	  {
		if (http.readyState == 4)
		 {
			if(http.status==200) 
			{
			
				var response = http.responseText;
		
				if(response==1)
				{
					divGen.style.border="1px solid green";
					divGen.style.backgroundColor = "white";
					divGen.style.color = "green";
					divGen.innerHTML="<b style=color:green>Password Successfully Saved.</b>";
					setTimeout(function(){
										window.location='Settings.php';
										},1500);
					calcHeightChild();
				}
				else
				{
					divGen.style.border="1px solid red";
					divGen.style.backgroundColor = "white";
					divGen.style.color = "red";
					divGen.innerHTML="<b style=color:red>"+response+"</b>";
					calcHeightChild();
				}
				
				
					
			}
		 }
	  }
    http.send(null);
	return false;

	}
}

function Search(key)
{
	var s = document.getElementById(key).value;
	window.location = "SearchEvents.php?key="+s;
}

function UpdatePersonalInfo(Fname,Mname,Lname,Cnumber,Address,Eadd,gender,cstatus,datepicker,POB,citi,office,OAddress,OtNo,Fax,designation,YIS,username,div)
{
	document.getElementById(div).innerHTML="";	
	 var intValidNum = "1234567890";
	 var intValidContactNo = "1234567890+#()-";
	 var strValidName = " AaBbCcDdEeFfGgHhIiJjKkLlMmNnÑñOoPpQqRrSsTtUuVvWwXxYyZz-";
	 var strValidAdd = " AaBbCcDdEeFfGgHhIiJjKkLlMmNnÑñOoPpQqRrSsTtUuVvWwXxYyZz-'.,()&:;/#1234567890";
	 var strValidEAdd = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnÑñOoPpQqRrSsTtUuVvWwXxYyZz-._@1234567890";
	 var strValidChar = " AaBbCcDdEeFfGgHhIiJjKkLlMmNnÑñOoPpQqRrSsTtUuVvWwXxYyZz-',";
	 var errCount = 0;
	 var errlastname = 0, errfirstname = 0, errmiddlename = 0, errSchoolElem = 0,errSchoolHS = 0,errSchoolCol = 0,errSchoolPost = 0, errnationality = 0,erryear=0,
	 	 errstreetaddress = 0, errbaranggay = 0, erraddress = 0, errcontact = 0,errcontact1= 0, errcourse= 0,erremailaddress = 0;
		var divGen = document.getElementById(div);
	
		var email = document.getElementById(Eadd).value;
		var gen = document.getElementById(gender).value;
		var stat = document.getElementById(cstatus).value;
		var fname = document.getElementById(Fname).value;
		var mname = document.getElementById(Mname).value;
		var lname = document.getElementById(Lname).value;
		var cnumber = document.getElementById(Cnumber).value;
		var address = document.getElementById(Address).value;
		var bday = document.getElementById(datepicker).value;
		var Place = document.getElementById(POB).value;
		var citizenship = document.getElementById(citi).value;
		var opis = document.getElementById(office).value;
		var Oadd = document.getElementById(OAddress).value;
		var telNo = document.getElementById(OtNo).value;
		var fx = document.getElementById(Fax).value;
		var des = document.getElementById(designation).value;
		var ys = document.getElementById(YIS).value;
		divGen.innerHTML="";
	if(email=="")
		{
			genMesgError(divGen,"Please specify your Email Address.");
			errCount++;
		}
		else
		{
			if(email.length<5)
				{
					genMesgError(divGen,"Email Adress Must be 5 Characters and above.");
					errCount++;
				}
				 for (num = 0; num < email.length; num++)
				{
					var strChar = email.charAt(num);
					if (strValidEAdd.indexOf(strChar) == -1)
					 {
						erremailaddress++;
		 				errCount++;
					 }
					
				}
				 if (erremailaddress > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Email Address</b> contains invalid characters.");
					 errCount++;
				 }
				
		}
		if(fname=="")
		{
			genMesgError(divGen,"Please specify your First Name.");
			errCount++;
		}
		else
		{
			if(fname.length>50)
			{
				genMesgError(divGen,"First Name Must be 50 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < fname.length; num++)
				{
					var strChar =fname.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						errfirstname++;
		 				errCount++;
					 }
					
				}
			
			 if (errfirstname > 0)
				 {
				 genMesgError(divGen,"Your <b style='color:red'>First Name</b> contains invalid characters.");
				 errCount++;
				 }
		
		}
		
		
		
		
		if(mname=="")
		{
			genMesgError(divGen,"Please specify your Middle Name.");
			errCount++;
		}
		else
		{
			if(mname.length>50)
			{
				genMesgError(divGen,"Middle Name Must be 50 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < mname.length; num++)
				{
					var strChar =mname.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						errmiddlename++;
		 				errCount++;
					 }
					
				}
			
			 if (errmiddlename > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Middle Name</b> contains invalid characters.");
					 errCount++;
				 }
		}
		
		
		if(lname=="")
		{
			genMesgError(divGen,"Please specify your Last Name.");
			errCount++;
		}
		else
		{
			if(lname.length>50)
			{
				genMesgError(divGen,"Last Name Must be 50 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < lname.length; num++)
				{
					var strChar =lname.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						errlastname++;
		 				errCount++;
					 }
					
				}
			
			 if (errlastname > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>last Name</b> contains invalid characters.");
					 errCount++;
				 }
		}
		
		
		
		if(cnumber=="")
		{
			genMesgError(divGen,"Please specify your Contact Number.");
			errCount++;
		}
		else
		{
			if(cnumber.length>11||cnumber.length<6)
			{
				genMesgError(divGen,"Contact Number Must be 50 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < cnumber.length; num++)
				{
					var strChar =cnumber.charAt(num);
					if (intValidNum.indexOf(strChar) == -1)
					 {
						errcontact++;
		 				errCount++;
					 }
					
				}
			
			 if (errcontact > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Contact Number</b> contains invalid characters.");
					 errCount++;
				 }
		}
			
		
		
	if(address=="")
		{
			genMesgError(divGen,"Please specify your Address.");
			errCount++;
		}
		else
		{
			if(address.length>100)
			{
				genMesgError(divGen,"Address Must be 100 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < address.length; num++)
				{
					var strChar =address.charAt(num);
					if (strValidAdd.indexOf(strChar) == -1)
					 {
						erraddress++;
		 				errCount++;
					 }
					
				}
			
			 if (erraddress > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Address</b> contains invalid characters.");
					 errCount++;
				 }
		}
		
		
	if(Place=="")
		{
			genMesgError(divGen,"Please specify your Place of Birth.");
			errCount++;
		}
		else
		{
			if(Place.length>100)
			{
				genMesgError(divGen,"Place of Birth Must be 100 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < Place.length; num++)
				{
					var strChar =Place.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						errSchoolElem++;
		 				errCount++;
					 }
					
				}
			
			 if (errSchoolElem > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Place of Birth</b> contains invalid characters.");
					 errCount++;
				 }
		}
		
		
			if(citizenship =="")
		{
			genMesgError(divGen,"Please specify your Citizenship.");
			errCount++;
		}
		else
		{
			if(citizenship.length>50)
			{
				genMesgError(divGen,"Citizenship year Must be 50 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < citizenship.length; num++)
				{
					var strChar =citizenship.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						erryear++;
		 				errCount++;
					 }
					
				}
			
			 if (erryear > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Citizenship</b> contains invalid characters.");
					 errCount++;
				 }
		}
		
		
		
		if(opis=="")
		{
			genMesgError(divGen,"Please specify your Office Name.");
			errCount++;
		}
		else
		{
			if(opis.length>100)
			{
				genMesgError(divGen,"Office Name Must be 100 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < opis.length; num++)
				{
					var strChar =opis.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						errSchoolHS++;
		 				errCount++;
					 }
					
				}
			
			 if (errSchoolHS > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Office</b> contains invalid characters.");
					 errCount++;
				 }
		}
		
		
		if(Oadd=="")
		{
			genMesgError(divGen,"Please specify your Office Address.");
			errCount++;
		}
		else
		{
			if(Oadd.length>100)
			{
				genMesgError(divGen,"your Office Address Must be 100 Characters and below.");
				errCount++;
			}
			
			for (num = 0; num < Oadd.length; num++)
				{
					var strChar =Oadd.charAt(num);
					if (strValidName.indexOf(strChar) == -1)
					 {
						erryear++;
		 				errCount++;
					 }
					
				}
			
			 if (erryear > 0)
				 
				 {
					 genMesgError(divGen,"Your <b style='color:red'>Office Address</b> contains invalid characters.");
					 errCount++;
				 }
		}

if(errCount==0)
{
	var http=GetXmlHttpObject()
	if (http==null)
     {
		alert ("Browser does not support HTTP Request")
		return false;
     }
	
var url ="../php/query/UpdateInfo.php";
	url = url+"?&email="+email+"&fname="+fname+"&mname="+mname+"&lname="+lname+"&cnumber="+cnumber+"&address="+address+"&bday="+bday+"&Place="+Place+"&citizenship="+citizenship+"&opis="+opis+"&Oadd="+Oadd+"&telNo="+telNo+"&fx="+fx+"&des="+des+"&ys="+ys+"&username="+username+"&gender="+gen+"&cstatus="+stat;

	http.open("GET",url,true);
	
    http.onreadystatechange = 
	 function()
	  {
		if (http.readyState == 4)
		 {
			if(http.status==200) 
			{	
				var response = http.responseText;
				
				
			 if(response==1)
				{
                    document.getElementById("ErrorTitle").innerHTML="";		
					divGen.style.display="";
					divGen.style.border="1px solid green";
					divGen.style.backgroundColor = "white";
					divGen.style.color = "green";
					divGen.innerHTML='<b style=color:green><span style=font-size:12px>Information Successfully Updated.</span></b>';

					}
					else
					{

					alert("An Error has Occured.");
					}
					
				}
				
		        
			//alert(response);
			}
		 }
	  
    http.send(null);
	
}

}



