// Registeration form validation function //

function valid_reg(form) 
{

  var fname=document.registration.fname.value.substring(0,1);
  var lname=document.registration.fname.value.substring(0,1);
  var email=document.registration.emailid.value.substring(0,1);
  var address=document.registration.address.value.substring(0,1);
  var passwd=document.registration.pass.value;
   
  var mail=document.registration.emailid.value;
  var mon=document.registration.mon.value;
  var dat=document.registration.day.value;
  var yr=document.registration.year.value;
  
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
 

  
  if(document.registration.fname.value=="") {
    inlineMsg('fname','Enter First Name',2);
    return false;
  }
   if(fname==" ") {
    inlineMsg('fname','First letter should not be blank',2);
    return false;
  }
  if(document.registration.lname.value=="") {
    inlineMsg('lname','Enter Last Name',2);
    return false;
  }
   if(lname==" ") {
    inlineMsg('lname','First letter should not be blank',2);
    return false;
  }
   if(document.registration.emailid.value=="") {
    inlineMsg('emailid','Enter Email id',2);
    return false;
  }
   if(email==" ") {
    inlineMsg('emailid','First letter should not be blank',2);
    return false;
  }
  if(!mail.match(emailRegex)) {
    inlineMsg('emailid','You have entered an <br>invalid email.',2);
    return false;
  }
  
  
  if(document.registration.pass.value=="") {
    inlineMsg('pass','Enter Password',2);
    return false;
  }
   if(lname==" ") {
    inlineMsg('pass','First letter should not be blank',2);
    return false;
  }
  if(document.registration.address.value=="") {
    inlineMsg('address','Enter Address',2);
    return false;
  }
   if(address==" ") {
    inlineMsg('address','First letter should not be blank',2);
    return false;
  }
  
  
    if(document.registration.day.value=="0") {
    inlineMsg('day','Select Date',2);
    return false;
  }
 
   if(document.registration.mon.value=="-1") {
    inlineMsg('mon','Select Month',2);
    return false;
  }
  
   if(document.registration.year.value=="") {
    inlineMsg('year','Select Year',2);
    return false;
  }
  if(document.registration.terms.checked==false)
  {
	inlineMsg('terms','You should accept Terms and Conditions',2);
    return false;
  }
  var user=document.registration.emailid.value;
  $.post('checkuser.php',{usermail:user} ,function(getData){
	
	  if(getData==1)
	  {
		  	inlineMsg('emailid','Already Exists! Please try some other Id!',2);
			return false;
	  }
	  else
	  { 
	         
            document.registration.action="index.php";
            document.registration.submit();
	  }
  }); 
													
 
  return true;
}



// START OF MESSAGE SCRIPT //
var MSGTIMER = 1;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;

  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = (leftPosition(targetdiv)-35) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
	
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}


/*****************************************/
function check_login(curURL)
{
	  var email=document.form1.logemail.value.substring(0,1);
	  var mail=document.form1.logemail.value;
	   var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	   
if(document.form1.logemail.value=="") {
    inlineMsg('logemail','Enter Email id',2);
    return false;
  }
   if(email==" ") {
    inlineMsg('logemail','First letter should not be blank',2);
    return false;
  }
  if(!mail.match(emailRegex)) {
    inlineMsg('logemail','You have entered an <br>invalid email.',2);
    return false;
  }
  if(document.form1.logpass.value=="") {
    inlineMsg('logpass','Enter Password',2);
    return false;
  }

  
    var pwd=document.form1.logpass.value;
  $.post('checklogin.php',{usermail:mail,passwd:pwd} ,function(getData){
	
	  if(getData==0)
	  {
		  	inlineMsg('logpass','Invalid Login!',2);
			return false;
	  }
	  else if(getData=='ja')
	  {
		  inlineMsg('logpass','Not yet Activated... Check your mail to Activate!',2);
			return false;
	  }
	  else
	  { 	         
	         document.form1.action=curURL+"?log=1";
             document.form1.submit();
           
	  }
  }); 													
 
  return true;
	  
}
function check_login_home(curURL)
{
	
	  var email=document.lgn_frm.hlogemail.value.substring(0,1);
	  var mail=document.lgn_frm.hlogemail.value;
	   var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	   
if(document.lgn_frm.hlogemail.value=="") {
    inlineMsg('hlogemail','Enter Email id',2);
    return false;
  }
   if(email==" ") {
    inlineMsg('hlogemail','First letter should not be blank',2);
    return false;
  }
  if(!mail.match(emailRegex)) {
    inlineMsg('hlogemail','You have entered an <br>invalid email.',2);
    return false;
  }
  if(document.lgn_frm.hlogpass.value=="") {
    inlineMsg('hlogpass','Enter Password',2);
    return false;
  }
  if(document.lgn_frm.security_code_log.value=="") {
    inlineMsg('security_code_log','Enter Security code',2);
    return false;
  }

  
    var pwd=document.lgn_frm.hlogpass.value;
  $.post('checklogin.php',{usermail:mail,passwd:pwd} ,function(getData){
	
	  if(getData==0)
	  {
		  	inlineMsg('hlogpass','Invalid Login!',2);
			return false;
	  }
	  else
	  { 	         
	         document.lgn_frm.action=curURL;
             document.lgn_frm.submit();
           
	  }
  }); 													
 
  return true;
	  
}
function userLogout()
{
	 window.location.href="logout.php";
	// window.location.href=cururl;
	
}
function facebook_logout(logoutURL)
{
	 FB.logout(function(response) {
	  document.location.reload();
	});

}

function getSubcat(catid)
{	
$.ajax({
   type: "POST",
   url: "addfaviroute.php",
   data: {
	catid:catid
   },
   success: function(msg){
	 $('#faviroute').html(msg);
   }
});
}



