/* ****************************
       form checker
***************************** */

jQuery(document).ready(function($){
    var user_tz = -( new Date().getTimezoneOffset() / 60 );
    $("#timezone-picker option[value="+user_tz+"]").attr('selected', 'selected');
    $("#regform select[name=month], #regform select[name=day]").change(function(e){
        var m = $("#regform select[name=month]").val();
        var d = $("#regform select[name=day]").val();
        if (m == -1 || d == -1) {
            return;
        }
        var sign = find_sign(m, d);
        $("#regform select[name=sign] option[value="+sign.number+"]").attr('selected','selected');
        $("#regform select[name=sign]").change();
    });
});

function cardSwap( string ){
  var opt_key = string.selectedIndex;
  var ccimage = string.options[opt_key].value;
  document.creditcard.src="/secure/images/" + ccimage + ".jpg";
  return true;
}

function isEmail( string ) {
  if (string.search(/^[^.][a-zA-Z0-9!#$%&*+\/=?{|}~._-]*@([A-Za-z0-9-]+\.)+[A-Za-z0-9-]+$/) != -1) return true;
  else return false;
} 

function isName( string ) {
  if (string.search(/^([A-Za-z0-9]?\W?\'?[A-Za-z0-9]+\W?)+$/) != -1 ) return true;
  else return false;
}

function isPassword( string ) {
  if (string.search(/^[\!$%\(-\[\]-~]{3,15}$/) != -1 ) return true;
  else return false;
}

// checking for numeric & 5 chars
function isZipcode( string )
{
	if (string.search(/^\d{5}$/) != -1)
	{
		return true;
	} else {
		return false;
	}
}

function month_to_sign(month, offset) {
    // add 11 (to make january=0), subtract offset, mod by 12 to get a number between 0-11, add 1 to normalize that back to jan=1
    return ((month + 11 - offset) % 12) + 1;
}

function find_sign(month,day) {
    month = parseInt(month);
    day = parseInt(day);
    if (!month || !day) { return {number:0, name:'General'}; }
    var sign_names = ['General', "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces"];
    var thresholds = [ 0, 20, 19, 21, 20, 21, 21, 23, 23, 23, 23, 22, 22 ];
    var when = thresholds[month];
    var sign_int = (day < when) ? month_to_sign(month, 3) : month_to_sign(month, 2);
    return {number:sign_int, name:sign_names[sign_int]};
}

//check the answer to the security question.
// can't contain "a", "an" or "the"
function checkSecurityAnswer(string)
{
	problems = 0;
	string = string.toLowerCase();
	commonWordsArray = new Array ('a', 'an', 'the');
	//check for spaces or common words at the beginning of the string
	if (string.charAt(0) == " ") problems++; //starts with a space
	msg = "";
	for (ansX = 0; ansX < commonWordsArray.length; ansX++)
	{
		//starts with the word
		str = commonWordsArray[ansX] + " ";
		if (string.indexOf(str) == 0) problems++;
		
		//ends with the word (with a space preceding the word)
		str = " " + commonWordsArray[ansX];
		checkPlace = string.length - str.length;
		if (string.indexOf(str, checkPlace) >= 0) problems++; 
		
		//contains the word (with spaces on either side)
		str = " " + commonWordsArray[ansX] + " ";
		if (string.indexOf(str) >0) problems++;
	}

	if (problems > 0) return false;
	else return true;
	}



// for the "why not" form popup
function noFormPopup()
{
  var winUrl = "/signup/why_not_popup.php";
  var winName = "why_not"; 
  window.open(winUrl, winName, "height=640, width=540, location=0, menubar=0, resizable=0, scrollbars=auto, status=0, toolbar=0");
}

// when a country other than USA is selected, hide zipcode
// called onchange for the country select menu
function hideZipcodeNonUS(country)
{
  if ((country == "USA") | (country == ""))
  {
    document.getElementById('zipcodeRow').style.display = "block";
  } else {
    document.getElementById('zipcodeRow').style.display = "none";
  }
}

