///search through characters one by one until we find a non-whitespace 
//character, when we do, return false, if we don't then return true.
 
function isWhiteSpace(c) {
 
  var whitespace = " \t\n\r\,\.";
 
  //check that current character isn't whitespace
  if (whitespace.indexOf(c) == -1) return false;   
 
  //all characters are whitespace
  return true;
}
 
function countWords(s) {
 
  var wordCount = 0
  var newWord = true
 
  for(i = 0; i < s.length; i++) {
                if(newWord && ! isWhiteSpace(s.charAt(i))) {
                                wordCount = wordCount + 1
                                newWord = false;
                }
                if(newWord == false && isWhiteSpace(s.charAt(i))) {
                                newWord = true
                }
  }
  return wordCount;
}
 
function checkRequired(f) {
 
 
 
   }
 
 
function disableUpload(f) {
                if ( ! document.getElementById )  return;
 
            var Category = document.getElementById('photoupload');
            if ( Category == null )  alert("photoupload");
 
            Category.style.visibility = 'hidden';
 
            Category = document.getElementById('adborders');
            Category.style.visibility = 'visible';
 
            calcCost(f);
 
            return;
            }            
 
function enableUpload(f) {
                if ( ! document.getElementById )  return;
 
            var Category = document.getElementById('photoupload');
            if ( Category == null )  alert("photoupload");
 
            Category.style.visibility = 'visible';
 
            Category = document.getElementById('adborders');
            Category.style.visibility = 'hidden';
 
            f.border.value = 'No Border';
 
            calcCost(f);
 
            return;
            }            
 
 
function calcCost(f) { 


 
   numWords = countWords(f.placeAd.value);
   f.numberWords.value = numWords;
   
   if (numWords > 50 && f.adtype[0].checked){
                                                                  
        f.placeAd.focus();
        alert('You have too many words.\nA Classified Ad can include up to 50 words.');
	document.getElementById('submitButtonName').disabled = true;
        return;
   }
   if (f.adtype[1].checked && numWords > 25){
                                                                  
        f.placeAd.focus();
        alert('You have too many words.\nA Photo Ad can only have 25 words.');
	document.getElementById('submitButtonName').disabled = true;
        return;
   }
   document.getElementById('submitButtonName').disabled = false;
 
   extraCost=0
   numWords2 = numWords - 25
   ad_cost = 0
   if (numWords2 >0) { 
   		extraCost = parseInt((numWords2/5)+ 0.9) 
       }
 
     if (f.adtype[0].checked) {
		  
		  var Number_of_Weeks_to_Charge = parseInt(f.Number_of_Weeks_to_Run.value)-(parseInt(parseInt(f.Number_of_Weeks_to_Run.value)/3));
                ad_cost = (parseInt((parseInt(Number_of_Weeks_to_Charge) + 1) / 2 ) * 18)+(parseInt(Number_of_Weeks_to_Charge / 2) * 12);         

        ad_cost = 8.5 + (extraCost * 0.5);
        ad_cost = ad_cost * Number_of_Weeks_to_Charge;
        }
     else {
        var Number_of_Weeks_to_Charge = f.Number_of_Weeks_to_Run.value;
		ad_cost = (parseInt((parseInt(f.Number_of_Weeks_to_Run.value) + 1) / 2 ) * 18)+(parseInt(parseInt(f.Number_of_Weeks_to_Run.value) / 2) * 12); 
          }

 
     if (f.border.value == 'Solid Line') {
        ad_cost = ad_cost + (2 * Number_of_Weeks_to_Charge);
        }
     else if (f.border.value != 'No Border') {
        ad_cost = ad_cost + (4 * Number_of_Weeks_to_Charge);        
        }
 
     if (ad_cost == parseInt(ad_cost)) {
                   f.billAmount.value = "$" + ad_cost + ".00"
        }
     else {
                   f.billAmount.value = "$" + ad_cost + "0"
        }
                }
 
function SendAd (f) {
   calcCost(f);
   if ( f.Name.value =="" ) {
      f.Name.focus();
      alert("You must enter your name and address information.");
      return false;
      }
   if ( f.Address.value =="" ) {
      f.Address.focus();
      alert("You must enter your name and address information.");
      return false;
      }
   if ( f.City.value =="" ) {
      f.City.focus();
      alert("You must enter your name and address information.");
      return false;
      }
   if ( f.State.value =="" ) {
      f.State.focus();
      alert("You must enter your state.");
      return false;
      }
   if ( f.Zip.value =="" ) {
      f.Zip.focus();
      alert("You must enter your zip code.");
      return false;
      }
   if ( f.Phone.value =="" ) {
      f.Phone.focus();
      alert("You must enter your phone number.");
      return false;
      }
   if ( f.placeAd.value =="" ) {
      f.placeAd.focus();
      alert("You must enter your ad text.");
      return false;
      }
   if (( f.adtype.value == "photo") && ( f.uploaded_file.value == "")) {
      f.uploaded_file.focus();
      alert("You need to select a photo for upload.");
      return false;
     }
 
    f.submit();
   }
 

