// 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)
{
	
}

//==============================//
//------------------------------//
// 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) && phonenumber != '') 
	{ 
		//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 extracheck(obj)
{
	return !obj.disabled;
}


function submitThisForm()
{

	var form = document.entry 
	
	var currentContactValue = form.CONTACT_EMAIL.value;
	var currentPersonalContactValue = form.LISTER_PHONE.value;
	var currentEmailAddressValue = form.LISTER_EMAIL.value;
	var currentPriceValue = form.PRICE.value;
	var currentCategoryValue = form.CLASS.value;
	var currentTitleValue = form.TITLE.value;
	var currentNumPhotosValue = form.PHOTO_COUNT.value;
	var currentDescriptionValue  = form.AD_TEXT.value;
	var currentPhoneValue = form.LISTER_PHONE.value;
	
	
	var errorMessage = "";
	
	var isEmail = true;
	var isPhone = true;
	
	var submitForm = true;
	
	
		if(currentCategoryValue < 1){
			errorMessage += "<div class=\"errorBullets\">Please select a category for your listing.</div>";
			var submitForm = false;
		} 
		if(currentTitleValue == null || currentTitleValue == ""){
			errorMessage += "<div class=\"errorBullets\">Please provide a title for your merchandise listing.</div>";
			var submitForm = false;
		}
		
		if(currentContactValue == null || currentContactValue == "" || currentEmailAddressValue == null || currentEmailAddressValue == ""){
		
			errorMessage += "<div class=\"errorBullets\">Please enter a e-mail address.</div>";
			var submitForm = false;
		
		} else {
			if (!isValidEmail(currentContactValue) || !isValidEmail(currentEmailAddressValue))
			{	
				errorMessage += "<div class=\"errorBullets\">Please enter a correctly formatted e-mail address.</div>";
			var submitForm = false;
				form.LISTER_EMAIL.focus();
			}
		}
		
		if (currentPriceValue == null || currentPriceValue == "" && !form.PRICE.disabled) {
			errorMessage += "<div class=\"errorBullets\">Please enter a price.</div>";
			var submitForm = false;
		} else {
		
			if(!IsNumeric(currentPriceValue) && !form.PRICE.disabled){
				errorMessage += "<div class=\"errorBullets\">Please enter a correctly formatted price.</div>";
				var submitForm = false;
			}
		
		}
		
		
		if(currentNumPhotosValue == "0")
		{
			document.entry.action = "http://commerce.washingtonpost.com/cgi-bin/WPclass/shopping/oae/listingPreview";
		}
		else {
			document.entry.action = "http://commerce.washingtonpost.com/cgi-bin/WPclass/shopping/oae/photoUpload";
		}
		
		if(currentDescriptionValue == null || currentDescriptionValue == "")
		{
			errorMessage += "<div class=\"errorBullets\">Please enter a description for your listing.</div>";
			var submitForm = false;
		}
		
		if(currentPhoneValue != "")
		{
			if(!validatePhoneNumber(currentPhoneValue)){
				errorMessage += "<div class=\"errorBullets\">Please enter a correctly formatted phone for Contact Information.</div>";
			var submitForm = false;
			}
		}
			
	

	
	if(submitForm){
		form.submit();
	}else{
		document.getElementById("errorMessageDisplay").innerHTML = errorMessage+"<br>";
		return false;
	}
	




}

