var EMPTY_FIELDS        = -801;
var CAPTCHA_NOT_VALID   = -805;
var EMAIL_NOT_VALID     = -806;
var EMAIL_NOT_UNIQUE    = -813;

var ERROR_EMPTY_EMAIL        = 'Please enter your e-mail id<br>';
var ERROR_EMAIL_NOT_VALID    = 'Please enter a valid e-mail id<br>';
var ERROR_EMAIL_NOT_UNIQUE_VCHANNEL = 'There is an existing account on BIGADDA associated with this e-mail id. Please register with a different e-mail id.<br>';
var country_array = new Array("India","Philippines","United Kingdom","United States","Canada","Australia","Pakistan","New Zealand","South Africa","Indonesia","Malaysia");


var SUCCESS = 0;

var AjaxObject = {

    handleSuccess_email:function(o){
	
	if(o.responseText == 'Not Available')
	    {
		error_code = EMAIL_NOT_UNIQUE;
                document.getElementById("error_email").innerHTML = ERROR_EMAIL_NOT_UNIQUE_VCHANNEL;
	    }
	else
	    {
                document.getElementById("error_email").innerHTML = '';
		error_code = 0;
	    }
    },
    handleFailure_email:function(o){
	// Failure handler
    },
    handleSuccess_profileid:function(o){
	document.getElementById("error_profile").innerHTML = o.responseText;
	error_code = 0;
    },
    handleFailure_profileid:function(o){
	// Failure handler
    },
    handleSuccess_city:function(o){
	document.getElementById("citylist").innerHTML = o.responseText;
	error_code = 0;
    },
    handleFailure_city:function(o){
	// Failure handler
    },
    handleSuccess_state:function(o){
     var data= o.responseText;
     if(data.substr(0,4) == "city")
       {
	 document.getElementById("error_state").innerHTML = "";
	 document.getElementById("state_divEle").setAttribute("class", "");
	 document.getElementById("state").disabled = true;
         document.getElementById("statehidden").value = "Nostate";
	 document.getElementById("citylist").innerHTML = data.substr(4);
	 document.getElementById("city").disabled = false;
       }
     else
       {
	 document.getElementById("statehidden").value = "";
	 document.getElementById("statelist").innerHTML = data.substr(4);
	 document.getElementById("city").disabled = true;
       }
	error_code = 0;
    },
    handleFailure_state:function(o){
	// Failure handler
    },
    handleSuccess_state_city:function(o){
	document.getElementById("citylist").innerHTML = o.responseText;
	error_code = 0;
    },
    handleFailure_state_city:function(o){
	// Failure handler
  }
  
};


/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */
var callback_email =
{
    success:AjaxObject.handleSuccess_email,
    failure:AjaxObject.handleFailure_email,
    scope: AjaxObject
};

/*
 * Define the callback object for success and failure
 * handlers as well as object scope.
 */

var callback_profileid =
{
    success:AjaxObject.handleSuccess_profileid,
    failure:AjaxObject.handleFailure_profileid,
    scope: AjaxObject
};

var callback_city=
{
    success:AjaxObject.handleSuccess_city,
    failure:AjaxObject.handleFailure_city,
    scope: AjaxObject
};

var callback_state=
{
 success:AjaxObject.handleSuccess_state,
  failure:AjaxObject.handleFailure_state,
  scope: AjaxObject
  
};


var callback_state_city=
{
  success:AjaxObject.handleSuccess_state_city,
  failure:AjaxObject.handleFailure_state_city,
  scope: AjaxObject
  
};

var callback_city_val=
{
    success:AjaxObject.handleSuccess_city_val,
    failure:AjaxObject.handleFailure_city_val,
    scope: AjaxObject
};



function checkCaptcha(captcha,div_id)
{
    var error_code = validateCaptcha(captcha);
    if(error_code)
	{
	    showError(error_code,div_id);
	}
    else
	{
	    document.getElementById(div_id).innerHTML = '';
	}
}



// checks email id is available  to db(Using AJAX)
/*! 
 * \param email=>string type contains email value
 * \param basr_url contains BASE URL Value
 */
function emailCheckDb(email,base_url, num)
{ 
    var params    = Array();
    params['email'] = email;
    var error_email = validateEmail(params);
    if(error_email)
	{
	  //	    document.getElementById('email_info').style.display = 'none';
	    showError(error_email,'error_email');
	}
    else
      {
	    YAHOO.util.Connect.asyncRequest('POST',base_url+'ajax/signup/is_user_unique/email_id/'+email, callback_email);    
	}    
}

function trim(str, chars)
{
    return ltrim(rtrim(str, chars), chars);
} 

function ltrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+ s", "g"), "");
}


//! method to display/request a new captcha image
/*! 
 * \param img_name => string, value of image src
 * \param base_url => string, value of base url
 * return captcha image on success
 */
function captchaRefresh(img_name, base_url)
{
    document.getElementById(img_name).src = base_url+"/plugins/captcha";    
}


//! Method to validate Captcha/verification code
/*!
 * param name=>consists of data entered by user
 * return Boolean True on success, negative error code on failure
 */
function validateCaptcha(captcha)
{
    var filter  = /^[a-zA-Z0-9]+$/;
    if(captcha.length == 0 || captcha == '')
	{
	    return EMPTY_FIELDS;
	}
    else if(!filter.test(captcha))
	{
	    return CAPTCHA_NOT_VALID;
	}
    else 
	{
	    return SUCCESS;
	}
}


//! Method to validate email address
/*!
 * params associative array consisting following keys
 * email  => string, user's email address
 * return Boolean True on success, negative error code on failure
 */

function validateEmail(params)
{
    var filter = /^[a-z][a-z|0-9|]*([_.][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$/;
    var email = params['email'].toLowerCase();
    
    var domain_check = email.split('@'); 

    if(email.length == 0 || email == '')
	{
	    return EMPTY_FIELDS;
	}
    else if(!filter.test(email))
	{
	    return EMAIL_NOT_VALID;
	}
    else if(domain_check[1] == 'bigadda.com')
	{
	    return EMAIL_NOT_VALID;
	}
    else 
	{
	    return SUCCESS;
	}
}


function isSelect(country)
{ 
    var selected=document.frmSignup.country.value;
    var country_available = 0;
    for(x in country_array)
	{
	    if(country_array[x]== selected)
		{
		    country_available = 1;
		}
	}
      
    if(country_available == 1)
	{
	  YAHOO.util.Connect.asyncRequest('POST',system_base_url+'ajax/signup/get_city/country/'+selected, callback_state);           
	}
    else
	{
	  //document.getElementById('auto_city_on').style.display = 'none';
	  document.getElementById('city').disabled = true;
	}
}


function selectState(country)
{ 
    
    var selected=document.frmSignup.country.value;
    var country_available = 0;
    for(x in country_array)
	{
	    if(country_array[x]== selected)
		{
		    country_available = 1;
		}
	}
      
    if(country_available == 1)
	{
	  YAHOO.util.Connect.asyncRequest('POST',system_base_url+'ajax/signup/get_state/country/'+selected, callback_state);           
	}
    else
	{
	  //document.getElementById('auto_city_on').style.display = 'none';
          document.getElementById("state_divEle").setAttribute("class", "");
          document.getElementById("city_divEle").setAttribute("class", "");
          document.getElementById('state').disabled = true;
	  document.getElementById('city').disabled = true;
	}
}


function selectCity(state)
{
  if(state == "Select State")
    {
     document.getElementById('city').disabled = true;
    }
  else
    {
     YAHOO.util.Connect.asyncRequest('POST',system_base_url+'ajax/signup/get_state_city/country/'+state, callback_state_city);           
    }
}


// Method to show error
/*! 
 * \param error_code=>string contains error number/value
 * \param div_id=>string contains div id where error has to be displayed
 */
function showError(error_code,div_id)
{
  if(div_id == 'error_email')
	{
	    if(error_code  == EMPTY_FIELDS)
		{         
		    document.getElementById(div_id).innerHTML = ERROR_EMPTY_EMAIL;
		}
	    else if(error_code  == EMAIL_NOT_VALID)
		{                 
		    document.getElementById(div_id).innerHTML = ERROR_EMAIL_NOT_VALID;
		}
	    else
		{
		    document.getElementById(div_id).innerHTML = '';
		}
	}
}

$(document).ready(function() {
	$('#allfriends').click(function() {
		$('span.checkbox').css('background-position','0pt -184px');
		$('input.styled1').attr('checked','checked');
		$('#error_select_user').attr('innerHTML','');
	    });
	
	$('#nonefriends').click(function() {	 
		$('span.checkbox').css('background-position','0pt 0pt');
		$('input.styled1').attr('checked','');
	    });
	
	$('#inviteall').click(function() {
		$('.invities input').attr('checked','checked');
		$('#error_select_user').attr('innerHTML','');
	    });

	$('#invitenone').click(function() {
		$('.invities input').attr('checked','');
	    });
	
    })
