function initAjaxXMLHttp()
{
    if (window.XMLHttpRequest)
    {
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        try
        {
            return new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e1)
        {
        	try
            {
                 return new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e2) {return new ActiveXObject("Microsoft.XMLHTTP");}
        }
    }
}



function processRequest() {

  if (xmlHttpReq.readyState == 4) {
      if (xmlHttpReq.status == 200)
      {
            xmlResp = xmlHttpReq.responseText;		            	             
      }
      else
      {
	        alert("Problem in Server Response");
      }
	}		
}

//get captcha for register page
function getRegisterCaptcha(frmid)
{
	var b=false;		
	var captchaval = frmid.captcha.value; 
		  xmlHttpReq = initAjaxXMLHttp();
	      //var url = "<%=request.getContextPath()%>";      
	      var url = "/Movicha/Register?kaptcha=" + captchaval+"&operation=captchaverify";
	      var syncFlag = false;
	      xmlHttpReq.open("POST", url, syncFlag);
	      xmlHttpReq.send(null);
	      processRequest();
	      if(xmlResp.indexOf("Invalidcode") != -1)
	      {
	    	  b=false;
	      }
	      else if(xmlResp.indexOf("Correctcode") != -1)
		  {
			  b=true;
		  }	
	  return b;
}

//get captcha for fogotpwd page
function getForgotPwdCaptcha(frmid)
{
	var b=false;		
	var captchaval = frmid.captcha.value; 
		  xmlHttpReq = initAjaxXMLHttp();
	      //var url = "<%=request.getContextPath()%>";      
	      var url = "/Movicha/Myaccount?kaptcha=" + captchaval+"&operation=forgotpwdcaptcha";
	      var syncFlag = false;
	      xmlHttpReq.open("POST", url, syncFlag);
	      xmlHttpReq.send(null);
	      processRequest();
	      if(xmlResp.indexOf("Invalidcode") != -1)
	      {
	    	  b=false;
	      }
	      else if(xmlResp.indexOf("Correctcode") != -1)
		  {
			  b=true;
		  }	
	  return b;
}

//for register new user
function register(frmid)
{
	//alert("Inside JD reg");
	var winormac = "0";
	if(frmid.group2 != null)
	{
		if(frmid.group2[0].checked)
			winormac = "0";
		else
			winormac = "1";
	}
	  var login = frmid.username.value;
	  var pwd = frmid.pwd.value;	  
	  var lname = frmid.lname.value;
	  var fname = frmid.fname.value;	  
	  var dname = fname+" "+lname;	  
	  var email = frmid.email.value;
	  var devicename = frmid.devicename.value;
	 xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";
    var url = "/Movicha/Register?operation=register&login="+login+"&pwd="+pwd+"&fname="+fname+"&lname="+lname
    	  +"&dname="+dname+"&email="+email+"&device="+devicename+"&winormac="+winormac;
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    	    
    processRequest();
	return xmlResp;
}

//for add meid page
function meidonly(frmid)
{
	  var login = frmid.musername.value;
	  var pwd = frmid.mpwd.value;
	  var customid = frmid.guid.value;
	  var winormac;
	  if(frmid.group2[0].checked)		  
		  winormac = '0';	  	
	  else if(frmid.group2[1].checked)
		  winormac = '1';
	  
    xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";      
    var url = "/Movicha/Register?operation=meidonly&login="+login+"&pwd="+pwd
    	  	+"&customid="+customid+"&winormac="+winormac;
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    
    processRequest();			    			
   	return xmlResp;	
}

//check loginid availability in register page
function getLoginAvail(frmid)
{  
	var loginval = frmid.username.value;
	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";		         
    var url = "/Movicha/Register?login="+loginval+"&operation=checkusername";
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    
    processRequest();    
	  return xmlResp;
 }


// for ForgotPassword 
function sendNewPwd(frmid)
{		
	  var userid = frmid.userid.value;
	  var email = frmid.email.value;	  

	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";	    
    url = "/Movicha/Myaccount?operation=forgotpassword&userid="+userid+"&email="+email;
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    	    
    processRequest();    
	return xmlResp;
}

//for change password
function changePwd(frmid)
{			
	  var userid = frmid.userid.value;
	  var oldpwd = frmid.pass1.value;
	  var newpwd = frmid.pass2.value;  

	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";	    
    var url = "/Movicha/Myaccount?operation=changepassword&userid="+userid+"&oldpwd="+oldpwd+"&newpwd="+newpwd;
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    	    
    processRequest();    
	return xmlResp;
}

//for retrieve back the email link url 
function retrieveTID(frmid)
{			
	  var email = frmid.email.value;
	  
	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";	    
    var url = "/Movicha/Myaccount?operation=retrieveTID&email="+email;
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    	    
    processRequest();    
	return xmlResp;
}

//To retrieve the list of institute from db to show in usercreate page 
function getInstList()
{
	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";	    
    var url = "/Movicha/Register?operation=getInstitutelist";    
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    	    
    processRequest();    
	return xmlResp;
}

function escapeVal(textareaval,replaceWith){
	
	textareaval = escape(textareaval);
	var i=0;
	for(i=0; i<textareaval.length; i++){

		if(textareaval.indexOf("%0D%0A") > -1){	
		textareaval = textareaval.replace("%0D%0A",replaceWith)
		}
		else if(textareaval.indexOf("%0A") > -1){
		//Unix encodes returns as \n hex
			textareaval=textareaval.replace("%0A",replaceWith)
		}
		else if(textareaval.indexOf("%0D") > -1){
		//Macintosh encodes returns as \r hex
			textareaval = textareaval.replace("%0D",replaceWith)
		}

	}
	return textareaval;
	//textarea.value=unescape(textarea.value) unescape all other encoded characters
	}

function addInvited(frmid)
{			
	  var userid = frmid.refuserid.value;
	  var myemail = frmid.myEmail.value;
	  var femails = frmid.femail.value;
	  var emailcontent = frmid.emailcontent.value;
	  emailcontent = escapeVal(emailcontent,"<br>");	  
	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";	    
    var url = "/Movicha/Myaccount?operation=addInvited&userid="+userid+"&myemail="+myemail+"&femails="+femails+"&emailcontent="+emailcontent;
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    	    
    processRequest();    
	return xmlResp;
}


//check loginid availability in invite page
function checkLoginExists(frmid)
{
	var loginval = frmid.refuserid.value;
	xmlHttpReq = initAjaxXMLHttp();
    //var url = "<%=request.getContextPath()%>";		         
    var url = "/Movicha/Myaccount?operation=checkLoginExist&login="+loginval;    
    var syncFlag = false;
    xmlHttpReq.open("POST", url, syncFlag);
    xmlHttpReq.send(null);    
    processRequest();    
	return xmlResp;
 }

	//get redumption scheme from webservice n load into the page
	function getredemptionScheme()
	{	
		xmlHttpReq = initAjaxXMLHttp();
	    //var url = "<%=request.getContextPath()%>";		
	    var url = "/Movicha/Myaccount?operation=getredemptioninfo";	    
	    var syncFlag = false;
	    xmlHttpReq.open("POST", url, syncFlag);
	    xmlHttpReq.send(null);    
	    processRequest();    
		return xmlResp;
	}
	//submit the redemption form to send mail & delete customer points
	function redemptionFormSubmit(myfrm)
	{
		var fname = myfrm.ufname.value;
		var lname = myfrm.ulname.value;
		var email = myfrm.uemail.value;
		var phno = myfrm.uphno.value;
		var ptlen = myfrm.group1.length;
		var userpts;
		var i=0;
		for(i=0;i<ptlen; i++)
		{
			if(myfrm.group1[i].checked)
			{
				userpts = myfrm.group1[i].value;
			}
		}
		xmlHttpReq = initAjaxXMLHttp();		
	    //var url = "<%=request.getContextPath()%>";		         
	    var url = "/Movicha/Myaccount?operation=redemptionsubmit&fname="+fname+"&lname="+lname+"&email="+email+"&phno="+phno+"&points="+userpts;	    
	    var syncFlag = false;
	    xmlHttpReq.open("POST", url, syncFlag);
	    xmlHttpReq.send(null);    
	    processRequest();    
		return xmlResp;
	}
	
	//Login form validation
	function LoginFormValidation(logfrm)
	{
		var logvalid = true;
		if(logfrm.user.value == null || logfrm.user.value == "")
		{
			logvalid = false;	
		}	
		if(logfrm.pass.value == null || logfrm.pass.value == "")
		{
			logvalid = false;
		}
		return logvalid;
	}
	
	function validateinviteForm(frm)
	{	
		var emailad = frm.myEmail.value;		 
		var a = true;							
		var loginval = frm.refuserid.value;
		var loginreg = /^([A-Za-z0-9_\.]{5,32})+$/;
		var emailreg = /.+@.+\.([A-Za-z])+$/;
		if(loginval == '' || emailad == '' || frm.femail.value == '' || frm.emailcontent.value == '')
		{	
			document.getElementById("inviterespDiv").style.visibility="visible";								
			document.getElementById("inviterespDiv").innerHTML = '<font color=#FF0000>All the fields are mandatory</font>';
			a = false;
		}		
		else if(loginreg.test(loginval) == false) 
		{
			document.getElementById("iuseridDiv").style.visibility = "visible";
			document.getElementById("iuseridError").innerHTML = "<font color=#27408B>User Id can contain only letters (a-z), numbers (0-9),  uderscore(_) and period (.) and length should be 5-32</font>";
			document.getElementById("inviterespDiv").style.visibility="visible";								
			document.getElementById("inviterespDiv").innerHTML = "<font color=#FF0000>User Id can contain only letters, numbers, uderscore, period and length should be 5-32</font>";
			document.getElementById("ifemaildiv").style.visibility="hidden";
			document.getElementById("iemailcontdiv").style.visibility="hidden";
			document.getElementById("imyEmaildiv").style.visibility="hidden";
			a=false;
		}
		else if(emailreg.test(emailad) == false)
		{
				document.getElementById("imyEmaildiv").style.visibility = "visible";
				document.getElementById("imyEmailError").innerHTML = '<font color=#27408B>Please enter Valid Email Id</font>';
				document.getElementById("inviterespDiv").style.visibility="visible";								
				document.getElementById("inviterespDiv").innerHTML = "<font color=#FF0000>Please enter Valid Email Id</font>";
				document.getElementById("ifemaildiv").style.visibility="hidden";
				document.getElementById("iemailcontdiv").style.visibility="hidden";
				document.getElementById("iuseridDiv").style.visibility="hidden";
				a=false;
		}	
		else 
		{
			var resp1 = checkLoginExists(frm);
			if(resp1.indexOf("Invalid") != -1)
			{
				document.getElementById("iuseridDiv").style.visibility="visible";
		    	document.getElementById("iuseridError").innerHTML = '<font color=#27408B>InValid UserId</font>';
		    	document.getElementById("inviterespDiv").style.visibility="visible";								
				document.getElementById("inviterespDiv").innerHTML = "<font color=#FF0000>Invalid UserId</font>";
				document.getElementById("ifemaildiv").style.visibility="hidden";
				document.getElementById("iemailcontdiv").style.visibility="hidden";
				document.getElementById("imyEmaildiv").style.visibility="hidden";				
		    	a=false;
			}			
	
		}
			if(a == true)
			{
				var resp = addInvited(frm);				
				if(resp != null)
		    	{				    	
			    	if(resp.indexOf("success") != -1)
			    	{
				    	var id="Thank you for inviting your friends to iMovicha.";
			    		document.getElementById("inviteForm").style.visibility = "hidden";
			    		document.getElementById("inviteDiv").innerHTML = "<br/><br/><br/><center><b><font size='20'>"+id+"</font><b></center>";
			    		document.getElementById("inviteDiv").style.visibility = "visible";
			    	}
			    	else if(resp.indexOf("does not exist") != -1)
			    	{
			    		document.getElementById("ierrorDiv").style.visibility = "visible";
			    		document.getElementById("ierrorDiv").innerHTML = "<br/><br/><br/><center><b><font size='20' color=#FF0000>"+resp+"</font></b></center>";
			    	}			    	
			    	else
			    	{			    		
			    		document.getElementById("inviteDiv").innerHTML = "<br/><br/><br/><center><b><font size='20'>"+resp+"</font></b></center>";
			    		document.getElementById("inviteDiv").style.visibility = "visible";			    			
			    	}				    	
		    	}	
		    	else
			    {
			    	window.location.href = "/Movicha/Errorpage.jsp";
			    }
			}
		//return a;		
	}	
	
	//submit the change meid/platform form
	function changeMeidSubmit(platformname,oldmeid,newitem,newmeid)
	{		
		xmlHttpReq = initAjaxXMLHttp();		
	    //var url = "<%=request.getContextPath()%>";		         
	    var url = "/Movicha/Myaccount?operation=changeplatform&olditem="+platformname+"&oldmeid="+oldmeid+"&newitem="+newitem+"&newmeid="+newmeid;	    
	    var syncFlag = false;
	    xmlHttpReq.open("POST", url, syncFlag);
	    xmlHttpReq.send(null);    
	    processRequest();    
		return xmlResp;
	}
