//calling jquery script
document.write("<script type='text/javascript' src='../../scripts/jquery/jquery/jquery.js'></script>");
//Ajax script to select country/state/district values 
var xmlHttp;
function fn_ajaxstate(blog,val)
{
  
   xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
   var url="ajaxscript.php?blog="+blog+"&val="+val;
  	xmlHttp.onreadystatechange=function() { stateChangedcs(blog)};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChangedcs(blog) 
{
	if(xmlHttp.readyState==4) 
	{
		if(blog=="country")
		{
			document.getElementById("select_state").innerHTML=xmlHttp.responseText;
		}
		if(blog=="state")
		{
			document.getElementById("select_city").innerHTML=xmlHttp.responseText;
		}
	}

}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
//ajax code ends for country/state selection 

// To validate character,number and special characters
function fn_chkchar(e) {
var keycode;
if(window.event) {
keycode=window.event.keyCode;
}
else if(e) {
keycode=e.which;
}

if((keycode==0)||(keycode==8)||(keycode==32)|| (keycode==46)||(keycode>=65 && keycode<=90) || (keycode>=97 && keycode<=122)||(keycode==44) || (keycode>=48 && keycode<=57) || (keycode==45) || (keycode==95)){}
else { return false;}
}
//validation code ends

// To validate number only
function fn_num(e) {
var keycode;
if(window.event) {
keycode=window.event.keyCode;
}
else if(e) {
keycode=e.which;
}

if((keycode==0)||(keycode==8)||(keycode==32) || (keycode>=48 && keycode<=57)){}
else { return false;}
}
//validation code ends

//For validate community form
function fn_valid()
{
if(document.community.c_name.value == "")
{
  alert("Please enter community name");
  document.community.c_name.focus();
  return false;
   }  
  
if(document.community.c_name.value !="")
  {
   var cname=document.community.c_name.value;
   
   if(cname.substring(0,1)==" ")
    {
       alert("First character Should not be space in community name");
       document.community.c_name.value="";
       document.community.c_name.focus();
       return false;
      }
}
 if(document.community.channel.value == 0)
     { 
       alert("Please select the channel");
       document.community.channel.focus();
         return false;
     }
  if(document.community.country.value == 0)
     { 
     alert("Please select the country name");
       document.community.country.focus();
         return false;
     }
 if(document.community.state.value == 0)
     {
       alert("Please select the state name");
       document.community.state.focus();
         return false;
     }

  if(document.community.district.value == 0)
     { 
       alert("Please select the district name");
       document.community.district.focus();
         return false;
     }
 if(document.community.postalcode.value == 0)
     { 
      alert("Please enter the postal code");
       document.community.postalcode.focus();
         return false;
     }
if(document.community.postalcode.value.length < 6)
     { 
      alert("Postal code should be minimum of 6 numbers");
	document.community.postalcode.value='';
       document.community.postalcode.focus();
         return false;
     }
/*if(document.community.image1.value == "")
     { 
       alert("Please upload community image");
       document.community.image1.focus();
         return false;
     }
*/
if(document.community.image1.value != "")
    	{
		var image_file = document.community.image1.value;  
		var image_length = document.community.image1.value.length;  
		var pos = image_file.lastIndexOf('.') + 1;  
		var ext = image_file.substring(pos, image_length);  
		var final_ext = ext.toLowerCase();  
		if((final_ext == "jpg")  || (final_ext == "jpeg") || (final_ext == "gif") || 
(final_ext == "png") || (final_ext == "bmp")) 
		{   }
		else {
		alert("You must upload an image file with one of the following extensions: jpg, jpeg, gif, png, bmp");
		document.community.image1.value = "";
 		return false;  }
 	}
if(document.community.comm.value == "")
     { 
       alert("Please enter the community details");
       document.community.comm.focus();
         return false;
     }
 if($("input[@id=subname]:checked").length == 0)
   { 
     alert("Please select atleast one category");
     return false;
   }
if(document.community.categ.value !="")
  {
   var subcat=document.community.categ.value;
   
   if(subcat.substring(0,1)==" ")
    {
       alert("First character Should not be space in subcategory");
       document.community.categ.value="";
       document.community.categ.focus();
       return false;
      }
}
if(document.community.security_code.value == "")
     { 
       alert("Please enter the security code");
       document.community.security_code.focus();
         return false;
     }
}
// Community form validation Ends

// community availability check function
var xmlHttpc;
function fn_avail()
{
var c_name=document.getElementById("c_name").value;
if(c_name==''){
}else{
xmlHttpc=GetXmlHttpObject();
if (xmlHttpc==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var avail="avail";
var url="community_avail.php?c_name="+c_name+"&type="+avail;
xmlHttpc.onreadystatechange=stateChanged;
xmlHttpc.open("GET",url,true);
xmlHttpc.send(null);
} 
}

function stateChanged() 
{ 
if (xmlHttpc.readyState==4)
{ 
document.getElementById("avail").innerHTML=xmlHttpc.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttpc=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttpc=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttpc=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttpc=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttpc;
}
//community availability check ajax code ends


// validation code for Textarea fields
 function fn_desc(e){
var keycode;
if(window.event){
keycode=window.event.keyCode;
}
else if(e){
keycode=e.which;
}
if((keycode==0) || (keycode==8) || (keycode==32) || (keycode==46) || (keycode==39) || (keycode==40) || (keycode==41) || (keycode==44) || (keycode>=48 && keycode<=57) || (keycode>=65 && keycode<=90) || (keycode>=97 && keycode<=122) || (keycode==127) || (keycode==45) || (keycode==47)){
return true;
}
else{
alert("Special characters are not allowed");
return false;
}
}
// validation code for Textarea fields ends 


// For counting textarea characters length
function check_length(community)
{
maxLen = 250; 
 if (community.comm.value.length > maxLen) {
 community.countdown.value = 0;
 community.comm.value = community.comm.value.substring(0, maxLen);
 var msg = "You have reached your maximum limit of characters allowed";
 alert(msg);
 return false;
 }
 else { 
 community.countdown.value = maxLen - community.comm.value.length; 
 }
}
// For counting textarea characters length code ends 


// ajax code for subcategory selection
var xmlHttps;
function fn_category()
{
var cat_name=document.getElementById("channel").value;
if(cat_name==''){
}else{
xmlHttps=GetXmlHttpObject();
if (xmlHttps==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var category="category";
var url="community_avail.php?cat_name="+cat_name+"&type="+category;
xmlHttps.onreadystatechange=stateChangedct;
xmlHttps.open("GET",url,true);
xmlHttps.send(null);
} 
}

function stateChangedct() 
{ 
if (xmlHttps.readyState==4)
{ 
document.getElementById("subcat").innerHTML=xmlHttps.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttps=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttps=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttps=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttps=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttps;
}
// ajax code for subcategory selection ends 

/// Lightbox Open Popup Window //////////function to load add  page
function favorite_forms(e,popid,tot) {
 var rand = Math.random();   
 $.get('quick_login.php',{popid:popid,rand:rand,tot:tot},
 function (resp)
 {
    if(tot >= 1)    
    document.getElementById('community').style.display = 'none';
    var windowWidth  = document.documentElement.clientWidth - 200;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight  = $("#add_sub_form").height()+450;
    var popupWidth   = $("#add_sub_form").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_form').css({'left' : '37%','top' : 220});////set the css for popup using jquery
    $("#add_sub_form").css({"opacity": "1.0"});

    //only need force for IE6

    //centering
//$("#add_sub_forms").css({"position": "absolute","center": windowHeight/2-popupHeight,"left": windowWidth/2-popupWidth
//});
    $("#backgroundPopup").css({"opacity": "0.7","height": windowHeight});  
    $("#backgroundPopup").fadeIn(30);
    $("#add_sub_form").fadeIn(30);
    document.getElementById('add_sub_form').innerHTML = resp;
    document.getElementById('add_sub_form').style.visibility = "visible";
 });
}
// End Lightbox Popup window
// Close Popup Window //////////function to close add popup /// add_sub_forms here
function close_subforms(addeddiv,tot) {
    if(tot >= 1)    
    document.getElementById('community').style.display = 'block';
    document.getElementById(addeddiv).innerHTML = "";
	$("#backgroundPopup").fadeOut(30);
    document.getElementById(addeddiv).style.visibility = "hidden";
}

// Login Page Validation
function log_validate()
{	
    var rand = Math.random();
	if(document.getElementById('uname').value == ""){
		alert("Please Enter User Name");
		document.getElementById('uname').focus();
		return false;
    }
    if(document.getElementById('password').value == ""){
		    alert("Please Enter Password");
		    document.getElementById('password').focus();
		    return false;
    }
    var uname = document.getElementById('uname').value;
    var pwd = document.getElementById('password').value;
    var id = document.getElementById('comid').value;
    $.get("quick_insert.php",{rand:rand,uname:uname,pwd:pwd},            
        function (resp)
        {
            var rand = Math.random();
            if(resp == 1){
                    $("#backgroundPopup").fadeOut(30);
                    document.getElementById('add_sub_form').style.visibility = "hidden";
                    self.location = "http://www.opinionpoll.in/modules/community/view_community_private.php?id="+id;     
            }
            
            else if(resp == 2){
                    alert('Your Username and Password is Invalid');
                    document.getElementById('uname').value = '';
                    document.getElementById('password').value = '';
            }
            
            else if(resp == 3){
                    alert('Your Username and Password is blocked ! Please Contact Site Administrator');
		            document.getElementById('uname').value = '';
                    document.getElementById('password').value = '';
            }
            
            else {
                if(resp == 4){
                    alert('You are not yet activated your account');
                    document.getElementById('uname').value = '';
                    document.getElementById('password').value = '';
                }
            }
        }
    );
}
//for user registration
// LIGHTBOX POP UP WINDOW OPEN
function login_forms(e,popid,tot){
    var rand = Math.random();   
    $.get('quickuser_register.php',{popid:popid,rand:rand,tot:tot},
    function (resp)
    {
        if(tot >= 1)    
        document.getElementById('community').style.display = 'none';
        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' : '31.5%','top' : 200, 'width': 425, 'height' : 147, 
        'border-width':5});////set the css for popup using jquery
        $("#add_sub_forms").css({"opacity": "0.92"});
    
        //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";
		
			
		f = document.getElementsByName("embed");
		for(var i=0; i<f.length; i++) {
		f[i].style.visibility='none';
		}
    });
}
// END LIGHTBOX POP UP WINDOW OPEN

// Close Lightbox Popup Window              //function to close add popup 			//add_sub_forms here
function close_subform(addeddiv,tot) {
    if(tot >= 1)    
    document.getElementById('community').style.display = 'block';
    document.getElementById(addeddiv).innerHTML = "";
	$("#backgroundPopup").fadeOut(100);
    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' : '31.5%','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' : '31.5%','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('username').value;
    var pwd = document.getElementById('password').value;
    if(document.getElementById('username').value == '')
    {
        alert("Please Enter Email Id");
        document.getElementById('username').focus();
        return false; 
    }
    
    if(document.getElementById('username').value != '')
	{
		var emailo = document.getElementById('username').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('username').value == '';
            document.getElementById('avail').innerHTML='';
            document.getElementById('errormsg').innerHTML='';
			return false;
			}
		}
	}
   
    if(document.getElementById('password').value == '')
    {
        alert("Please Enter Password");
        document.getElementById('password').focus();
        return false; 
    }
    

    if(pwd.length < 8)
    {
        alert("Enter minimum 8 characters for Password");
        document.getElementById('password').focus();
        return false; 
    }
    
    if(document.getElementById('terms').checked == false)
    {
        alert("Please accept 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>Email Id already exists in our database. Please Register Again</b></font>";
            document.getElementById("errormsg").innerHTML = msg;
            document.getElementById("password").value = '';
            document.getElementById("avail").style.display = 'none';
         }
         
         else {
            $("#backgroundPopup").fadeOut(30);
            document.getElementById('add_sub_forms').style.visibility = "hidden";
            //self.location = "thanks.php?uid="+resp;
            alert("Registration Successful. Please check your email for Confirmation");     
         }
    }
    );
}
// 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('username').value = '';
document.getElementById('username').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('username').value = '';
document.getElementById('username').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('username').value = '';
document.getElementById('username').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('username').value = '';
document.getElementById('username').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('username').value = '';
document.getElementById('username').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('username').value = '';
document.getElementById('username').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('username').value = '';
document.getElementById('username').focus();
return false;
}


if (len<2) {
alert("Please enter Email ID in correct format");
document.getElementById('username').value = '';
document.getElementById('username').focus();
return false;
}

return true;
}

//script for community availability checking
function user_avail() {
     var rand = Math.random();
     var uname = document.getElementById('username').value;
     var type = 2;  
     if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('username').value)) {
         if(uname != '') {
            $.get("http://www.opinionpoll.in/modules/user/check_avail.php",{rand:rand,uname:uname,type:type},
            function (resp)
            {
              // document.getElementById("errormsg").style.display = 'none';
              document.getElementById("avail").innerHTML = resp;
             }
            );
        }   
    }
    else {
			alert("Invalid E-mail Address! Please re-enter.");
			document.getElementById('avail').innerHTML='';
			document.getElementById('errormsg').innerHTML='';
			document.getElementById('username').focus();
			return false;
    }
}
//script for community availability checking ends

