
// THE FOLLOWING JAVASCRIPT FUNCTIONS ARE USED ON THE MERCHANDISE AD ENTRY PAGE

// GLOBAL VAR - used to determine if the ad price has been prefilled
var prefilled = false;
var prefilledPrice = "N/A";

//**************************************//
// call some functions on page load
// onload=init
//**************************************//


//==============================//
//------------------------------//
// GOAL: to determine if the value passed is a number
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}


//==============================//
//------------------------------//
// GOAL: to determine if the value passed is a valid email address
function isValidEmail(str) {
   return (str.indexOf(".") >= 2) && (str.indexOf("@") > 0);
}




//==============================//
//------------------------------//
// GOAL: to alert the user if their phone number or email address is not valid
function validateContactInfo(form,whatToValidate)
{
	var currentContactValue = form.CONTACT_INFO.value;
	var currentPersonalContactValue = form.LISTER_PHONE.value;
	var currentEmailAddressValue = form.LISTER_EMAIL.value;
	var isEmail = true;
	var isPhone = true;
	
	if (whatToValidate=="phone")
	{
		if (currentPersonalContactValue != null && !currentPersonalContactValue == "")
		{
			if (!validatePhoneNumber(currentPersonalContactValue))
			{	
				alert("Please enter a valid phone number in the Phone Number field. \n\n Example phone number: 999-999-9999 "); 	
				form.LISTER_PHONE.focus();
				//form.LISTER_PHONE.value = "";
				return false;
			}
		}
		else return true;
	}
	else if (whatToValidate=="email")
	{
		if (currentEmailAddressValue != null && !currentEmailAddressValue == "")
		{
			if (!isValidEmail(currentEmailAddressValue))
			{	
				alert("Please enter a valid e-mail address in the E-mail Address field. \n\n Example email address: yourname@domainname.com "); 	
				form.LISTER_EMAIL.focus();
				//form.LISTER_EMAIL.value = "";
				return false;
			}
		}
		else return true;
	}
	else
	{
		if (currentContactValue != null && !currentContactValue == "")
		{
			if (!isValidEmail(currentContactValue))
			{	isEmail = false; } 
			if (!validatePhoneNumber(currentContactValue))
			{	isPhone = false; }
			
			// if neither a phone or email address was entered (or was valid), alert the user
			if (isPhone==false && isEmail==false)
			{
				alert("Please enter a valid e-mail address in the E-mail Address field. \n\n Example email address: yourname@domainname.com ");
		  	form.CONTACT_INFO.focus();
				//form.CONTACT_INFO.value = "";
		  	return false; 
			}
			
			// PREFILL FIELDS!
			if (isPhone) // PREFILL THE OTHER PHONE NUMBER FIELD
			{
				//alert("prefill phone"); // DEBUG
				form.LISTER_PHONE.value = currentContactValue;
			}
			if (isEmail) // PREFILL THE OTHER EMAIL FIELD
			{
				//alert("prefill email"); // DEBUG
				form.LISTER_EMAIL.value = currentContactValue;
			}
			
		}
	}
	return true;
}

//==============================//
//------------------------------//
// GOAL: to make sure the user has entered a valid phone number
function validatePhoneNumber(phonenumber) 
{ 
	// ex: (999) 999-9999
	//var phoneRE = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/; 
	
	// ex: 999-999-9999
	var phoneRE = /^\d\d\d\-\d\d\d-\d\d\d\d$/; 
	if (phonenumber.match(phoneRE)) 
	{ 
		//alert("valid #"); // DEBUG
	  return true; 
	} 
	else 
	{ 
	  //alert("Invalid #"); // DEBUG 
	  return false; 
	} 

	
	/*
	if(phonenumber.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) 
	{	return false; }
	else
	{	return true; }
	*/
}






//+++++++++++++++++++++++++++++++++++++++++++++//
//+++++++++++++++++++++++++++++++++++++++++++++//
// OTHER FUNCTIONS FOR MERCHANDISE AD ENTRY PAGE:
//+++++++++++++++++++++++++++++++++++++++++++++//

function popup( url )
{
  win = window.open( url, '','width=800,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,left=50,top=50,screenX=50,screenY=50' );
  win.focus();
}

function popupClear()
{
var x = window.open('','','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=no,copyhistory=no,width=250,height=100,left=200,top=300,screenX=200,screenY=300');
x.document.open('text/html');
x.document.write('<html>');
x.document.write('<head>');
x.document.write('<title>Confirm</title>');
x.document.write('<body topmargin="0" marginheight="0" marginwidth="0" leftmargin="0"><div align="center" valign="middle"><font face="arial, helvetica" size="-1"><p>Are you sure you want to clear your entry?<p><a href="javascript:window.opener.ClearEntry();window.close();">Yes</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:window.close();">No</a></font></div></body>');
x.document.write('</html>');
}


function ClearEntry()
{
	for(count=0;count<document.entry.elements.length;count++)
	{
	//	alert(document.entry.elements[count].type+'='+document.entry.elements[count].value);
	if((document.entry.elements[count].type.indexOf('text')==0) || (document.entry.elements[count].type.indexOf('file')==0))
		document.entry.elements[count].value = '';	
	}
	//window.opener.document.entry.reset();
}

function chkLength()
{
  if( ! document.layers )
  {
    if(document.entry.AD_TEXT.value.length > document.entry.maxChars.value)
    {
      document.entry.AD_TEXT.value=document.entry.AD_TEXT.value.substring(0,document.entry.maxChars.value);
    }
    document.entry.charsTyped.value = document.entry.AD_TEXT.value.length;
  }
}

/*
function resetForm()
{
  if( confirm( "Are you sure you want to erase all the data you've entered?" ) )
  {
    return true;
  }
  else return false;
}	
*/

/*
function copyDescription()
{
  if( document.all )
  {
    document.selection.clear;
    var adText=document.forms.entry.AD_TEXT.createTextRange();
    adText.execCommand("Copy");
  }
}
*/







function extracheck(obj)
{
	return !obj.disabled;
}


function submitThisForm(form)
{
	loadImageName();
	
	var validPhone = validateContactInfo(form,'phone');
	var validEmail = validateContactInfo(form,'email');
	
	if (validPhone==true && validEmail==true)
	{
		//alert("submitting"); // DEBUG
		form.submit();
	}
	else
	{
		//alert("false"); // DEBUG
		return false;
	}
}
