var whorotates= "";
var subjects = [];
var grades = [];
var classes = [];

/*8*******

START FROM PHP FILE

*************/

var getUrlParameterPlain = function getUrlParameterPlain(sParam) {
		
		var sPageURL = decodeURIComponent(window.location.search.substring(1)),
		sURLVariables = sPageURL.split('&'),
		sParameterName,
		i;

		for (i = 0; i < sURLVariables.length; i++) {
			sParameterName = sURLVariables[i].split('=');

			if (sParameterName[0] === sParam) {
				return sParameterName[1] === undefined ? true : sParameterName[1];
			}
		}
	};

var getUrlParameter = function getUrlParameter(sParam) {
		var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split("=");
		
		//alert(decUrl(url[1]));
		var durl = decUrl(url[1]);
		
		var urlvars = durl.split('&');
		/*var sPageURL = decodeURIComponent(window.location.search.substring(1)),
		sURLVariables = sPageURL.split('&'),*/
		var sParameterName,
		i;

		for (i = 0; i < urlvars.length; i++) {
			sParameterName = urlvars[i].split('=');

			if (sParameterName[0] === sParam) {
				return sParameterName[1] === undefined ? true : sParameterName[1];
			}
		}
		
		return getUrlParameterPlain(sParam);
	};
	


function getJasonDataOld(params) {
	//alert(params);
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "api/process.php?" + params + "&t=" + Math.random();
	//alert(url);
	xmlhttp.open("GET", url, false);
	xmlhttp.send();
	return xmlhttp.responseText;
}

function getJasonData(params,functionName) {
	//alert(params);
	$.ajax({
     type: "GET",
     url: 'api/process.php',
     data: params,
     success: function(response){
		 switch(functionName)
		 {
			 case "invite":
			 case "updatePassword":
				alert(response);
				break;
			 default:
				eval(functionName+"("+response+")");
				break;
		 }
		 
		 /*if(functionName =="updatePassword")
		 {
			 
		 }
		 else{
			eval(functionName+"("+response+")");
		 }*/
		 
		}
    });
}

function ajaxCall(theUrl)
{
	$.ajax({
	  url: theUrl,  
	  success:function(data) {
		 return data; 		 
		 //if(result.length == 0)
	  },
	error: function(data){
		return "error";
	}
   });
}

function getAsynchJasonData(params) {
	//alert(params);
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = "api/process.php?" + params + "&t=" + Math.random();
	//alert(url);
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
	return xmlhttp.responseText;
}

function sendDataByGet(params,tofile) {
	//alert(params);
	var xmlhttp;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	var url = tofile+"?" + params + "&t=" + Math.random();
	//alert(url);
	xmlhttp.open("GET", url, false);
	xmlhttp.send();
	//alert(xmlhttp.responseText);
	return xmlhttp.responseText;
}

function isEmail(email) {
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}


function facebook_login(name, facebook_id)
{
	var param = "action=LOGINFACEBOOK&name="+name+"&facebook_id="+facebook_id;
	var result = getJasonData(param);
	result = jQuery.parseJSON(result);
	localStorage.setItem('user_id',result[0].user_id);
	localStorage.setItem('name',result[0].name);
	localStorage.setItem('email',result[0].email);
	localStorage.setItem('password',result[0].password);
	localStorage.setItem('facebook_login',"true");
	var email = localStorage.getItem('email');
	var password = localStorage.getItem('password');
	alert(email + " " + password);
	window.location = "index.php?user_id="+result[0].user_id+"&email="+result[0].email;
}


function logout()
{
	var fb = localStorage.getItem('facebook_login');
	if(fb != "true")
	{
		window.location = "logout.php";
	}
	else{
		window.location = "logout.php?action=facebook";
	}
}

function doLogin(result)
{
	//alert(result);
	if(result == null)
	{
		alert("Invalid Credentials");
		return;
	}
	localStorage.setItem('user_id',result[0].user_id);
	localStorage.setItem('name',result[0].name);
	localStorage.setItem('email',result[0].email);
	localStorage.setItem('password',result[0].password);
	var email = localStorage.getItem('email');
	var password = localStorage.getItem('password');
	//alert("Redirect to : index.php?user_id="+result[0].user_id+"&email="+result[0].email);
	window.location = "index.php?user_id="+result[0].user_id+"&email="+result[0].email;

}
function indexLogin(result)
{
	alert("silapaho");
	if(result == "null" || result == null)
	{
		window.location = "login.php";
	}
	else{
		alert(result);
		result = jQuery.parseJSON(result);
		localStorage.setItem('user_id',result[0].user_id);
		localStorage.setItem('name',result[0].name);
		localStorage.setItem('email',result[0].email);
		localStorage.setItem('password',result[0].password);
		localStorage.setItem('user_type_id',result[0].user_type_id);
		
		//alert(result[0].user_id);
		$("#user_name").val(result[0].name +" " +result[0].surname);
		
		//var user = firebase.auth().currentUser;
		var email = localStorage.getItem('email');
		var password = localStorage.getItem('password');
		var user_id = localStorage.getItem('user_id');
		alert("email is " +email);
		if(email == null)
		{
			window.location = "login.php";
		}
	}
}

function getSchoolsGeneralSettings(schls)
{
	$('#the_school_id').html("");					
	for(i =0;i<schls.length;i++)
	{
		//alert(schls[i].school_name);
		$('#the_school_id').append(
			$('<option></option>').val(schls[i].school_id).html(schls[i].school_name)
		);
	}
}

function updatePassword(result)
{
	alert(result);
}

function saveSchoolData(result)
{	
	var user_id = localStorage.getItem('user_id');	
	var res = getAsynchJasonData("action=GETCAREERS&user_id="+user_id);
}

function invite(result)
{
	alert(result);
}

function careerInfo(result)
{
	
	var varsities = result[0].varsities.split(",");
	
	$("#wheretostudy").empty();
	$("#wheretostudy").append(
		$('<option></option>').val(0).html("Select A Varsity")
	);
		
	for(i = 0;i< varsities.length;i++)
	{
		if(i == 0)
		{
			//alert("action=INSTITUTEINFO&varsity="+varsities[i]);
			var varsity= varsities[i].replace(" ","_");
			$("#varsityname").html(varsities[i]);
			var res = getJasonData("action=INSTITUTEINFO&varsity="+varsity,"instituteInfo");								
		}
		$("#wheretostudy").append(
			$('<option></option>').val(varsities[i]).html(varsities[i])
		);
	}	
	
	$("#recommended").html( result[0].recommended_subject);		
}

function instituteInfo(res)
{
	
	//alert(res);
	//res = jQuery.parseJSON(res);
	$("#campusses").html(res[0].campuses);						
	$("#residence").html(res[0].residence);
	$("#reputation").html(res[0].reputation);
	$("#contact").html(res[0].contact);
	$("#bio").html(res[0].bio);	
}