var xmlhttp=getXMLHTTP();
var action='login';
var currentPage='';

function changeToCandidate(){
  document.getElementById("Interviewer_Login").style.display='none'; 
  document.getElementById("interviewerImage").src = "images/interviewer_login.jpg";
  document.getElementById("Candidate_Login").style.display='block';
  document.getElementById("candidateImage").src = "images/candidate_login.jpg";
}
function changeToInterviewer(){
  document.getElementById("Candidate_Login").style.display='none'; 
  document.getElementById("interviewerImage").src = "images/interviewer_login_new.jpg";
  document.getElementById("Interviewer_Login").style.display='block';
  document.getElementById("candidateImage").src = "images/candidate_login_new.jpg";
}

function candForm(path,currentpage){;
  MM_validateForm('emailCand','','RisEmail','passwordCand','','R'); 
  loginForm(path,document.MM_returnValue,"includes/login_control/candidate_login.php",'emailCand','passwordCand',currentpage); 
}

function intForm(path,currentpage){
  MM_validateForm('emailInt','','RisEmail','passwordInt','','R');
  loginForm(path,document.MM_returnValue,"includes/login_control/interviewer_login.php",'emailInt','passwordInt',currentpage);
}

function loginForm(path,originalMessage,formName,emailId,passwordId,currentpage){
  currentPage=currentpage;
  if(originalMessage!= ''){
    alert(originalMessage);
  }else{
    var params="email="+escape(encodeURI( document.getElementById(emailId).value ))+"&password="+escape(encodeURI( document.getElementById(passwordId).value ));
    queryPostDynamically(params,path+formName);
  }
}

function queryPostDynamically(params,urlPos){
  action='login';
  xmlhttp.open("POST",urlPos,true);
  xmlhttp.onreadystatechange = getHttpRes;
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", params.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(params);
}
         
function logoutDynamically(urlPos){
//  alert(urlPos);
  action='logout';
  var params="a="+escape(encodeURI( 'logout' ));
  xmlhttp.open("POST",urlPos,true);
  xmlhttp.onreadystatechange = getHttpRes;
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", params.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(params);
}


function getHttpRes(){
  //alert("getHttpRes called "+xmlhttp+" "+xmlhttp.readyState+" "+xmlhttp.status);
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      res = xmlhttp.responseText;  // These following lines get the response and update the page
	  //alert(res);
      if(action=='login'){
        if(res==0){
          document.getElementById('wrongDataMessage1').style.display='block';
          document.getElementById('wrongDataMessage2').style.display='block';
        }else{
		  
		   //alert(tt);http://www.bitsaajobs.com/interviewbay-test/candidate/verify.php?verify=bm8=&cand_id=MTA3
		   var tt = res.search('script language="javascript"');
		   //alert(tt);
		   if(tt==1) parent.window.location.href='http://www.bitsaajobs.com/interviewbay-test/candidate/verify.php?verify=bm8=&cand_id=MTA3';
		  else 
            parent.window.location.href=currentPage;
        }
      }else if(action=='logout'){
        parent.window.location.href=currentPage;
      }
  }else{
  }
}
  
function getXMLHTTP(){
  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");
  }
return xmlhttp;
}

