function validDate(m,d,y) {
    if (d > 28 && m == 2)
    {
        if (d > 29)
            return false;
        if (y % 4 != 0)
            return false;
    }
            
    if (d > 30 && (m == 2 || m == 4 || m == 6 || m == 9 || m == 11))
        return false;
    
    return true;
}

function isblank(s) {
    for (var i = 0; i < s.length; i++)
    {
       var c = s.charAt(i);
           if ((c != ' ') && (c != '\n') && (c != '\t'))
          return false;
    }
    return true;
}

function validateCustomWording() {
    var errors = "";
    var customWording = false;
    for (var i = 0; i < document.forms[0].length; i++)
    {
           var e = document.forms[0].elements[i];
         if (e.type == "checkbox" && e.value.indexOf("Custom Wording") != -1 && e.checked)
               customWording = true;
    }
    if (customWording && document.forms[0].CustomWording.value.length == 0) 
         errors += " - Please provide the Custom Wording for the products you have selected.\n\n";
    return errors;    
}

function validateForm() {
    var errors = "";
            
    if (anyItemsChecked('Item') && document.forms[0].Office.options[0].selected == true)
         errors += " - Please select your Windermere office from the list.\n\n";    
        
    if (typeof(document.forms[0].SpecialInstructions) != "undefined")         
        document.forms[0].SpecialInstructions.optional = true;
    if (typeof(document.forms[0].Manager) != "undefined")         
        document.forms[0].Manager.optional = true;
    if (typeof(document.forms[0].Agent) != "undefined")         
        document.forms[0].Agent.optional = true;
    if (typeof(document.forms[0].OtherDesignation) != "undefined")         
        document.forms[0].OtherDesignation.optional = true;
    if (typeof(document.forms[0].For1) != "undefined")         
        document.forms[0].For1.optional = true;
    if (typeof(document.forms[0].For2) != "undefined")         
        document.forms[0].For2.optional = true;
    if (typeof(document.forms[0].Phone1) != "undefined")         
        document.forms[0].Phone1.optional = true;
    if (typeof(document.forms[0].AlternateLabel1) != "undefined")         
        document.forms[0].AlternateLabel1.optional = true;
    if (typeof(document.forms[0].Phone2) != "undefined")         
        document.forms[0].Phone2.optional = true;
    if (typeof(document.forms[0].AlternateLabel2) != "undefined")         
        document.forms[0].AlternateLabel2.optional = true;
    if (typeof(document.forms[0].Phone3) != "undefined")         
        document.forms[0].Phone3.optional = true;
    if (typeof(document.forms[0].AlternateLabel3) != "undefined")         
        document.forms[0].AlternateLabel3.optional = true;
    if (typeof(document.forms[0].Phone4) != "undefined")         
        document.forms[0].Phone4.optional = true;
    if (typeof(document.forms[0].AlternateLabel4) != "undefined")         
        document.forms[0].AlternateLabel4.optional = true;
    if (typeof(document.forms[0].Phone5) != "undefined")         
        document.forms[0].Phone5.optional = true;
      if (typeof(document.forms[0].AlternateLabel5) != "undefined")         
        document.forms[0].AlternateLabel5.optional = true;
    if (typeof(document.forms[0].Phone6) != "undefined")         
        document.forms[0].Phone6.optional = true;
    if (typeof(document.forms[0].AlternateLabel6) != "undefined")         
        document.forms[0].AlternateLabel6.optional = true;
       if (typeof(document.forms[0].CustomWording) != "undefined")         
        document.forms[0].CustomWording.optional = true;

    // Always make Agent Name a required field (Per Geary)
    if (typeof(document.forms[0].Agent) != "undefined")         
        document.forms[0].Agent.optional = false;        
        
    // If personalization is required, but no repeat button is available,
    // a hidden field will be defined.         
       if (typeof(document.forms[0].RequiresPersonalInfo) != "undefined")
            errors += validatePersonalizationFields();
            
    // If the user says they want to create a new business card, they MUST 
    // provide a certain number of fields.  Only applies to business card pages....
       if (typeof(document.forms[0].Repeat) != "undefined")
       {    
        // If they've checked Revised Order, make sure they
        // indicate what type of revision they want..
        if (document.forms[0].Repeat[1].checked)
            if (document.forms[0].Revisions != "undefined" &&
                !document.forms[0].Revisions[0].checked &&
                !document.forms[0].Revisions[1].checked &&
                !document.forms[0].Revisions[2].checked)
            errors += " - You must indicate whether you wish to revise text or photo or both.\n\n";
        
        if (document.forms[0].Repeat[0].checked ||       // New Order
            (document.forms[0].Repeat[1].checked &&      // Revised & Text Revision requested
             document.forms[0].Revisions != "undefined" &&
            (document.forms[0].Revisions[0].checked ||
             document.forms[0].Revisions[1].checked)) &&
             typeof(document.forms[0].RequiresPersonalInfo) == "undefined")   // Don't repeat earlier check
            {
                errors += validatePersonalizationFields();
            }
    }
    
    // If the selection in any of the Title dropdown says "use Alt Label ->",
    // then AlternateLabel# is required.
    var alternateLabelErrors = "";
    var altErrorText = " - Please provide Alternate Labels where you've requested they be used.\n\n";            
    if (typeof(document.forms[0].Label1) != "undefined")         
        if (document.forms[0].Label1.options[document.forms[0].Label1.selectedIndex].text == "use Alt Label ->" && 
            document.forms[0].AlternateLabel1.length == 0)
            alternateLabelErrors += altErrorText;            
    if (alternateLabelErrors.length == 0 && typeof(document.forms[0].Label2) != "undefined")         
        if (document.forms[0].Label2.options[document.forms[0].Label2.selectedIndex].text == "use Alt Label ->" && 
            document.forms[0].AlternateLabel2.length == 0)
                alternateLabelErrors += altErrorText;                                
    if (alternateLabelErrors.length == 0 && typeof(document.forms[0].Label3) != "undefined")         
        if (document.forms[0].Label3.options[document.forms[0].Label3.selectedIndex].text == "use Alt Label ->" && 
            document.forms[0].AlternateLabel3.length == 0)
                alternateLabelErrors += altErrorText;        
    if (alternateLabelErrors.length == 0 && typeof(document.forms[0].Label4) != "undefined")         
        if (document.forms[0].Label4.options[document.forms[0].Label4.selectedIndex].text == "use Alt Label ->" && 
            document.forms[0].AlternateLabel4.length == 0)
                alternateLabelErrors += altErrorText;        
    if (alternateLabelErrors.length == 0 && typeof(document.forms[0].Label5) != "undefined")         
        if (document.forms[0].Label5.options[document.forms[0].Label5.selectedIndex].text == "use Alt Label ->" && 
            document.forms[0].AlternateLabel5.length == 0)
                alternateLabelErrors += altErrorText;            
    if (alternateLabelErrors.length == 0 && typeof(document.forms[0].Label6) != "undefined")         
        if (document.forms[0].Label6.options[document.forms[0].Label6.selectedIndex].text == "use Alt Label ->" && 
            document.forms[0].AlternateLabel6.length == 0)
                alternateLabelErrors += altErrorText;            

    errors += alternateLabelErrors;
    
    if (!anyItemsChecked('Item'))
        return true;
    else
    {        
         if (checkOrderForExactRepeat())
        {
            if (verify(errors))
            {
                return true;
            }
            else
                return false;
       }   
        else
            return false;
    }
}

function alertIfExactRepeatOrRevised()
{
       if (typeof(document.forms[0].Repeat) != "undefined")
       {

           if (document.forms[0].Repeat[2].checked)
           {
                alert("Please remember to email or fax us a copy of the original that you would like us to replicate for you!");
           }
       }
}
                    
function checkOrderForExactRepeat()
{
    // If they check Exact Repeat, look to see if there are other things selected
       askedForRepeatWithChanges = false;
       if (typeof(document.forms[0].Repeat) != "undefined")
       {
           if (document.forms[0].Repeat[2].checked)
           {
               
               // Did they ask for photo work to be done?
               if (typeof(document.forms[0].Photo) != "undefined" &&
                   document.forms[0].Photo[1].checked)
                   {askedForRepeatWithChanges = true;}
               
            if (typeof(document.forms[0].OtherDesignation) != "undefined" &&
                document.forms[0].OtherDesignation.value.length > 0)
                   {askedForRepeatWithChanges = true;}
                                                  
            for(i = 0; i < document.forms[0].elements.length; i++)
            {                    
                if (document.forms[0].elements[i].name.indexOf("Designations") > -1 &&
                       (document.forms[0].elements[i].checked ||
                        document.forms[0].elements[i].checked ||
                        document.forms[0].elements[i].checked ||
                        document.forms[0].elements[i].checked ||
                        document.forms[0].elements[i].checked))
                       {askedForRepeatWithChanges = true;}
               }
                   
            if (typeof(document.forms[0].Title1) != "undefined" &&
                !document.forms[0].Title1.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].For1) != "undefined" &&
                document.forms[0].For1.value.length > 0)
                   {askedForRepeatWithChanges = true;}
        
            if (typeof(document.forms[0].Title2) != "undefined" &&
                !document.forms[0].Title2.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].For2) != "undefined" &&
                document.forms[0].For2.value.length > 0)
                   {askedForRepeatWithChanges = true;}
        
            if (typeof(document.forms[0].Label1) != "undefined" &&         
                !document.forms[0].Label1.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Phone1) != "undefined" &&
                document.forms[0].Phone1.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].AlternateLabel1) != "undefined" &&
                document.forms[0].AlternateLabel1.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Label2) != "undefined" &&         
                !document.forms[0].Label2.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Phone2) != "undefined" &&
                document.forms[0].Phone2.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].AlternateLabel2) != "undefined" &&
                document.forms[0].AlternateLabel2.value.length > 0)
                   {askedForRepeatWithChanges = true;}

            if (typeof(document.forms[0].Label3) != "undefined" &&         
                !document.forms[0].Label3.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Phone3) != "undefined" &&
                document.forms[0].Phone3.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].AlternateLabel3) != "undefined" &&
                document.forms[0].AlternateLabel3.value.length > 0)
                   {askedForRepeatWithChanges = true;}

            if (typeof(document.forms[0].Label4) != "undefined" &&         
                !document.forms[0].Label4.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Phone4) != "undefined" &&
                document.forms[0].Phone4.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].AlternateLabel4) != "undefined" &&
                document.forms[0].AlternateLabel4.value.length > 0)
                   {askedForRepeatWithChanges = true;}

            if (typeof(document.forms[0].Label5) != "undefined" &&      
                !document.forms[0].Label5.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Phone5) != "undefined" &&
                document.forms[0].Phone5.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].AlternateLabel5) != "undefined" &&
                document.forms[0].AlternateLabel5.value.length > 0)
                   {askedForRepeatWithChanges = true;}

            if (typeof(document.forms[0].Label6) != "undefined" &&         
                !document.forms[0].Label6.options[0].selected)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Phone6) != "undefined" &&
                document.forms[0].Phone6.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].AlternateLabel6) != "undefined" &&
                document.forms[0].AlternateLabel6.value.length > 0)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Logo1) != "undefined" &&      
                !document.forms[0].Logo1[8].checked)
                   {askedForRepeatWithChanges = true;}

               if (typeof(document.forms[0].Logo2) != "undefined" && 
                !document.forms[0].Logo2[8].checked)
                   {askedForRepeatWithChanges = true;}
           }
       }
    
       if (askedForRepeatWithChanges)
    {
        answeredOk = confirm("You've requested an Exact Repeat Order, but then requested changes to the order.\r\n\r\nPlease press Ok to continue with Exact Repeat Order pricing - you must still email or fax us the card you want us to replicate.\r\n\r\nSelect Cancel to continue with the order - you must specify whether changes to text or photos or both are to be included in the order.")
        if (answeredOk)
        {
            unsetEverythingForExactRepeat();
            return true; 
        }
        else  {           
            // Change the order type to Revised and allow edits to occur upon return
            document.forms[0].Repeat[1].checked = true;
            document.forms[0].Repeat[1].focus();
            return false;
        }
    }    
       else
           return true;                                                                        
}
    
function unsetEverythingForExactRepeat()
{
    if (typeof(document.forms[0].Photo) != "undefined")
       {document.forms[0].Photo[1].checked = false;}
   
    if (typeof(document.forms[0].OtherDesignation) != "undefined")
        document.forms[0].OtherDesignation.value = "";
                                          
    for(i = 0; i < document.forms[0].elements.length; i++)
    {                    
        if (document.forms[0].elements[i].name.indexOf("Designations") > -1)
            document.forms[0].elements[i].checked = false
    }
       
if (typeof(document.forms[0].Title1) != "undefined")
    document.forms[0].Title1.options[0].selected = true;

   if (typeof(document.forms[0].For1) != "undefined" )
    document.forms[0].For1.value = ""; 

if (typeof(document.forms[0].Title2) != "undefined" )
    document.forms[0].Title2.options[0].selected = true;

   if (typeof(document.forms[0].For2) != "undefined")
    document.forms[0].For2.value = "";

if (typeof(document.forms[0].Label1) != "undefined")         
    document.forms[0].Label1.options[0].selected = true;

   if (typeof(document.forms[0].Phone1) != "undefined")
    document.forms[0].Phone1.value = "";
       
   if (typeof(document.forms[0].AlternateLabel1) != "undefined")
    document.forms[0].AlternateLabel1.value = "";
       
   if (typeof(document.forms[0].Label2) != "undefined")    
    document.forms[0].Label2.options[0].selected = true;
       
   if (typeof(document.forms[0].Phone2) != "undefined")
    document.forms[0].Phone2.value = "";
       
   if (typeof(document.forms[0].AlternateLabel2) != "undefined")
    document.forms[0].AlternateLabel2.value = "";
       
if (typeof(document.forms[0].Label3) != "undefined")         
    document.forms[0].Label3.options[0].selected = true;
       
   if (typeof(document.forms[0].Phone3) != "undefined")
    document.forms[0].Phone3.value = "";
       
   if (typeof(document.forms[0].AlternateLabel3) != "undefined")
    document.forms[0].AlternateLabel3.value = "";
       

if (typeof(document.forms[0].Label4) != "undefined")         
    document.forms[0].Label4.options[0].selected = true;
       

   if (typeof(document.forms[0].Phone4) != "undefined")
    document.forms[0].Phone4.value = "";
       

   if (typeof(document.forms[0].AlternateLabel4) != "undefined")
    document.forms[0].AlternateLabel4.value = "";
       

if (typeof(document.forms[0].Label5) != "undefined")      
    document.forms[0].Label5.options[0].selected = true;
       
   if (typeof(document.forms[0].Phone5) != "undefined")
    document.forms[0].Phone5.value = "";
       
   if (typeof(document.forms[0].AlternateLabel5) != "undefined")
    document.forms[0].AlternateLabel5.value = "";
       
if (typeof(document.forms[0].Label6) != "undefined")         
    document.forms[0].Label6.options[0].selected = true;
       

   if (typeof(document.forms[0].Phone6) != "undefined")
    document.forms[0].Phone6.value = "";

   if (typeof(document.forms[0].AlternateLabel6) != "undefined")
    document.forms[0].AlternateLabel6.value = "";
       

   if (typeof(document.forms[0].Logo1) != "undefined")      
    document.forms[0].Logo1[8].checked = true;
       

   if (typeof(document.forms[0].Logo2) != "undefined") 
    document.forms[0].Logo2[8].checked = true;
}
    
function validatePersonalizationFields()
{
    var errors = "";
    document.forms[0].Agent.optional = false;

    // Customize the message a bit.
    if (document.forms[0].Title1.options[0].selected)
        errors += " - Please select at least one title. [If no title is desired \n\tplease select \"No Title Required\" from the list]\n\n";
    //alert("isblank(e.value) = " + isblank(document.forms[0].Phone1.value));
       if (typeof(document.forms[0].Label1) != "undefined")
        if (document.forms[0].Label1.options[0].selected || (isblank(document.forms[0].Phone1.value) && !document.forms[0].Label1.options[1].selected && !document.forms[0].Label1.options[2].selected))
            errors += " - Please select at least one label and phone number.\n\n";
    return errors;
}
// This is the function that performs form verification.  It will be invoked from the
// onSubmit() event handler.  The handler should return whatever value this function
// returns.
function verify()
{
    var msg;
    var ridx;
    var empty_fields = "";
    var total_errors = 0;
    var total_emptys = 0;
    var formnum = 0;
    var errors = "";
    var str = "";
        
    // Currently, you can pass in an error message you want displayed AND/OR
    // a form number that you want to verify other than form[0]
    for (i = 0; i < arguments.length; i++) {
        str = arguments[i] + "";       // This is how you make SURE its a string value!!!!
        if (str.length == 1)             // Form number will always be a one-digit (hopefully) number
            formnum = arguments[i];                
        else                             // Errors will be longer than one character (or an empty string)
            errors += arguments[i];
    }

    // Loop through the elements of the form, looking for all
    // text and textarea elements that don't have an "optional" property
    // defined.  Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    var strName;
    for (var i = 0; i < document.forms[formnum].length; i++)
    {
       var e = document.forms[formnum].elements[i];
       if (((e.type == "text") || (e.type == "textarea") || e.type == "password"))
       {
        
          // Strip off the brackets, if the name has 'em...
            if (e.name.substr(e.name.length-2,2) == "[]")
              strName = e.name.substring(0,e.name.length-2);
          else 
              strName = e.name;
       
          // First check if the field is empty
          if (((e.value == null) || (e.value == "") || isblank(e.value)) && !e.optional)
          {
              empty_fields += "\n            " + strName;
              total_emptys += 1;
              total_errors += 1;
              continue;
            }
/*
          // Now check for fields that are supposed to be numeric
           if (e.value != "" && (e.numeric || (e.min != null) || (e.max != null))) 
          {
              var v = parseFloat(e.value);
              if (isNaN(v) ||
                 ((e.min != null) && (e.min != "") && (v < e.min)) ||
                 ((e.max != null) && (e.max != "") &&  (v > e.max)))
              {
                  errors += " - The field " + strName + " must be a number";
                  if (e.min != null)
                      errors += " that is greater than or equal to " + e.min;
                  if (e.max != null && e.min != null)
                      errors += " and less than or equal to " + e.max;
                    else if (e.max != null)
                      errors += " that is less than " + e.max;
                   errors += ".\n";
                   total_errors += 1;
               }
          }
*/
       }                   
    }
    
    // Now, if there were any errors, display the messages, and 
    // return false to prevent the form from being submitted.
    // Otherwise, return true.
    if (!empty_fields && !errors)
       return true;

    if (total_errors > 1)
    {    
       msg  = "_________________________________________________________\n\n";
       msg += "The form was not submitted because of the following errors. \n";
       msg += "Please correct these errors and re-submit.\n";
       msg += "_________________________________________________________\n\n";
    }
    else
    {
       msg  = "_________________________________________________________\n\n";
       msg += "The form was not submitted because of the following error. \n";
       msg += "Please correct this error and re-submit.\n";
       msg += "_________________________________________________________\n\n";
    }

    if (empty_fields) 
    {
       if (total_emptys > 1)
       {
           msg += " - The following required fields are empty:" 
                  + empty_fields + "\n";
       }
       else
       {
          msg += " - The following required field is empty:"
                   + empty_fields + "\n";
       }
       if (errors) 
          msg += "\n";
    }

    msg += errors;
    alert(msg);
    return false;

}

function setOffice(myOffice) {
    officeList = document.forms[0].Office;
    //alert(document.forms[0].Office.options.length);
    for (i = 0; i < officeList.options.length; i++){
       // if (i <= 5)
        //    alert(document.forms[0].Office.options[i].text);
        if (officeList.options[i].text == myOffice)
        {
          officeList.options[i].selected = true;
          break;
        }
    }
}

function anyItemsChecked(prefix) 
{
    var e, subElementName;

    // When a user selects a given type, unselect all the rest
    for (var i = 0; i < document.forms[0].length; i++)
    {
       e = document.forms[0].elements[i];
       theFirstPartOfName = e.name.substring(0, prefix.length);
       if (theFirstPartOfName == prefix && e.type == "checkbox") // right series of checkboxes....
       {
           if (e.checked == true)
                   return true;         // selected, return true...
        }
    }
    return false;
}

function uncheckRevisions()
{
    document.forms[0].Revisions[0].checked = false;
    document.forms[0].Revisions[1].checked = false;
    document.forms[0].Revisions[2].checked = false;
}

var newwindow;
function openUploadWindow() {

    windowprops = "height=400,width=600,location=no,"
    + "scrollbars=yes,menubars=no,toolbars=no,resizable=yes";

    newwindow = window.open("upload.php", "Popup", windowprops);
    if (window.focus) newwindow.focus();
    return false;

}

function openEmail() {

    parent.location = "mailto:photos@ptarmigan-press.com?subject=Incoming%20Photo";
    parent.focus();
}
