
// LIGHTBOX POP UP WINDOW OPEN
function login_forms(e,popid){
    var rand = Math.random();   
    $.get('quickuser_register.php',{popid:popid,rand:rand},
    function (resp)
    {
        var windowWidth  = document.documentElement.clientWidth - 200;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight  = $("#add_sub_forms").height()+450;
        var popupWidth   = $("#add_sub_forms").width()+320;

        var mousex = e.clientX;////to get mouse position x axis
        var mousey = e.clientY;////to get mouse position y axis

        //mousex = mousex - 150 ;////to get the popup window in desired x position
        //mousey = mousey - 200;////to get the popup window in desired y position*/
        $('#add_sub_forms').css({'left' : 350,'top' : 150, 'width': 425, 'height' : 147, 
        'border-width':5});////set the css for popup using jquery
        $("#add_sub_forms").css({"opacity": "0.8"});
    
        //only need force for IE6
        //centering
        $("#backgroundPopup").css({"opacity": "0.7","height": windowHeight});  
        $("#backgroundPopup").fadeIn(30);
        $("#add_sub_forms").fadeIn(30);
        document.getElementById('add_sub_forms').innerHTML = resp;
        document.getElementById('add_sub_forms').style.visibility = "visible";
     });
}
// END LIGHTBOX POP UP WINDOW OPEN

// Close Lightbox Popup Window              ////////// function to close add popup /// add_sub_forms here
function close_subforms(addeddiv) {
    document.getElementById(addeddiv).innerHTML = "";
	$("#backgroundPopup").fadeOut(30);
    document.getElementById(addeddiv).style.visibility = "hidden";
}
// End Close Lightbox Popup Window

// Hide & Show Terms & Condition
function hideterms(id){
       var e = document.getElementById(id);
       if(e.style.display == 'block') 
       {
          e.style.display = 'none'; 
          $('#add_sub_forms').css({'left' : 350,'top' : 150, 'width': 425, 'height' : 147, 
        'border-width':5});////set the css for popup using jquery
       }
       else 
       {
          e.style.display = 'block';
          $('#add_sub_forms').css({'left' : 350,'top' : 150, 'width': 425, 'height' : 215, 
        'border-width':5}); 
       }
    //document.getElementById('conditions').style.display = 'block';
}
// End Hide & Show Terms & Condition

// Validation for Lightbox
function login_validate(){
    var rand = Math.random();
    var uname = document.getElementById('username1').value;
    var pwd = document.getElementById('password1').value;
    if(document.getElementById('username1').value == '')
    {
        alert("Please Enter Email Id");
        document.getElementById('username1').focus();
        return false; 
    }
    
    if(document.getElementById('username1').value != '')
	{
		var emailo = document.getElementById('username1').value;
		var emailsplit = emailo.split(",");
		for(p=0;p<emailsplit.length;p++)
		{
			if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailsplit[p])) { 
			}
			else {
			alert('Invalid E-mail Address ! Please re-enter.');
            document.getElementById('username1').value == '';
			return false;
			}
		}
	}
   
    if(document.getElementById('password1').value == '')
    {
        alert("Please Enter password");
        document.getElementById('password1').focus();
        return false; 
    }
    

    if(pwd.length < 8)
    {
        alert("Enter minimum 8 characters for password");
        document.getElementById('password1').focus();
        return false; 
    }
    
    if(document.getElementById('terms').checked == false)
    {
        alert("Please Check Terms & Condition");
        return false; 
    }
     emailCheck(uname);
    
    $.get("quickuser_insert.php",{rand:rand,uname:uname,password:pwd},
    function (resp)
    {
         if(resp == 0) {
            // var msg = "<font color = 'red'>Your OpinionPoll registration failed due to following reason. <br>Email Id already exists in our database</font>";
            var msg = "<font color = 'red'><b>Invalid/Email ID already exists in our database. Please Register Again</b></font>";
            document.getElementById("errormsg").innerHTML = msg;
            document.getElementById("password1").value = '';
            document.getElementById("quickavail").style.display = 'none';
         }
         
         else {
            //$("#backgroundPopup").fadeOut(30);
            //document.getElementById('add_sub_forms').style.visibility = "hidden";
            self.location = "/modules/user/thanks.php?uid="+resp;     
         }
    }
    );
}
// End Validation for Lightbox

function emailCheck (emailStr) {
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray == null) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
  }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
   }
}

// See if "user" is valid 
if (user.match(userPat)==null) {
// user is not valid
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
   }
}
return true;
}
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
   }
}

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
}


if (len<2) {
alert("Please enter Email ID in correct format");
document.getElementById('username1').value = '';
document.getElementById('username1').focus();
return false;
}

return true;
}


function user_avail(){
     var rand = Math.random();
     var uname = document.getElementById('username1').value;
     var type = 2;  
     
     if(uname != '') {
        $.get("check_avail.php",{rand:rand,uname:uname,type:type},
        function (resp)
        {
          // document.getElementById("errormsg").style.display = 'none';
          document.getElementById("quickavail").innerHTML = resp;
         }
        );
    }   
}

