// AUTHOR:  Cedric Norman
// DATE:    16th November 2010

// Move cursor to first feild in the form
function setup()
	{
	document.mailtoForm.uname.focus();
	}

// Create a MAILTO Form
function createMailto(sourceForm) 
    {
    var to 		= "manual@bsa-c15.org.uk"; 
    var subject 	= "BSA C15 CD Manual Order";
    var uname		= sourceForm.uname.value; 
    var uaddress1	= sourceForm.uaddress1.value;
    var uaddress2	= sourceForm.uaddress2.value;
    var uaddress3	= sourceForm.uaddress3.value;    
    var utown		= sourceForm.utown.value;
    var ucounty		= sourceForm.ucounty.value;
    var upostcode	= sourceForm.upostcode.value;
    var ucountry	= sourceForm.ucountry.value;
    var uemail		= sourceForm.uemail.value;
    var payment		= sourceForm.payment.value;
    var confirm		= sourceForm.confirm.value;
    var invoicetext     = "blank"
    var cost		= "cost"
    var address		= "address"
    
    // If the Value is set include it in the Email Body
    if (uname != "") 
    {
		uname = toTitleCase(uname);
		uname = uname;
    }
    else 
    {
        alert("Please enter 'Your Name'");
        sourceForm.uname.focus();
        return(1);
    }
    if (uaddress1 != "") 
    {
		uaddress1 = toTitleCase(uaddress1);
		uaddress1 = uaddress1;
                address = uaddress1;
    }
    else 
    {
        alert("Please enter 'Your Mail Address'");
        sourceForm.uaddress1.focus();
        return(1);
    }
    if (uaddress2 != "") 
    {
		uaddress2 = toTitleCase(uaddress2);
		uaddress2 = uaddress2;
                address = address + "\n" + uaddress2;
    }
    if (uaddress3 != "") 
    {
		uaddress3 = toTitleCase(uaddress3);
		uaddress3 = uaddress3;
                address = address + "\n" + uaddress3;
    }

    if (utown != "") 
    {
		utown = toTitleCase(utown);
		utown = utown;
    }
    else 
    {
        alert("Please enter 'The Postal Town'");
        sourceForm.utown.focus();
        return(1);
    }
    if (ucounty != "") 
    {
		ucounty = toTitleCase(ucounty);
		ucounty = ucounty;
    }
    else 
    {
        alert("Please enter 'Your the County Details'");
        sourceForm.ucounty.focus();
        return(1);
    }
    if (upostcode != "") 
    {
		upostcode = upostcode.toUpperCase();
		upostcode = upostcode;
    }
    else 
    {
        alert("Please enter 'Your the Post Code Details (ZIP)'");
        sourceForm.upostcode.focus();
        return(1);
    }
    {
	ucountry = ucountry.toUpperCase();
    }


    if (ucountry == "UK")
    {
        cost = "£14-50";
    }
    else if (ucountry != "UK")
    {
	cost = "£18-50";
    }

    if (ucountry == "")
    {
        alert("Please enter 'Your the Country Details'");
        sourceForm.ucountry.focus();
        return(1);
    }
    else if (ucountry != "UK" && payment == "Cheque")
    {
        alert("I am sorry it is not possible to purchase the CD by cheque from outside the UK due to the cost of bank exchange charges! \nIf you want to proceed please select PayPal or Credit Card.");
        sourceForm.payment.focus();
        return(1);
    }

    if (payment != "Cheque")
    {   
    invoicetext = "Please invoice me for " + cost + " through PayPal for this purchase.";
    }
    else if (payment == "Cheque")
    {
    invoicetext = "Please email me the postal details so that I may send a cheque in payment for the CD-Manual.";
    }

    if (uemail != "") 
    {
    }
    else 
    {
        alert("Please enter 'Your Email address'");
        sourceForm.uemail.focus();
        return(1);
    }

    if (confirm != "Yes") 
    {
        alert("Please CONFIRM you wish to place an order");
        sourceForm.confirm.focus();
        return(1);
    }

    
    {
    // Create Body Details
    var body = 
	"Details: " +
        "\n" + uname + 
        "\n" + address + 
	"\n" + utown +
        "\n" + ucounty +
        "\n" + upostcode +
        "\n" + ucountry +
        "\nEmail: " + uemail +
        "\n\nPayment type: " + payment +
        "\n\nThe Order has been 'CONFIRMED'" +
        "\n\n" + invoicetext +
        "\nI understand that the CD containing the manuals will not be despatched until after payment has been received."
                 
        
    // BUILD MAIL MESSAGE COMPONENTS 
        var doc = "mailto:" + to + 
        "?subject=" + escape(subject) + 
        "&body=" + escape(body); 

    // POP UP EMAIL MESSAGE WINDOW
    window.location = doc; 
    }


    {   alert("I would like to thank you for your purchase. \nI hope you have enjoyed your visit to this website.");
    }
    
function toSentenceCase(line)
	{
    // Convert to Sentence Case
        var uflag = "y"
        var temp = ""
        for (y = 0; y < line.length ; y++)
          {
          char = (uflag == "y") ? line.charAt(y).toUpperCase(): line.charAt(y).toLowerCase();
          uflag = (line.charAt(y) == " " && line.charAt(y-1) == "."  ) ? "y" : "n" ;
          temp += char;
          }
        return temp;
	}
	
function toTitleCase(line)
	{
    // Convert to Title Case
        var uflag = "y"
        var temp = ""
        for (y = 0; y < line.length ; y++)
          {
          char = (uflag == "y") ? line.charAt(y).toUpperCase(): line.charAt(y).toLowerCase();
          uflag = (line.charAt(y) == " ") ? "y" : "n" ;
          temp += char;
          }
        return temp;
	}
}
