function get_checked_radio_status(id)
{
	var found_it_radio //initial value is null because we gave it no other value
	for (var i=0; i<id.length; i++)  
	{
		if (id[i].checked)  
		{
			found_it_radio = id[i].value //set found_it equal to checked button's value
		} 
	}
	return found_it_radio;
}

function get_checked_select_status(id)
{
	var found_it_select //initial value is null because we gave it no other value
	for (var i=0; i<id.length; i++)  
	{
		if (id[i].selected)  
		{
			found_it_select = id[i].value //set found_it equal to checked button's value
		} 
	}
	return found_it_select;
}

function get_id(id)
{
	return id=document.getElementById(id);
}

function popup_volatility_box(text)
{
//testwindow= window.open ("", "mywindow","location=1,status=1,scrollbars=1,width=100,height=100");
//testwindow= window.open('', '', 'width=200, height=77, directories=no, location=no, menubar=no, resizable=no, scrollbars=1, status=no, toolbar=no'); 

 var width  = 580;
 var height = 250;
 var left   = (screen.width  - width);
 var top    = (screen.height - height);
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=yes';
 params += ', scrollbars=yes';
 params += ', status=no';
 params += ', toolbar=no';
 testwindow=window.open('','', params);
 //if (window.focus) {newwin.focus()}


//testwindow.moveTo(20,20);
testwindow.document.body.innerHTML=text;
}


function submit_form(formName)
{
	var id;
	id=document.getElementById(formName);
	id.submit();
}

function change_feed_back_class()
{
	var id=get_id('feedback');
	message=escape(id.value);
	//alert(message);
	if(message=="Thank%20you%20for%20taking%20the%20time%20to%20visit%20our%20site.%20We%20would%20appreciate%20any%20suggestions%20or%20comments%20in%20this%20box%20that%20will%20help%20us%20provide%20you%20with%20the%20best%20user%20experience.%20We%27d%20love%20to%20hear%20what%20you%20think%21" || message=="Your%20feedback%20has%20been%20submitted%20successfully.")
	{
		var id;
		id=document.getElementById('feedback');
		id.value='';
		id.setAttribute("class", "input3");
	}
}

function redirect(data)
{
	//alert(data);
	window.location=site_js_url+'index.php?err=no_data';
}


function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return "Enter a valid e-mail address.";
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return "Enter a valid e-mail address."
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return "Enter a valid e-mail address.";	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
		return "Enter a valid e-mail address.";	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return "Enter a valid e-mail address.";	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
		return "Enter a valid e-mail address.";	 }
	
	 if (str.indexOf(" ")!=-1){
		return "Enter a valid e-mail address.";	 
	}
	
	 return true					
}

//Function to check numeric values only
function IsNumeric(strString)
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
}

function validate_login()
{
	id=document.getElementById('user_name');
	h_id='hid_'+'user_name';	
	document.getElementById(h_id).style.display='none';
	document.getElementById(h_id).innerHTML='';
	if(id.value=='')
	{
		document.getElementById(h_id).style.display='';
		document.getElementById(h_id).innerHTML='Enter Email';
		return false;
	}
	if(echeck(id.value)!=true)
	{
		document.getElementById(h_id).style.display='';
		document.getElementById(h_id).innerHTML=echeck(id.value);
		id.focus();
		return false;
	}
	id=document.getElementById('pass');
	h_id='hid_'+'pass';	
	document.getElementById(h_id).style.display='none';
	document.getElementById(h_id).innerHTML='';
	if(id.value=='')
	{
		document.getElementById(h_id).style.display='';
		document.getElementById(h_id).innerHTML='Enter Password';
		return false;
	}
	return true;
}

//Function for signup validation
function validate_signup()
{ 
	id=document.getElementById('user_id');
	h_id='hid_'+'user_id';	
	document.getElementById(h_id).style.display='none';
	document.getElementById(h_id).innerHTML='';
	document.getElementById('hid_name').style.display='none';
	document.getElementById('hid_name').innerHTML='';
	if(document.getElementById('name').value =='')
	{
		document.getElementById('hid_name').style.display='';
		document.getElementById('hid_name').innerHTML='Please Enter Your Name';
		return false;
	}
	if(!IsValidName(document.getElementById('name').value))
	{
		document.getElementById('hid_name').style.display='';
		document.getElementById('hid_name').innerHTML='Enter alphabats only';
		return false;
	}

	if(id.value=='')
	{
		document.getElementById(h_id).style.display='';
		document.getElementById(h_id).innerHTML='Please Enter Email Address';
		return false;
	}
	if(echeck(id.value)!=true)
	{
		document.getElementById(h_id).style.display='';
		document.getElementById(h_id).innerHTML=echeck(id.value);
		id.focus();
		return false;
	}

	id=document.getElementById('profession');
	h_id='hid_'+'profession';
	document.getElementById(h_id).style.display='none';
	document.getElementById(h_id).innerHTML='';
	if(id.value != '')
	{
		if(isSplChar(id.value))
		{
			document.getElementById(h_id).style.display = '';
			document.getElementById(h_id).innerHTML = 'You can not use special chars';
			return false;
		}		
	}

	id=document.getElementById('location');
	h_id='hid_'+'location';
	document.getElementById(h_id).style.display='none';
	document.getElementById(h_id).innerHTML='';
	if(id.value != '')
	{
		if(isSplChar(id.value))
		{
			document.getElementById(h_id).style.display = '';
			document.getElementById(h_id).innerHTML = 'You can not use special chars';
			return false;
		}		
	}
	add_signup();
}

//Function for add signup using ajax
function add_signup()
{
	var id=get_id('user_id');
	email=escape(id.value);
  var id=get_id('name');
	name= id.value;
	xmlHttp1=check_ajax_support();
	if (xmlHttp1==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}
	var url=site_js_url+"ajax_response_files/signup_response.php";
	url=url+"?act=add_user";
  url=url+"&name="+name;
	url=url+"&email="+escape(email);
	if(get_id('gender_m').checked) {
		url=url+"&gender="+escape(get_id('gender_m').value);
	} else if(get_id('gender_f').checked) {
		url=url+"&gender="+escape(get_id('gender_f').value);
	}
	url=url+"&ethnicity="+escape(get_id('ethnicity').value);
	url=url+"&age="+escape(get_id('age').value);
	url=url+"&profession="+escape(get_id('profession').value);
	url=url+"&marital_status="+escape(get_id('marital_status').value);
	url=url+"&location="+escape(get_id('location').value);
	xmlHttp1.open("POST",url,true);
	xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp1.setRequestHeader("Content-length",url.length);
	xmlHttp1.setRequestHeader("Connection", "close");
	xmlHttp1.send(url);
	document.getElementById('signup_confirm').style.display='block';
	document.getElementById('signup_confirm').innerHTML='<div><img src="images/loadingAnimation.gif"></div>';
	xmlHttp1.onreadystatechange=function()
	{
		if(xmlHttp1.readyState==4)
		{
			//alert(xmlHttp1.responseText);
			if(xmlHttp1.responseText != '')
			{
				if (xmlHttp1.responseText == 'Email Already Exists.') {
					document.getElementById('hid_user_id').style.display='block';
					document.getElementById('hid_user_id').innerHTML=xmlHttp1.responseText;
					document.getElementById('signup_confirm').style.display='none';
				} else {
					document.getElementById('signup_confirm').style.display='block';
					document.getElementById('signup_confirm').innerHTML=xmlHttp1.responseText;
				}
			}
		}
	}
}
function IsValidName(sText) {
  var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  var IsCharacter=true;
  var Char;
	sText = trim(sText);
	if(sText.length == 0) {
		return false;
	}
  for (i = 0; i < sText.length && IsCharacter == true; i++) {
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) {
      IsCharacter = false;
    }
  }
  return IsCharacter;
}

// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

/*Check for a special character'***/
function isSplChar(str) {	
	var spchar, getChar, SpecialChar;
	spchar="`()(~!^&*+\"|%:=<>#";
	getChar='Empty';
	SpecialChar='No';
	//var spchars =" ` ( )  ~ ! @ ^ & * + \" | : =  < > "; 
	for(var i=0; i < str.length; i++)
	{
		for(var j=0; j < spchar.length; j++)
		{			
			if(str.charAt(i)== spchar.charAt(j))
			{			
				SpecialChar='Yes';
				break;
			}
			else
			{
				if (str.charAt(i)!=' ')
				getChar='Normal';
			}
		}		
	}
	if (SpecialChar == 'Yes')
	{
		//alert('Please do not enter any of the following characters: \n ' + spchars);	
		return true;
	}
	else if (SpecialChar == 'No')
	{
		return false;
	}
}
