// Functions to validate user input

function checkForm(theForm) 
{
	var why = "";
	why += checkUsername(theForm.username.value);
	why += checkPassword(theForm.password.value);
	why += checkSamePassword(theForm.password.value, theForm.password2.value);
	why += checkEmail(theForm.email.value);

	if (why != "") 
	{
		alert(why);
		return false;
	}

  return true;
}

function checkForm2(theForm) 
{
	var why = "";
	why += checkPassword(theForm.password.value);
	why += checkSamePassword(theForm.password.value, theForm.password2.value);
	why += checkEmail(theForm.email.value);

	if (why != "") 
	{
		alert(why);
		return false;
	}

  return true;
}

function checkCustomBanner(theForm) 
{
	var why = "";
	why += checkExists(theForm.name.value, "Sponsor Name");
	why += checkExists(theForm.url.value, "Link to URL");
	why += checkExists(theForm.userfile.value, "Image Location");
	why += checkExists(theForm.mover.value, "MouseOver Text");
	why += checkExists(theForm.altt.value, "Image Alt Text");

	if (why != "") 
	{
		alert(why);
		return false;
	}

  return true;
}

function checkNetworkBanner(theForm) 
{
	var why = "";
	why += checkExists(theForm.name.value, "Sponsor Name");
	why += checkExists(theForm.ad_data.value, "Paste Custom Link");

	if (why != "") 
	{
		alert(why);
		return false;
	}

  return true;
}

function checkExists (type, strng) 
{
	var error = "";
	if (type == "") 
	{
	  error = "You must enter " + strng + "    \n";
	}
	return error;
}

function checkUsername (strng) 
{
  var error = "";
	if ((strng.length < 6) || (strng.length > 14)) 
	{
		error = "Your username must be between 6 and 14 characters.\n";
	}
	var illegalChars = /\W/;
	// allow only letters, numbers, and underscores
	if (illegalChars.test(strng)) 
	{
		error = "Your username can only contain letters, numbers, and underscores.\n";
	} 
  return error;
}

function checkPassword (strng) 
{
  var error = "";

	var illegalChars = /\W/;
	// allow only letters, numbers, and underscores
	if (illegalChars.test(strng)) 
	{
		error = "Your password can only contain letters, numbers, and underscores.\n";
	} 
	if ((strng.length < 6) || (strng.length > 14)) 
	{
		error = "Your password must be between 6 and 14 characters.\n";
	}
  return error;
}

function checkSamePassword (strng, strng2) 
{
  var error = "";
	if (strng != strng2) 
	{
		error = "The passwords you entered are not the same.\n";
	} 
  return error;
}

function checkEmail (strng) 
{
	var error = "";
	if (strng == "") 
	{
	  error = "You must enter an email address.\n";
	}
	return error;
}

function checkSameEmail (strng, strng2) 
{
  var error = "";
	if (strng != strng2) 
	{
		error = "The email addresses you entered are not the same.\n";
	} 
  return error;
}

// JavaScript Document
function setReview(progStr)
{
  if (progStr == '')
  { alert('You must choose a rating'); }
  else
	{
		var str_array = progStr.split("_");
		window.location=('../rating_proc.php?sID=' + str_array[0] + '&rating=' + str_array[1] + '');
  }
}

function imps_math(totalImps, theForm)
{
  var totalSpent = 0;
  var imps_left = 0;
  var numberAds = parseInt(theForm.elements.length) - 6;
  for(var j = 0; j < numberAds; j++)
	{
	  if (theForm.elements[j].value > 0)
		{	totalSpent += parseInt(theForm.elements[j].value); }
	}
  
	imps_left = parseInt(totalImps) - parseInt(totalSpent);
	
	theForm.imps_spent.value = parseInt(totalSpent);
	theForm.imps_rem.value = parseInt(imps_left);
}

function checkSell(theForm) 
{
	var why = "";
	why += checkExists(theForm.imps.value, "Number of Impressions");
	why += checkExists(theForm.price.value, "Price");
	why += checkExists(theForm.price.value, "Email Address");

  if (theForm.imps.value > 10000000)
	{ why += "The maximum number of impressions you can request to purchase is 10,000,000"; }
  if (theForm.imps.price > 10000)
	{ why += "The maximum amount you can offer is $10,000.00"; }

	if (why != "") 
	{
		alert(why);
		return false;
	}

  return true;
}
