ieandmac = 0;

function display(index, text) 
{
var name = 'linktext' + index;

if (document.all) document.all[name].innerHTML=text;
else if (document.layers) document.layers[name].innerHTML=text;
else document.getElementById(name).innerHTML=text; 
}


function productwindow(prodpage)
{
    nBrowser = navigator.appName;
    vBrowser = parseInt(navigator.appVersion);
    if(nBrowser=="Netscape" && vBrowser>=3 || nBrowser=="Microsoft Internet Explorer" && vBrowser>=4) 
    {
      noiZ = window.open(prodpage,"Chippendale","width=660,height=550,toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=yes");
    }
    else{
        window.open("error.html","Chippendale","width=660,height=450,toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=no");
    }
}

function searchwindow(searchpage)
{
    nBrowser = navigator.appName;
    vBrowser = parseInt(navigator.appVersion);
    if(nBrowser=="Netscape" && vBrowser>=3 || nBrowser=="Microsoft Internet Explorer" && vBrowser>=4)  
    {
      window.opener.location.href=searchpage;
      window.opener.focus();
    }  
    else{
        window.open("error.html","Search","width=660,height=450,toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=no");
    }
}

function premisewindow(thepage)
{
    nBrowser = navigator.appName;
    vBrowser = parseInt(navigator.appVersion);
    if(nBrowser=="Netscape" && vBrowser>=3 || nBrowser=="Microsoft Internet Explorer" && vBrowser>=4) noiZ = window.open(thepage,"NewPremises","width=580,height=400,toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=yes");
    else{
        window.open("error.html","NewPremises","width=580,height=400,toolbar=no,menubar=no,status=yes,scrollbars=no,resizable=no");
    }
}


function clearEmail(){
    if(document.forms[0].email_address.value == "Email Address"){
        document.forms[0].email_address.value = "";
    }
}

function checkRequiredFields(form)
{
    var marker = 0;

    for (var i=0; i<form.elements.length; i++){
        if (form.elements[i].name.substring(0,3) == "req")
	  {
	      if (form.elements[i].value == "")
	      {
		    marker = 1;
		    alert("You have not filled out all of the required fields");
                form.elements[i].focus();
		    break;
            } 
        }  
	  if (form.elements[i].value == "Email Address")
        {
	      marker = 1;
	      alert("You have not filled in your Email Address");
            form.elements[i].focus();
	      break;
        }
    }
    if(!marker){
        return true;
    }
    else{
        return false;
    }
}

function performSearch(form)
{
    var sAction;
    var iIndex;
    var bContinue=0;
    var i;
    for (i=0; i<form.elements.length; i++)
    {
      if (form.elements[i].value != "")
      {
        bContinue++;
      }
    }
    
    if (!bContinue)
    {  
      alert("Please enter either a search word or a product number to search our website.");
    }
    else if ((form.reqSearch.value != "") && (form.searchProdNum.value != ""))
    {
      bContinue = 0;
      alert("Please enter only one of either search criteria - search word(s) OR a product number.");
    }
    else
    {
      for (i=0; i<form.elements.length; i++)
      {
          iIndex = form.elements[i].value.indexOf("%");
          if (iIndex > 0)
             form.elements[i].value = form.elements[i].value.substr(0,iIndex) + " " + form.elements[i].value.substr(iIndex+1,form.elements[i].value.length);
      }
      if (bContinue)
      {
          sAction= "http://www.chippendalerestorations.com.au/cgi-bin/SiteManager/search.pl?pageNumber=1&searchWords=" + form.reqSearch.value + "&productNumber=" + form.searchProdNum.value;
          form.action = sAction;
          form.submit();
          
      }
    }
    return bContinue;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

// This function checks to see if a string contains only alphanumeric characters or the space character. If so, it returns
// true. If not, it returns false.
function isAlphaNumOrSpace(str) 
{
  var sAlphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789";
  var bIsAlphaNum = true;

  for (i=0; i<str.length; i++) 
  {
    if (sAlphaNum.indexOf(str.charAt(i)) == -1) 
    {
      bIsAlphaNum = false;
      break;
    }
  }
  return bIsAlphaNum;
}

// This function removes any trailing or preceeding spaces from a string.
function trim(str)
{
  var iLength = str.length;
  
  if (iLength > 0)
  {
    // Remove any preceeding spaces
    while (str.charAt(0) == ' ')
      str = str.substring(1);

    // Remove trailing spaces
    while (str.charAt(str.length - 1) == ' ')
      str = str.substring(0, str.length - 1);
  }
  return str;
}

// This function determines whether or not all the mandatory fields for the Add New Product page
// have been entered. If not, it returns false.
function checkMandatoryFieldsAddProduct(form, iElementNum)
{
    var bError = 0;
    if (form.elements[iElementNum].value == "")
    {
        if (form.elements[iElementNum].name == "displayName") 
        {
  	    bError = 1;
	    alert("Please enter the display name of the product. The display is the name that will be displayed below the thumbnail picture.");
        }
        else if (form.elements[iElementNum].name == "price")
        {
	    bError = 1;
	    alert("Please enter the price of the product.");
        }
        else if (form.elements[iElementNum].name == "description")
        {
          bError = 1;
	    alert("Please enter the description of the product.");
	  }
        else if (form.elements[iElementNum].name == "FILE1")
        {
	    bError = 1;
	    alert("Please select a small (thumbnail) picture of the product.");
	  }
        else if (form.elements[iElementNum].name == "FILE2")
        {
	    bError = 1;
	    alert("Please select a larger picture of the product.");
	  }
   }
   return bError;
}

// This function determines whether or not all the mandatory fields for the Add New Instructional Video page
// have been entered. If not, it returns false.
function checkMandatoryFieldsAddInstructionalVideo(form, iElementNum)
{
    var bError = 0;
    if (form.elements[iElementNum].value == "")
    {
        if (form.elements[iElementNum].name == "linkText") 
        {
  	    bError = 1;
	    alert("Please enter the link text for the instructional video.");
        }
        else if (form.elements[iElementNum].name == "link")
        {
	    bError = 1;
	    alert("Please enter the link to the instructional video.");
        }

   }
   return bError;
}

// This function determines whether or not all the mandatory fields for the Add New FAQ page
// have been entered. If not, it returns false.
function checkMandatoryFieldsAddFaq(form)
{
    var bError = 0;
 
    if (form.elements["question"].value == "")
    {
        bError = 1;
        alert("Please enter the question.");

    }
    if ((form.elements["answer"].value == "") && (form.elements["pdfLink"].value == ""))
    {
	bError = 1;
	alert("Please enter the EITHER the link to the PDF or the answer text.");
    }
    if ((form.elements["answer"].value != "") && (form.elements["pdfLink"].value != ""))
    {
        bError = 1;
	alert("Please enter the EITHER the link to the PDF or the answer text.");
    }

    return bError;
}

// This function determines whether or not all the mandatory fields for the Update Product page
// have been entered. If not, it returns false.
function checkMandatoryFieldsUpdProduct(form, iElementNum)
{
    var bError = 0;
    if (form.elements[iElementNum].value == "")
    {
        if (form.elements[iElementNum].name == "displayName") 
        {
  	    bError = 1;
	    alert("Please enter the display name of the product. The display is the name that will be displayed below the thumbnail picture.");
        }
        else if (form.elements[iElementNum].name == "price")
        {
	    bError = 1;
	    alert("Please enter the price of the product.");
        }
        else if (form.elements[iElementNum].name == "description")
        {
          bError = 1;
	    alert("Please enter the description of the product.");
	  }
        
   }
   return bError;
}

// This function determines whether or not the fields entered on the Add New Product and Update Product pages have valid
// data. If not, it returns false.
function validateFieldsAddUpdProduct(form, iElementNum)
{
   var bError = 0;
   
   // The two picture files selected must have the prefix of either .jpg or .gif (case insensitive)
   if (((form.elements[iElementNum].name == "FILE1") || (form.elements[iElementNum].name == "FILE2")) &&
       (form.elements[iElementNum].value != ""))
   {
     var iLength = form.elements[iElementNum].value.length;

     if ((form.elements[iElementNum].value.toLowerCase().indexOf(".gif") < 0) && (form.elements[iElementNum].value.toLowerCase().indexOf(".jpg") < 0) ||
         ((form.elements[iElementNum].value.toLowerCase().indexOf(".gif") < (iLength-4)) && (form.elements[iElementNum].value.toLowerCase().indexOf(".jpg") < (iLength-4))))
     {
       bError = 1;
       alert("Please ensure that the pictures selected are either in GIF or JPG format.");
     }
   }
   // Check that the product display name contains only alphanumeric characters or spaces.
   else if (form.elements[iElementNum].name == "displayName")
   {
     if (!isAlphaNumOrSpace(trim(form.elements[iElementNum].value)))
     {
       bError = 1;
       alert("Please ensure that the display name for this product consists only of letters, numbers or spaces.");
     }
     
   }

   return bError;
}

// This function determines whether or not the fields entered on the Add New Instructional Video page have valid
// data. If not, it returns false.
function validateFieldsAddInstructionalVideo(form, iElementNum)
{
   var bError = 0;
   
  
   // Check that the link begins with http:
   if (form.elements[iElementNum].name == "link")
   {
     if (form.elements[iElementNum].value.toLowerCase().indexOf("http:") != 0)
     {
       bError = 1;
       alert("Please ensure that the link to the instructional video starts with http:");
     }
     
   }

   return bError;
}

function performAddNewProduct(form)
{
    var bError = 0;
    var bSectionChecked = 0;
    var sSection = "";
    var sDescription = form.description.value;

    try
    {
      for (var i=0; i<form.elements.length; i++)
      {
         bError = checkMandatoryFieldsAddProduct(form, i);
        
         if (!bError) 
           bError = validateFieldsAddUpdProduct(form, i);

        // If this form field is one of the section radio buttons, see if it is checked. At least one of the
        // radio buttons must be selected.  
        if ((form.elements[i].name == "section") && (form.elements[i].checked))
        {
          bSectionChecked = 1;
          sSection = form.elements[i].value;
        }

        if (bError)
          break;
      }
      if (!bError && !bSectionChecked)
      {
         bError = 1;
         alert("Please select the section that you would like the product to appear in.");
      }

      if(!bError )
      {
          form.action = "http://www.chippendalerestorations.com.au/cgi-bin/SiteManager/addNewProduct.pl";
          form.submit();
      }
      else if (bSectionChecked)
      {
          form.elements[i].focus();
      }
    }
    catch(e)
    {
      alert("Please check that the pictures you have selected are correct and try again.\n Error:" + e.message); 
    }
    return !bError;
}

function performUpdateProduct(form)
{
    var bError = 0;
    var sSection = "";
    var sDescription = form.description.value;

    try
    {
      // Validate the update product page. The same validation rules as for the Add New Product page apply.
      for (var i=0; i<form.elements.length; i++)
      {
         bError = checkMandatoryFieldsUpdProduct(form, i);
        
         if (!bError) 
           bError = validateFieldsAddUpdProduct(form, i);

         if (bError)
          break;
      }
      if(!bError )
          form.submit();
      else 
          form.elements[i].focus();
    }
    catch(e)
    {
      alert("Please check that the pictures you have selected are correct and try again.\n Error:" + e.message); 
    }
    return !bError;
}

// This function checks that a section has been selected and if so, then invokes the listProducts script.
function showProducts(form)
{
    var bError = 0;
    var bSectionChecked = 0;
    var sSection = "";
    var sActionType = "";
    var S_UPDATE_ACTION = "update";
  
    sActionType = form.actionType.value;
    
    for (var i=0; i<form.elements.length; i++)
    {
      // If this form field is one of the section radio buttons, see if it is checked. At least one of the
      // radio buttons must be selected.  
      if ((form.elements[i].name == "section") && (form.elements[i].checked))
      {
        bSectionChecked = 1;
        sSection = form.elements[i].value;
      }
     }
    if (!bSectionChecked)
    {
       bError = 1;
       if (sActionType == S_UPDATE_ACTION)
         alert("Please select the section from which you would like to update a product.");
       else
         alert("Please select the section that you would like to remove a product from.");
    }
    if(!bError )
    {
        form.action = "http://www.chippendalerestorations.com.au/cgi-bin/SiteManager/listProducts.pl?actionType="+sActionType+"&section="+sSection;
        form.submit();
    }
      
    return !bError;
}

// This function checks that the user has selected a product for deletion. If not, an error message is displayed. If a
// product has been selected, the user is issued with a warning asking if they'd like to proceed.
function deleteProduct(form)
{
    var bError = 0;
    var bProdSelected = 0;
    var sProd = "";
    var bContinue = 0;
    var sAction = "";

    for (var i=0; i<form.elements.length; i++)
    {
      // If this form field is one of the product radio buttons, see if it is checked. At least one of the
      // radio buttons must be selected.  
      if ((form.elements[i].name == "product") && (form.elements[i].checked))
      {
        bProdSelected = 1;
        sProd = form.elements[i].value;
      }
     }
    if (!bProdSelected)
    {
       bError = 1;
       alert("Please select the product that you would like to delete.");
    }
    if(!bError )
    {
      bContinue = confirm("Are you sure you would like to delete this product?");
      if (!bContinue)
        bError = 1;
      else
        form.submit();

    }
      
    return !bError;
}

// This function checks that the user has selected an instructional video for deletion. If not, an error message is displayed. If a
// video has been selected, the user is issued with a warning asking if they'd like to proceed.
function deleteInstructionalVideo(form)
{
    var bError = 0;
    var bSelected = 0;
    var sVid = "";
    var bContinue = 0;
    var sAction = "";

    for (var i=0; i<form.elements.length; i++)
    {
      // If this form field is one of the video radio buttons, see if it is checked. At least one of the
      // radio buttons must be selected.  
      if ((form.elements[i].name == "video") && (form.elements[i].checked))
      {
        bSelected = 1;
        sVid = form.elements[i].value;
      }
     }
    if (!bSelected)
    {
       bError = 1;
       alert("Please select the instructional video that you would like to delete.");
    }
    if(!bError )
    {
      bContinue = confirm("Are you sure you would like to delete this instructional video?");
      if (!bContinue)
        bError = 1;
      else
        form.submit();

    }
      
    return !bError;
}

// This function checks that the user has selected an FAQ for deletion. If not, an error message is displayed. If a
// FAQ has been selected, the user is issued with a warning asking if they'd like to proceed.
function deleteFaq(form)
{
    var bError = 0;
    var bSelected = 0;
    var sFaq = "";
    var bContinue = 0;
    var sAction = "";

    for (var i=0; i<form.elements.length; i++)
    {
      // If this form field is one of the FAQ radio buttons, see if it is checked. At least one of the
      // radio buttons must be selected.  
      if ((form.elements[i].name == "faq") && (form.elements[i].checked))
      {
        bSelected = 1;
        sFaq = form.elements[i].value;
      }
     }
    if (!bSelected)
    {
       bError = 1;
       alert("Please select the FAQ that you would like to delete.");
    }
    if(!bError )
    {
      bContinue = confirm("Are you sure you would like to delete this FAQ?");
      if (!bContinue)
        bError = 1;
      else
        form.submit();

    }
      
    return !bError;
}

// This function checks that the user has selected a product for updating. If not, an error message is displayed. 
function updateProduct(form)
{
    var bError = 0;
    var bProdSelected = 0;
    var sProd = "";
    var bContinue = 0;
    var sAction = "";
    var sSection = "";

    for (var i=0; i<form.elements.length; i++)
    {
      // If this form field is one of the product radio buttons, see if it is checked. At least one of the
      // radio buttons must be selected.  
      if ((form.elements[i].name == "product") && (form.elements[i].checked))
      {
        bProdSelected = 1;
        sProd = form.elements[i].value;
      }
      if (form.elements[i].name == "actionType") 
      {
        sAction = form.elements[i].value;
      }
      if (form.elements[i].name == "section") 
      {
        sSection = form.elements[i].value;
     }

    }
    if (!bProdSelected)
    {
       bError = 1;
       alert("Please select the product that you would like to update.");
    }
    if (!bError)
    {
       form.action = "http://www.chippendalerestorations.com.au/cgi-bin/SiteManager/updateProduct.pl?product="+sProd+"&actionType="+sAction+"&section="+sSection;
       form.submit();
    }

      
    return !bError;
}

function performAddNewInstructionalVideo(form)
{
    var bError = 0;
    try
    {
      for (var i=0; i<form.elements.length; i++)
      {
         bError = checkMandatoryFieldsAddInstructionalVideo(form, i);
        
         if (!bError) 
           bError = validateFieldsAddInstructionalVideo(form, i);

        if (bError)
          break;
      }


      if(!bError )
      {
          form.action = "http://www.chippendalerestorations.com.au/cgi-bin/SiteManager/addNewInstructionalVideo.pl";
          form.submit();
      }

    }
    catch(e)
    {
      alert("An error occurred whilst adding the instructional video.\n Error:" + e.message); 
    }
    return !bError;
}

function performAddNewFaq(form)
{
    var bError = 0;
    try
    {
      bError = checkMandatoryFieldsAddFaq(form);


      if(!bError )
      {
          form.action = "http://www.chippendalerestorations.com.au/cgi-bin/SiteManager/addNewFaq.pl";
          form.submit();
      }

    }
    catch(e)
    {
      alert("An error occurred whilst adding the faq.\n Error:" + e.message); 
    }
    return !bError;
}
