var isNav = (navigator.appName.indexOf("Netscape") !=-1);
var saving = false;

function checkSubmit()
{

  var f = document.forms['editad'];

  if(saving == false){
    saving = true;
    f.save.value = "SAVING ...";
    f.save.disabled = true;

    return true;
  } else {
    return false;
  }
}

function validate(){

    var f = document.forms['editad'];
    if (f.idnum.value > 10)
    {
        alert("You have reached the limit of 10 ads!");
       
        return false;
    }
   else
    if (f.category.selectedIndex == 0)
    {
        alert("Please select a category for your ad");
        f.category.focus();
        return false;
    }
    else if (f.description.value == "")
    {
        alert("Please enter a Description for your advertisement");
        f.description.focus();
        return false;
    }
    else if ((f.price.value != "") && isNaN(f.price.value))
    {
        alert("An invalid value for Price was entered. Must be a number.");
        f.price.focus();
        return false;
    }
    else if (f.suburb.value == "")
    {
        alert("Please enter your Suburb/Town");
        f.suburb.focus();
        return false;
    }
    else if (f.city.value == "")
    {
        alert("Please enter your nearest Major City/Town");
        f.city.focus();
        return false;
    }
    /*
    else if ((!f.duration.value) || isNaN(f.duration.value))
    {
        alert("An invalid value for Duration was entered. Must be a number.");
        f.duration.focus();
        return false;
    }*/
    else
    {
        return checkSubmit();
        //return true;
    }
}

function changeCategory(formName){
  var loc = window.location + "";
  var f= document.forms[formName];
  var catID = f.category[f.category.selectedIndex].value;

  if(loc.indexOf("categoryID") < 0){
    loc = loc + "&categoryID="+catID;
  } else {
	 
	  if(loc.substring(loc.length-3,loc.length-2) == "="){
			loc = loc.substring(0,loc.length-2) + catID ;

	  }else if(loc.substring(loc.length-2,loc.length-1) == "="){
			loc = loc.substring(0,loc.length-1 ) + catID ;
	  }
    //loc = loc.substr(0, loc.indexOf("categoryID")+"categoryID=".length) + catID + loc.substr(loc.indexOf("categoryID")+"categoryID=".length+1, loc.length);
  }
  window.location = loc;
}

function changeRegions(formName, selectedRegion){
  var f = document.forms[formName];
  // Clear the curent region array
  for(index = f.region.length; index >=0; index --){
    f.region[index] = null
  }
  var state = f.state[f.state.selectedIndex].value;

  // Add the regions that match the current state
  for(index = 0; index < regions.length; index ++){

    if(regions[index][0] == state){
      f.region.options[f.region.length] = new Option(regions[index][2], regions[index][1]);
      if(selectedRegion != null){
        if(regions[index][1] == selectedRegion){
          f.region.options[f.region.length-1].selected=true;
        }
      }
    }
  }
}

function loadRegions(formName, state){
  var f = document.forms[0];

    // Add the regions that match the current state
  for(index = 0; index < regions.length; index ++){

    if(regions[index][0] == state){

      f.region.options[f.region.length] = new Option(regions[index][2], regions[index][1]);
    }
  }
}

function setRegion(formName, region){
  var f= document.forms[formName];
  for(index = 0; index < f.region.length; index ++){
    if(f.region[index].value == region){
       f.region.selectedIndex = index;
    }
  }
}

function DeleteAd(adID){
  if(confirm("Are you sure you wish to delete this advertisement?")){
    window.location="delete_advertisement.php?id="+adID;
  }
}

function paymentWindow(url){
  window.open(url, "yousell_payment", "width=550,height=550,toolbar=no,scrollbars=yes,location=no,status=yes");
}

function showImage(){
  var source = "ads/display_image.php?id="+imageArray[image];
  if(document.images['image'] != null){
    document.images['image'].src = source;
  }
}

function prevImage(){
 if(image > 0){
  image = image -1;
 } else {
  image = totalimages-1;
 }
 showImage();
}

function nextImage(){
  if(image+1 < totalimages){
    image = image + 1;
  } else {
    image = 0;
  }
  showImage();
}

function imageWindow(baseURL){
//  url = "ads/display_image.php?id=" + imageArray[image];
  url = baseURL + "image_popup.php?id=" + imageArray[image];
  window.open(url, "yousell_image","width=550,height=550,toolbar=no,scrollbars=yes");
}

/*
function prevImage(image){
  var loc = window.location+"";
  if(image > 0){
  if(loc.indexOf("image") < 0){
    loc = loc+"&image="+(image-1);
  } else {
    loc = loc.substr(0, loc.indexOf("image")+"image=".length) + (image-1) + loc.substr(loc.indexOf("image")+"image=".length+1, loc.length);
  }
  window.location = loc;
  }
}

function nextImage(image){
  var loc = window.location+"";
  if(loc.indexOf("image") < 0){
    loc = loc+"&image="+(image+1);
  } else {
    loc = loc.substr(0, loc.indexOf("image")+"image=".length) + (image+1) + loc.substr(loc.indexOf("image")+"image=".length+1, loc.length);
  }
  window.location = loc;
}
*/