// JavaScript Document
function checkValidEmail(value)
{if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value)))
return false;else
return true;}
function $objid(element)
{if(typeof element=='string')
element=document.getElementById(element);return element;}
function checkpoll()
{
	var chk = false;	
	for(var i=1;i<=4;i++)
	{
			if($objid("optval"+i).checked==true)
			{
				var optval = $objid("optval"+i).value;
				chk= true;
			}
			}
			
			if(chk==false)
			{
				alert("Please select a option")
				return false;
			}
			else
			{
				var qid = $objid("quest_id").value;
				ajaxLink("operation.php?action=submit_poll&qid="+qid+"&optid="+optval,"before_poll");
			}
}
function validate_registration()
{
	frm = document.registration;
	for(var i=0;i<frm.elements.length;i++)
	{
		if(frm.elements[i].type=="text" || frm.elements[i].type=="textarea")
		{
			if(frm.elements[i].value!="")
			{
				if(!check_blocked_words(frm.elements[i].value,frm.elements[i].name))
					return false;
			}
		}
	}
	
	if($objid('uname').value=="")
	{
		alert("Please Enter the username");	
		$objid('uname').focus();
		return false;
	}
	if($objid("found").value=="1")
	{
		alert("Username value already exists. Please choose another");
		$objid("uname").focus(); 
		return false;
	}
	if($objid("uname").value.indexOf(" ") && !(/^([a-zA-Z0-9_-])+$/.test($objid("uname").value)))
	{
		alert("Username can only contain alphanumerics, hyphens or underscores, Blank spaces not allowed.");
		$objid("uname").focus();
		return false;
	}
	if($objid('fname').value=="")
	{
		alert("Please Enter the Firstname");	
		$objid('fname').focus();
		return false;
	}
	if($objid('country').value=="")
	{
		alert("Please select the country");	
		$objid('country').focus();
		return false;
	}
	if($objid('email').value=="")
	{
		alert("Please Enter the email");	
		$objid('email').focus();
		return false;
	}
	if(!checkValidEmail($objid("email").value))
	{
	    alert("Please Enter valid email address.");
		$objid("email").focus(); 
		return false;
	}
	if($objid("found_email").value=="1")
	{
		alert("Email already exists. Please choose another");
		$objid("email").focus(); 
		return false;
	}
	
	if($objid("remail").value=='')
	{	
	    alert("Please retype your email address.");
		$objid("remail").focus();
		return false;		
	}
	if($objid("email").value!=$objid("remail").value)
	{
		alert("Email do not match with confirm email.");
		$objid("remail").focus();
		return false;
	}
	if($objid("pass").value=='')
	{	
	    alert("Please Enter password.");
		$objid("pass").focus();
		return false;		
	}	
	if($objid("pass").value.indexOf(" ")>=0)
	{
		alert("Blank spaces not allowed in password.");
		$objid("pass").focus();
		return false;
	}
	
	if($objid("pass").value.length<6 || $objid("pass").value.length>12) 
	{
		
		alert("Password must be between 6 to 12 characters long.");
		$objid("pass").focus();
		return false;
	} 
	
	if($objid("repass").value=='')
	{	
	    alert("Please retype your password.");
		$objid("repass").focus();
		return false;		
	}	
			
	if($objid("pass").value!=$objid("repass").value)
	{
		alert("Password do not match with confirm password.");
		$objid("repass").focus();
		return false;
	}
	
	if($objid("sequrity_question_id").value!='')
	{	
	   if($objid("sequrity_answer").value=='')
		{	
			alert("Please Enter Security Answer.");
			$objid("sequrity_answer").focus();
			return false;		
		}		
	}	
	
	if($objid("varification").value=='')
	{	
		alert("Please Enter Varificaton Code.");
		$objid("varification").focus();
		return false;		
	}
			
	if($objid("varification").value!=$objid("ver_code").value)
	{
		alert(" Please Re Enter Verification code!");
		$objid("varification").focus();
		return false;	
	}
	if($objid("terms").checked==false)
	{	
	    alert("Please check terms and condition.");
		$objid("terms").focus();
		return false;		
	}
	
	
return true;
}
function check_regusername(val)
{
	if($objid('uname').value!="")
	{
	var uid=val;
	ajaxLink("check_ajax.php?action=check_username&uname="+uid,"username_exist");
	}
	if($objid('email').value!="")
	{
	var uid=val;
	ajaxLink("check_ajax.php?action=check_email&uemail="+uid,"useremail_exist");
	}
	return true;
}
function edit_check_regusername(val)
{
	if($objid('uname').value!="")
	{
	var uid=val;
	ajaxLink("check_ajax.php?action=check_edit_username&uname="+uid,"username_exist");
	}
	return true;
}

function refresh_captha()
	{
	url="captcha.php";
	ajaxLink(url,"disp_capcha");
	}	
	
function validateLoginThis(frm)
	{
		if(frm.uname.value=="")
		{
			alert("Please enter username.");
			frm.uname.focus();
			return false;
		}
		if(frm.pass.value=="")
		{
			alert("Please enter password.");
			frm.pass.focus();
			return false;
		}
		return true;
	}
	
function validateForgotPass(frm)
{
	if(frm.email.value=="")
	{
		alert("Please Enter Email.");
		frm.email.focus();
		return false;
	}
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.email.value)))
	{
		alert('Oops! You have forgotten to tell us your Valid Email Address!');
		frm.email.focus();
		return false;
	} 
	return true;
}
	
function validate_edit_profile()
{
	if($objid('fname').value=="")
	{
		alert("Please enter the Firstname");	
		$objid('fname').focus();
		return false;
	}
	if($objid('country').value=="")
	{
		alert("Please select the country");	
		$objid('country').focus();
		return false;
	}
	return true;
}
function disp_row()
{
	if($objid('edit_image').style.display=="none")
	{	
		$objid('edit_image').style.display="";
	}
		else
	{
		$objid('edit_image').style.display="None";
	}
}
function submit_image(a)
{
	if($objid('avatar').value=="")
	{
		alert("Please select the image");
		return false;
	}
	var image=a;
url="check_ajax.php?action=edit_image_code&image="+image;
alert(url);
	ajaxLink(url,"edit_image");
}

function check_ask()
{
	
	if($objid('ask').value=="Ask other people about sex! Type in the question here, and post it for others to answer...")
	{
		alert("Enter your question");
		$objid('ask').focus();
		return false;
	}
	return true;
}
function check_ask_result()
{
	if($objid('ask').value=="Type in the question here, and post it for others to server....")
	{
		alert("Enter your question");
		$objid('ask').focus();
		return false;
	}
	return true;
}
function validate_pass_form()
{
	if(trimAll($objid('pass').value)=="")
	{
		alert("Please enter old password");
		$objid('pass').value="";
		$objid('pass').focus();
		return false;
	}
	if(trimAll($objid('new_pass').value)=="")
	{
		alert("Please enter new password");
		$objid('new_pass').value="";
		$objid('new_pass').focus();
		return false;
	}
	if($objid('new_pass1').value=="")
	{
		alert("Please retype new password");
		$objid('new_pass1').focus();
		return false;
	}
	
  var len=$objid('new_pass').value.length;
 
  if(len<6 && len!=0)
  {
    alert("Please enter at least six characters!");
	$objid('new_pass').focus();
	return false;
  }
  
	if($objid('new_pass1').value!=$objid('new_pass').value)
	{
		alert("Please retype confirm password value");
		$objid('new_pass1').focus();
		return false;
	}
	return true;
}

function sort_question(val)
{
if($objid('sort1').style.display=="")
   { 
	  $objid('sort1').style.display="none";
      $objid('sort2').style.display="";
   }
	else
   { 
	  $objid('sort2').style.display="none";
      $objid('sort1').style.display="";
   }
var order_by=val;
ajaxLink('operation.php?action=index_question&sort=top_answer&order_by='+order_by,'ques');
return true;
}

function chekPassLevel(val){
	var nowLevel=val.length;
	if (nowLevel>5)
	{
		if (nowLevel >15)nowLevel=15;
			$objid('level_img').src="images/lavel_img"+nowLevel+".jpg";
	}
	else
		$objid('level_img').src="images/lavel_img0.jpg";
}

function previewAvtar(val){
	$objid('avtar_image_src').src="member_image/"+val;
	$objid('avtar_image').value=val;
	$objid('avtar_preview').style.display='';
}
function remove_avtar(){
	if(confirm('do you really want to delete uploaded profile image !')){
		var avtar=$objid('avtar_image').value;
		ajaxAction('operation.php?action=delete_avtar&img='+avtar);
		$objid('avtar_preview').style.display='none';
	}
}

function check_contactus()
{
	if($objid('fullname').value=="")
	{
		alert('Please enter your name!');
		$objid('fullname').focus();
		return false;
	} 
	
	if($objid('email').value=="")
	{
		alert('Please enter a valid email address!');
		$objid('email').focus();
		return false;
	}
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($objid('email').value)))
	{
		alert('Please enter a valid email address!');
		$objid('email').focus();
		return false;
	} 
	if($objid('comment').value=="")
	{
		alert('Please Enter Your Comment!');
		$objid('comment').focus();
		return false;
	} 
	
	frm = document.contact_us;
	for(var i=0;i<frm.elements.length;i++)
	{
		if(frm.elements[i].type=="text" || frm.elements[i].type=="textarea")
		{
			if(frm.elements[i].value!="")
			{
				if(!check_blocked_words(frm.elements[i].value,frm.elements[i].name))
					return false;
			}
		}
	}
	
return true;
	
}

function addRowToTable(tblid)
{
  var tbl = $objid(tblid);
  var lastRow = tbl.rows.length;
  
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);  
  var cellLeft = row.insertCell(0);  
  
  cellLeft.innerHTML=jsstr_replace("_trid","_tr"+lastRow,$objid('template_'+tblid).innerHTML);
  
  $objid('remove_'+tblid).style.display='';
 }
function removeRowFromTable(tblid)
{
  var tbl = $objid(tblid);
  var lastRow = tbl.rows.length;
  if (lastRow > 1)
  tbl.deleteRow(lastRow - 1);
  if (lastRow == 2 )
  $objid('remove_'+tblid).style.display='none';
}
function validateRow(frm)
{
  var chkb = $objid('chkValidate');
  if (chkb.checked) {
    var tbl = $objid('tblSample');
    var lastRow = tbl.rows.length - 1;
    var i;
    for (i=1; i<=lastRow; i++) {
      var aRow = $objid('txtRow' + i);
      if (aRow.value.length <= 0) {
        alert('Row ' + i + ' is empty');
        return;
      }
    }
  }
  openInNewWindow(frm);
}
function textCounter(field, countfield, maxlimit) {
/*
* The input parameters are: the field name;
* field that holds the number of characters remaining;
* the max. numb. of characters.
*/
if (field.value.length > maxlimit) // if the current length is more than allowed
field.value =field.value.substring(0, maxlimit); // don't allow further input
else
$objid(countfield).innerHTML = maxlimit - field.value.length;} // set the display field to remaining number

function checkstep3(){
if($objid('name').value=="")
	{
		alert("Please Enter your full name");	
		$objid('name').focus();
		return false;
	}
	if($objid('email').value=="")
	{
		alert("Please Enter the email");	
		$objid('email').focus();
		return false;
	}
	if(!checkValidEmail($objid("email").value))
	{
	    alert("Please Enter valid email address.");
		$objid("email").focus(); 
		return false;
	}	
	if($objid("contact_number").value=="")
	{
		alert("Please enter your contact number.");
		$objid("contact_number").focus(); 
		return false;
	}
	if($objid('state').value=="")
	{
		alert("Please select  state/province .");	
		$objid('state').focus();
		return false;
	}
	if($objid('zipcode').value=="")
	{
		alert("Please enter your zipcode.");	
		$objid('zipcode').focus();
		return false;
	}
	if(!IsNumeric($objid('zipcode').value))
	{
		alert("Please enter only numeric values in  zipcode.");	
		$objid('zipcode').focus();
		return false;
	}
	if($objid('country').value=="")
	{
		alert("Please select the country");	
		$objid('country').focus();
		return false;
	}
	
}
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

// {{{ str_replace
function jsstr_replace(search, replace, subject) {
    // Replace all occurrences of the search string with the replacement string
    // 
    // +    discuss at: http://DD.Agarwal.net/techblog/article/javascript_equivalent_for_phps_str_replace/
    // +       version: 803.3117
    // +   original by: DD Agarwal (http://DD.Agarwal.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)    
    // *     example 1: str_replace(' ', '.', 'DD Agarwal');
    // *     returns 1: 'DD.van.Zonneveld'

    var __regexp_escape = function(text) {
        if (!arguments.callee.sRE) {
            var specials = [
                '/', '.', '*', '+', '?', '|',
                '(', ')', '[', ']', '{', '}', '\\'
            ];
            arguments.callee.sRE = new RegExp(
                '(\\' + specials.join('|\\') + ')', 'g'
            );
        }
    	return text.replace(arguments.callee.sRE, '\\$1');
    };

    var numreplx, numon, fincods, k;
	 
    if(!(replace instanceof Array)){
        replace = new Array(replace);
        if(search instanceof Array){
            // If search is an array and replace is a string, 
            // then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array)){
        // put search string in an array anyway
        search = new Array( search );
    }
    while( search.length > replace.length ){ 
        // If replace has fewer values than search, 
        // then an empty string is used for the rest of replacement values
        replace[replace.length] = '';
    }
 
    if(subject instanceof Array){
        // If subject is an array, then the search and replace is performed 
        // with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k] = str_replace(search, replace, subject[k]);
        }
        return subject;
    }
    	
    // Each entry is replaced all at once, rather than one after another. 
    // This creates a problem: str_replace(["{name}","l"], ["hello","m"], "{name}, lars")
    // Theoretically, the code should return "hello, mars", but instead it returns "hemmo, mars"
    // as pointed out and fixed by Philip Peterson
    numreplx = search.length;
    numon = 0;
    fincods = new Array();
    while( fincods.length < numreplx ){
        nsub = subject;
        for( x = 0; x < fincods.length; x++ ){
            nsub = nsub.replace(new RegExp(__regexp_escape(search[x]), "g"), "[cod"+fincods[x]+"]");
        }
        for( x = 0; x < fincods.length; x++ ){
            nsub = nsub.replace(new RegExp(__regexp_escape("[cod"+fincods[x]+"]"), "g"), replace[x]);
        }
        if( nsub.indexOf("[cod"+numon+"]") == -1 ){
            fincods[fincods.length]=numon;
        }
        numon++;
    }
    for( x = 0; x < fincods.length; x++ ){
        subject=subject.replace(new RegExp(__regexp_escape(search[x]), "g"), "[cod"+fincods[x]+"]");
    }
    for( x = 0; x < fincods.length; x++ ){
        subject=subject.replace(new RegExp(__regexp_escape("[cod"+fincods[x]+"]"), "g"), replace[x]);
    }
    return subject;
}// }}}


function trimAll(sString) 
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString
}