// AUTHOR:  Cedric Norman
// DATE:    23rd November 2003

// 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 other		= sourceForm.other.value;
    var payment		= sourceForm.payment.value;
    var confirm		= sourceForm.confirm.value;
    

    // If the Value is set include it in the Email Body
    if (uname != "") 
    {
		uname = toTitleCase(uname);
    }
    else 
    {
        alert("Please enter 'Your Name'");
        sourceForm.uname.focus();
        return(1);
    }
    if (uaddress1 != "") 
    {
		uaddress1 = toTitleCase(uaddress1);
		uaddress1 = uaddress1 + "\n";

    }
    else 
    {
        alert("Please enter 'Your Mail Address'");
        sourceForm.uaddress1.focus();
        return(1);
    }
    if (uaddress2 != "") 
    {
		uaddress2 = toTitleCase(uaddress2);
		uaddress2 = uaddress2 + "\n";
    }
    if (uaddress3 != "") 
    {
		uaddress3 = toTitleCase(uaddress3);
		uaddress3 = uaddress3 + "\n";
    }
    if (utown != "") 
    {
		utown = toTitleCase(utown);
		utown = utown + "\n";
    }
    else 
    {
        alert("Please enter 'The Postal Town'");
        sourceForm.utown.focus();
        return(1);
    }
    if (ucounty != "") 
    {
		ucounty = toTitleCase(ucounty);
		ucounty = ucounty + "\n";
    }
    else 
    {
        alert("Please enter 'Your the County Details'");
        sourceForm.ucounty.focus();
        return(1);
    }
    if (upostcode != "") 
    {
		upostcode = upostcode.toUpperCase();
		upostcode = upostcode + "\n";
    }
    else 
    {
        alert("Please enter 'Your the Post Code / ZIP Details'");
        sourceForm.upostcode.focus();
        return(1);
    }
    if (ucountry != "") 
    {
		ucountry = ucountry.toUpperCase();
		ucountry = ucountry + "\n";
    }
    else 
    {
        alert("Please enter 'Your the Country Details'");
        sourceForm.ucountry.focus();
        return(1);
    }
    if (uemail != "") {}
    else 
    {
        alert("Please enter 'Your Email address'");
        sourceForm.uemail.focus();
        return(1);
    }
    if (other != "") 
    {
	// other = toSentenceCase(other);
    }
    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" + uaddress1 + uaddress2 + uaddress3 +
	utown + ucounty + upostcode + ucountry + "    \nEmail:- " + uemail +
        "\n\nPayment type:- " + payment +
	"\n\nOther Information :- " + other + 
	"\n\n" + "I wish to buy the C15 Service Manual" +
        "\n\nThe Order has been 'CONFIRMED', please process!"
                 
        
    // BUILD MAIL MESSAGE COMPONENTS 
        var doc = "mailto:" + to + 
        "?subject=" + escape(subject) + 
        "&body=" + escape(body); 

    // POP UP EMAIL MESSAGE WINDOW
    window.location = doc; 
    }
    
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;
	}
}