/*-------------------------------------------------------------------------------------------------*/
function ValidateLogin(oForm)
{
	if (oForm.email.value.length == 0)
	{
		alert("Please enter your email address.");
		oForm.email.focus();
		return false;
	} 

	if (oForm.password.value.length == 0)
	{
		alert("Please enter a your password.");
		oForm.password.focus();
		return false;
	} 
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateNewUser(oForm)
{

	var strExp = "^(([a-z]|[A-Z]|[0-9]|_|-)+\.)*([a-z]|[A-Z]|[0-9]|_|-)+@(([a-z]|[A-Z]|[0-9]|_|-)+\.)+([a-z]|[A-Z]|[0-9]|_|-)+$";
	var objRegEx = new RegExp(strExp, "g");
	
	if (!ValidateUpdateUser(oForm)){
		return false;
	}
	
	if (!IsValidEmail(oForm.email.value))
	{
		alert("Please enter a valid Email address.");
		oForm.email.focus();
		return false;
	}
	
	if (oForm.title.value == "")
	{
	  alert("Please enter a Title for your site.");
	  oForm.title.focus();
	  return (false);
	}
		
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸªµºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789- .,\t\r\n\f";
	var checkStr = oForm.title.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
	  ch = checkStr.charAt(i);
	  for (j = 0;  j < checkOK.length;  j++)
	    if (ch == checkOK.charAt(j))
	      break;
	  if (j == checkOK.length)
	  {
	    allValid = false;
	    break;
	  }
	}
	if (!allValid)
	{
	  alert("Please enter only letter, digit and whitespace characters in the \"Site Title\" field.");
	  try{oForm.title.focus();}catch(e){return (false);}
	  return (false);
	}	

	if (oForm.url.value == "")
	{
	  alert("Please enter the URL for your site.");
	  oForm.url.focus();
	  return (false);
	}
	
	if (oForm.CategoryID.value == "-1")
	{
		alert("Please enter a Category for your site.");
		oForm.CategoryID.focus();
		return (false);
	}	

	if (oForm.password.value.length < 5 || oForm.password.value.length > 15)
	{
		alert("Please enter a password between 5 and 15 characters long.");
		oForm.password.focus();
		return false;
	} 

	if (oForm.password.value == oForm.password2.value)
	{
		return true;
	}
	else
	{
		alert("Please enter the same new password twice.");
		oForm.password.focus();
		return (false);
	}
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateUpdateUser(oForm) {

	if (oForm.firstname.value == "")
	{
	  alert("First Name is required.");
	  oForm.firstname.focus();
	  return (false);
	}

	if (oForm.lastname.value == "")
	{
	  alert("Last Name is required.");
	  oForm.lastname.focus();
	  return (false);
	}
	
	if (oForm.company.value == "")
	{
	  alert("Company is required.");
	  oForm.company.focus();
	  return (false);
	}	
		
	if (oForm.phone.value == "")
	{
	  alert("Phone number is required.");
	  oForm.phone.focus();
	  return (false);
	}	

	if (oForm.address1.value == "")
	{
	  alert("Address is required.");
	  oForm.address1.focus();
	  return (false);
	}
	
	if (oForm.city.value == "")
	{
	  alert("City is required.");
	  oForm.city.focus();
	  return (false);
	}

	if (oForm.country.value == "")
	{
	  alert("Country is required.");
	  oForm.country.focus();
	  return (false);
	}

	// If country is U.S., check for valid state and 5 digit postalcode
	if (oForm.country.value == "US") {
		if (oForm.state.value == ""){
		  alert("State is required.");
		  oForm.state.focus();
		  return (false);
		}			
		if (oForm.postalcode.value.length < 5){
		  alert("Zip/Postal Code is required.");
		  oForm.postalcode.focus();
		  return (false);
		}		
	}

	if (oForm.postalcode.value == "")
	{
	  alert("Zip/Postal Code is required.");
	  oForm.postalcode.focus();
	  return (false);
	}
		
	return (true);
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateNewSite(theForm){

  if (theForm.Title.value == "")
  {
    alert("Please enter a Title for this new site in the \"Site Title\" field.");
    theForm.Title.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸªµºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789- .,\t\r\n\f";
  var checkStr = theForm.Title.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter, digit and whitespace characters in the \"Site Title\" field.");
	try{theForm.Title.focus();}catch(e){return (false);}
    return (false);
  }
  
  if (theForm.URL.value.length == 0 || theForm.URL.value == "http://")
  {
    alert("Please enter the domain for this new site in the \"Site URL\" field.");
    theForm.URL.focus();
    return (false);
  }  
 
  if (theForm.MonthlyUniques.value == "")
  {
    alert("Please enter a value for the \"Unique Visitors\" field.");
    theForm.MonthlyUniques.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = theForm.MonthlyUniques.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Unique Visitors\" field.");
    theForm.MonthlyUniques.focus();
    return (false);
  }

  if (theForm.CategoryID.selectedIndex == 0)
  {
    alert("The first \"Website Category\" option is not a valid selection.  Please choose one of the other options.");
    theForm.CategoryID.focus();
    return (false);
  }
  
	return true;
}
/*-------------------------------------------------------------------------------------------------*/

function IsValidEmail(strEmail)
{
	var strExp = "^(([a-z]|[A-Z]|[0-9]|_|-)+\.)*([a-z]|[A-Z]|[0-9]|_|-)+@(([a-z]|[A-Z]|[0-9]|_|-)+\.)+([a-z]|[A-Z]|[0-9]|_|-)+$";
	var objRegEx = new RegExp(strExp, "g");	
	return objRegEx.test(strEmail);
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateChangeEmail(oForm)
{
	var strExp = "^(([a-z]|[A-Z]|[0-9]|_|-)+\.)*([a-z]|[A-Z]|[0-9]|_|-)+@(([a-z]|[A-Z]|[0-9]|_|-)+\.)+([a-z]|[A-Z]|[0-9]|_|-)+$";
	var objRegEx = new RegExp(strExp, "g");
	
	if (!objRegEx.test(oForm.newemail.value))
	{
		alert("Please enter a valid new Email address.");
		oForm.newemail.focus();
		return false;
	}

	if (oForm.newemail.value == oForm.newemail2.value)
	{
		return true;
	}
	else
	{
		alert("Please enter the same new Email twice.");
		oForm.newemail.focus();
		return false;
	}
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateChangePassword(oForm)
{
	if (oForm.newpassword.value.length < 5 || oForm.newpassword.value.length > 15)
	{
		alert("Please enter a password between 5 and 15 characters long.");
		oForm.newpassword.focus();
		return false;
	} 

	if (oForm.newpassword.value == oForm.newpassword2.value)
	{
		return true;
	}
	else
	{
		alert("Please enter the same new password twice.");
		oForm.newpassword.focus();
		return false;
	}
}
/*-------------------------------------------------------------------------------------------------*/

function ValidatePurchase(theForm) {
	var checkOK = "0123456789-";
	var checkStr = theForm.ccard.value;
	var allValid = true;
		
	if ((theForm.ccard.value.length < 11) || (theForm.ccard.value.length > 20)) {
		alert("Please enter your valid credit card number in the \"Credit Card Number\" field.");
		theForm.ccard.focus();
		return (false);
	}
	
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length) {
				allValid = false;
				break;
			}
	}
	
	if (!allValid) {
		alert("Please enter only digit characters in the \"Credit Card Number\" field.");
		theForm.ccard.focus();
		return (false);
	}
	if (theForm.cname.value.length < 2) {
		alert("Please enter your full name in the \"Name On Card\" field.");
		theForm.cname.focus();
		return (false);
	} 
	if (theForm.street.value.length < 4) {
		alert("Please enter your Street Address for this card account in the \"Street Address\" field.");
		theForm.street.focus();
		return (false);
	} 
	if (theForm.zipcode.value.length < 3) {
		alert("Please enter your Zip or Postal Code for this card account in the \"Zip or Postal Code\" field.");
		theForm.zipcode.focus();
		return (false);
	}
	//if (!IsValidEmail(theForm.email.value)) {
	//	alert("Please enter your correct email address in the \"Email Address\" field.");
	//	theForm.email.focus();
	//	return (false);
	//}	
	if (theForm.email.value.length < 5) {
		alert("Please enter your correct email address in the \"Email Address\" field.");
		theForm.email.focus();
		return (false);
	}
	
	return (true);
}
/*-------------------------------------------------------------------------------------------------*/
 
function PurchaseClick() {
	var g_blnUserIsPurchasing = false;	
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateCBStep1(oForm)
{
	var dblCPM;

	if (!isCurrency(oForm.targetcpm.value))
	{
		if (blnIsCPM == 1)
		{
			alert("Please enter your Fixed CPM Price as currency.");
		}
		else
		{
			if (blnIsCPM == 3)
			{
				alert("Please enter your Dynamic Max CPM Price as currency.");
			}
			else
			{
				alert("Please enter your CPC Price as currency.");
			}
		}
		oForm.targetcpm.focus();
		return (false);
	}
		
	if (oForm.targetcpm.value.length == 0)
	{
		if (blnIsCPM == 1)
		{
			alert("Please enter your Fixed CPM Price.");
		}
		else
		{
			if (blnIsCPM == 3)
			{
				alert("Please enter your Dynamic Max CPM Price.");
			}
			else
			{
				alert("Please enter your CPC Price.");
			}		
		}	
		oForm.targetcpm.focus();
		return (false);
	}
	
	/* -- Commented out the code that requires a minimum for these values - per JB's request
	dblCPM = CurrencyToFloat(oForm.targetcpm.value);
	if (dblCPM < intMinCPM)
	{
		if (blnIsCPM == 1)
		{
			alert("Please enter at least " + strMinCPM + " for your Fixed CPM Price.");
		}
		else
		{
			if (blnIsCPM == 3)
			{
				alert("Please enter at least " + strMinCPM + " for your Dynamic Max CPM Price.");
			}
			else
			{
				alert("Please enter at least " + strMinCPM + " for your CPC Price.");
			}				
		}
		oForm.targetcpm.focus();
		return (false);
	}
	-- */

	if (!isCurrency(oForm.budget.value))
	{
		alert("Please enter your Total Budget as currency.");
		oForm.budget.focus();
		return (false);
	}	
	if (oForm.budget.value.length == 0)
	{
		alert("Please enter your Total Budget.");
		oForm.budget.focus();
		return (false);
	}
		
	return (true);
	
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateCBStep3(oForm)
{
	if (oForm.namedItem("name").value.length == 0)
	{
		alert("Please enter a Name for your ad.");
		oForm.namedItem("name").focus();
		return (false);
	}
	
	if (oForm.richmedia.value.length == 0)
	{
		if (oForm.clickurl.value.length == 0 || oForm.clickurl.value == "http://")
		{
			alert("Please enter a Click URL.");
			oForm.clickurl.focus();
			return (false);
		}
	}
	
	if (oForm.adxpert.value == "True")
	{
		if (oForm.categoryid.value.length == 0)
		{
			alert("Please select an AdXpert Category.");
			oForm.categoryid.focus();
			return(false);
		}

		if (oForm.targetcpa.style.visibility != 'hidden')
		{								
			if (oForm.targetcpa.value.length == 0)
			{
				alert("Please enter your Target CPA Goal.");
				oForm.targetcpa.focus();
				return(false);
			}
		
			if (oForm.targetcpa.value.indexOf("-") > -1)
			{
				alert("Please enter a minimum of $0.00 for Target CPA Goal.");
				oForm.targetcpa.focus();
				return(false);
			}

			if (!isCurrency(oForm.targetcpa.value))
			{
				alert("Please enter your Target CPA Goal as currency.");
				oForm.targetcpa.focus();
				return (false);		
			}
		}
		
	}

	if (oForm.currentmediaurl.value.length == 0 && oForm.richmedia.value.length == 0)
	{
		alert("Please upload Creative or enter Rich Media.");
		cboCreativeType.focus();
		return (false);
	}
	
	if (oForm.richmedia.value.length > 8000)
	{
		alert("Ad size limit has been exceeded. Please make sure your HTML or Javascript code is under 8000 characters in length.");
		try
		{
			richmedia.focus();
		}catch(e){}		
		
		return (false);
	}	
		
	return (true);
	
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateDefaultAds(oForm)
{
	
	if (oForm.RichMedia.value.length > 8000)
	{
		alert("Ad size limit has been exceeded. Please make sure your HTML or Javascript code is under 8000 characters in length.");
		try
		{
			oForm.RichMedia.focus();
		}catch(e){}		
		
		return (false);
	}	
	
	if (!isCurrency(oForm.FloorCPM.value))
	{
		alert("Please enter your Floor CPM as currency.");
		oForm.FloorCPM.focus();
		return (false);		
	}	
		
	return (true);
	
}
/*-------------------------------------------------------------------------------------------------*/

function ValidateEditAd(oForm)
{

	if (oForm.adurl.value.length == 0 || oForm.adurl.value == "http://")
	{
		alert("Please enter a Click URL.");
		oForm.adurl.focus();
		return (false);
	}
	
	if (oForm.adxpert.value == "True")
	{
		if (oForm.adcategory.value.length == 0)
		{
			alert("Please select an AdXpert Category.");
			oForm.adcategory.focus();
			return(false);
		}

		if (oForm.cpc.value == "True")
		{
			if (oForm.adunitprice.value.length == 0)
			{
				alert("Please enter your Target CPA Goal.");
				oForm.adunitprice.focus();
				return(false);
			}
		
			if (oForm.adunitprice.value.indexOf("-") > -1)
			{
				alert("Please enter a minimum of $0.00 for Target CPA Goal.");
				oForm.adunitprice.focus();
				return(false);
			}

			if (!isCurrency(oForm.adunitprice.value))
			{
				alert("Please enter your Target CPA Goal as currency.");
				oForm.adunitprice.focus();
				return (false);		
			}
		}		
	}
		
	return (true);
	
}

/*-------------------------------------------------------------------------------------------------*/

function CurrencyToFloat(strCur)
{
	var intIndex = 0;
	var strNewCur = "";
		
	for (intIndex = 0; intIndex < strCur.length; intIndex++)
	{
		if (strCur.charAt(intIndex) == "0" ||
			strCur.charAt(intIndex) == "1" ||
			strCur.charAt(intIndex) == "2" ||
			strCur.charAt(intIndex) == "3" ||
			strCur.charAt(intIndex) == "4" ||
			strCur.charAt(intIndex) == "5" ||
			strCur.charAt(intIndex) == "6" ||
			strCur.charAt(intIndex) == "7" ||
			strCur.charAt(intIndex) == "8" ||
			strCur.charAt(intIndex) == "9" ||
			strCur.charAt(intIndex) == ".")
		{
			strNewCur = strNewCur + strCur.charAt(intIndex);
		}
	}	
	
	return(parseFloat(strNewCur));
	
}
/*-------------------------------------------------------------------------------------------------*/

function isCurrency(f)
{
	var nNum = 0;			// Total numbers for currency value.
	var nDollarSign = 0;	// Total times a dollar sign occurs.
	var nDecimal = 0;		// Total times a decimal point occurs.
	var nCommas = 0;		// Total times a comma occurs.
	var txtLen;				// Length of string passed.
	var xTxt;				// Assigned object passed.
	var sDollarVal;			// Assigned dollar amount with or without commas.
	var bComma;				
	var decPos;				// Assigned value of numbers or positions after decimal point.
	var nNumCount = 0;		// Total numbers between commas.
	var i;					// For loop index.
	var x;					// Assigned each individual character in string.

	// Set the xTxt variable to the object passed to this function.
	// Assign the length of the string to txtLen.
	xTxt = f;
	txtLen = xTxt.length

	for(i = 0; i < txtLen; i++)
	{
		// Assign character in substring to x.
		x = xTxt.substr(i, 1);

		if(x == "$")
			nDollarSign = nDollarSign + 1; // Sum total times dollar sign occurs.
		else if(x == ".")
			nDecimal = nDecimal + 1; // Sum total times decimal point occurs.
		else if(x == ",")
			nCommas = nCommas + 1; // Sum total times comma occurs.
		else if(parseInt(x) >= 0 || parseInt(x) <= 9)
			nNum = nNum + 1; // If the character is a number sum total times a number occurs.
		else
		{
			// Error occurs if any other character value is in the string
			// other than the valid characters.
			return false;
		}
	}

	if(nDollarSign > 1)
	{
		return false;
	}

	if(nDecimal > 1)
	{
		return false;
	}

	if(nDollarSign == 1)
	{
		// Make sure dollar sign is the first character in string
		// if there is a dollar sign present.
		if(xTxt.indexOf("$") != 0)
		{
			return false;
		}
	}

	if(nDecimal == 1)
	{
		// Get the number of numbers after the decimal point in
		// the string if there is a decimal point present
		decPos = (txtLen - 1) - xTxt.indexOf(".");

		// Floating point cannot be more then two.
		// Valid format after decimal point.
		/**********************************/
		/*   $#.##, $#.#, $.#, $#., $.##  */
		/**********************************/
		if(decPos > 2)
		{
			return false;
		}
	}
	
	if(nCommas == 0)
	{
		// If no commas are present value is a valid US
		// currency.
		return true;
	}
	else
	{
		// Get total number of dollar number(s), removing
		// floating point numbers or cents.
		nNum = nNum - decPos;

		// Determine if dollar sign is in string so to be 
		// removed.
		// After determining dollar sign, assign sDollarVal
		// numbers and comma(s)
		if(xTxt.indexOf("$", 0) == 0)
			sDollarVal = xTxt.substr(1, (nNum + nCommas));
		else
			sDollarVal = xTxt.substr(0, (nNum + nCommas));
	
		// Determine if a zero is the first number or if a
		// comma is the first or last character in the string.
		if(sDollarVal.lastIndexOf("0", 0) == 0 )
		{
			return false;
		}
		else if(sDollarVal.lastIndexOf(",", 0) == 0)
		{
			return false;
		}
		else if(sDollarVal.indexOf(",", (sDollarVal.length - 1)) == (sDollarVal.length - 1))
		{
			return false;
		}
		else
		{
			// Initialize bComma indicating a comma has not occured yet.
			bComma = false;
			for(i = 0; i < sDollarVal.length; i++)
			{
				// Assign charater in substring to x.
				x = sDollarVal.substr(i, 1);

				if(parseInt(x) >= 0 || parseInt(x) <= 9)
				{
					// If x is a number add one to the number counter.
					nNumCount = nNumCount + 1;

					// Since comma(s) are present number counter cannot
					// be more then three before the first or next comma.
					if(nNumCount > 3)
					{
						return false;
					}
				}
				else
				{
					// If the number counter is less then three and
					// the comma indicator is true the comma is either
					// mis-placed or there are not enough values.
					if(nNumCount != 3 && bComma)
					{
						return false;
					}

					// Reset the number counter back to zero.
					nNumCount = 0;

					// Set the comma indicator to true indicating
					// that the first comma has been found and that
					// there now MUST be three numbers after each
					// comma until the loop hits the end.
					bComma = true;
				}
			}

			// Determine if after the loop ended that there
			// was a total of three final numbers after the
			// last comma.
			if(nNumCount != 3 && bComma)
			{
				return false;
			}
		}
	}

	// Return true indicating that the value is a valid currency.

	return true;
}
/*-------------------------------------------------------------------------------------------------*/

function ValidatePayee(oForm){
	
	// validate Payee Name
	if(oForm.payeename.value.length == 0) {
		alert("Please enter a Payee Name before updating your account information.")
		oForm.payeename.focus();	
		return false;
	}
	
	// if paypay is selected payment method, validate the paypal email address
	if(oForm.method1.checked){	
		if(!IsValidEmail(oForm.paypalemail.value)) {
			alert("The Paypal E-Mail Address you entered is not a valid email address.  Please correct this before updating your account information.")
			oForm.paypalemail.focus();
			return false;
		}
	}
	return true;
}