var xmlhttp;
var base_url="http://www.flexdirectory.com/";
xmlhttp_init();

if (false) createLoggingPane(false);


function validate() {
	var regex_directory=/^[a-zA-Z0-9]{1}\w{0,31}$/g;
	var regex_email=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var regex_password=/^\S{1,32}$/;
	var username = document.getElementById('username').value;
	var email = document.getElementById('email').value;
	var password = document.getElementById('password').value;
  var password_confirm = document.getElementById('password_confirm').value;
  var tos_check = document.getElementById('tos_check').checked;
  tos_check = true;
	var available;
	var failed = false;
	var checks = true;
  
  // Check to see if the Directory name is valid
	if (!(username.match(regex_directory))){
        	alert("The username must be formatted correctly. It may only contain alphanumeric characters and the underscore character.  It may not be longer than 32 characters.");
		failed = true;
		return false;
  }

 // Check for a valid password and make sure it is typed correctly
	if (!tos_check){
		alert("You must read and agree to the Terms of Service (and check the checkbox)");
		failed = true;
		return false;
	}
  
 // Check for a valid password and make sure it is typed correctly
	if (password != password_confirm){
		alert("Your password does not match.  Please type it in again");
		failed = true;
		return false;
	}
	
  if (!(password.match(regex_password))) {
		alert("Your password must be between 1 and 32 characters long");
		failed = true;
		return false;
	}
  
  // Check for a valid e-mail address
	if (!(email.match(regex_email))){
		alert("Please enter a valid e-mail address.");
		failed = true;
		return false;
	}

    
	var url = base_url + "available.pl?name=" + username;
  //alert(url);
	try {
		xmlhttp.open("GET", url, false);
		xmlhttp.send(null)
   //alert(xmlhttp.responseText);
		eval(xmlhttp.responseText);
	  if (!available) {
	    alert("That directory name is not available.  Please choose a different name.");
	    return false;
	  } else {
      return true;
    }
	} catch (e) {alert(e);} // do nothing
	
	checks = false;
	return false;
}

function validate2() {
  /*var url="http://www.flexdirectory.com/flexbeta.py/check/?directory=";*/
	var regex_directory=/^[a-zA-Z0-9]{1}\w{0,31}$/g;
	var regex_email=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var regex_password=/^\S{1,32}$/;
	var username = document.getElementById('username').value;
	var email = document.getElementById('email').value;
	var password = document.getElementById('password').value;
  var password_confirm = document.getElementById('password_confirm').value;
  /*var tos_check = document.getElementById('tos_check').checked;
  tos_check = true;
  */

  var directory = document.getElementById('username').value;

  // Check to see if the Directory name is valid
	if (!(username.match(regex_directory))){
	  document.getElementById('validation_results').innerHTML = "The username must be formatted correctly. It may only contain alphanumeric characters and the underscore character.  It may not be longer than 32 characters.";
		failed = true;
		return false;
  }

 // Check for a valid password and make sure it is typed correctly
	/*if (!tos_check){
		document.getElementById('validation_results').innerHTML = "You must read and agree to the Terms of Service (and check the checkbox)";
		failed = true;
		return false;
	} */
  
 // Check for a valid password and make sure it is typed correctly
	if (password != password_confirm){
		document.getElementById('validation_results').innerHTML = "Your password does not match.  Please type it in again";
		failed = true;
		return false;
	}
	
  if (!(password.match(regex_password))) {
		document.getElementById('validation_results').innerHTML = "Your password must be between 1 and 32 characters long";
		failed = true;
		return false;
	}
  
  // Check for a valid e-mail address
	if (!(email.match(regex_email))){
		document.getElementById('validation_results').innerHTML = "Please enter a valid e-mail address.";
		failed = true;
		return false;
	}

  document.getElementById('validation_results').innerHTML = "Validating, please wait...";

  var url = "http://www.flexdirectory.com/flexbeta.py/check/?directory="+directory;
  var d = loadJSONDoc(url);
  var gotMetadata = function (meta) {
    if (meta.exists) {
      if (meta.exists=="yes") {
        document.getElementById('validation_results').innerHTML = "The directory is already taken.";
      } else if (meta.exists=='invalid') {
        document.getElementById('validation_results').innerHTML = "The username must be formatted correctly. It may only contain alphanumeric characters and the underscore character.  It may not be longer than 32 characters.";
      } else if (meta.exists=='no') {
        document.getElementById('validation_results').innerHTML = "";
        $('signup').submit();
      }
    }
  };
  var metadataFetchFailed = function (err) {
    /*document.getElementById('result_div').innerHTML = "The metadata for MochiKit.Async could not be fetched: " + err;*/
  };
  d.addCallbacks(gotMetadata, metadataFetchFailed);

	return false;
}

function payment_validate() {
  var payment_amount = document.getElementById('payment_amount').value;
  var cc_exp_mm = document.getElementById('cc_exp_mm').value;
  var cc_exp_yyyy = document.getElementById('cc_exp_yyyy').value;

  payment_amount = payment_amount.replace(/\D/g, '');
  if (payment_amount*1 < 4.95) {
    alert("Sorry, the minimum payment is $4.95");
    return false;
  }

  if (isNaN(cc_exp_mm*1) || cc_exp_mm*1 < 0 || cc_exp_mm*1 > 12) {
    alert("Sorry, Expiration date isn't a 2-digit month (0-12)");
    return false;
  }
  
  
  if (isNaN(cc_exp_yyyy*1) || cc_exp_yyyy*1 < 2006 || cc_exp_yyyy*1 > 2100) {
    alert("Sorry, Expiration year isn't a valid 4-digit year.");
    return false;
  }
  
    
  cc_exp_mm = payment_amount.replace(/\D/g, '');
  return true;
}


function xmlhttp_init()
{
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
 	        try {
		        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
		xmlhttp = new XMLHttpRequest();
}

function help_text(action) {
	var inner_text,inner_text_title;

	if (action == 'people') {
    inner_text_title='Max People:';
    inner_text = 'The maximum number of people that can be listed in the directory.'; 
	} else if (action == 'groups') {
		inner_text_title='Max Groups:';
		inner_text = 'The maximum number of groups that can be listed in the directory.';
	} else if (action == 'ads') {
		inner_text_title='Ads:';
		inner_text = 'Free accounts have google ads shown above the directory (like the demo)';
	} else if (action == 'support') {
		inner_text_title='Support:';
		inner_text = '<p>We try to address questions and issues through e-mail as quickly as possible.</p>'
                +'<p>All paid plans include phone support.</p>';
	}
	
	document.getElementById('help_text').innerHTML='<fieldset><legend style="font-weight:bold;">'+inner_text_title+'</legend>' + inner_text + '</fieldset>';
	return true;
}

function initialize_field(el) {
  
  if(el.className.match(/initial/)) {
    removename(el,'initial');
    el.value = '';
  }
}

function removename(el, name) {
  var i, curList, newList;
  // Remove the given class name from the className property of the element.
  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function toggle(el_id) {
  try {
    el = document.getElementById(el_id);
    el.style.display = (el.style.display == 'none') ? 'block':'none';
  } catch (e) {} // do nothing
}

function hide(el_id) {
  try {
    el = document.getElementById(el_id);
    el.style.display = 'none';
  } catch (e) {} // do nothing
}

