// JQuery Scripts
$(document).ready(function(){
	
    /**
	 * Image Rollover Functionality
	 */
	$(".rollover").mouseover(function(){
		imgsrc = $(this).attr("src");
		matches = imgsrc.match(/-ro/);
		// don't do the rollover if state is already ON
		if (!matches) {
			
			if(imgsrc.indexOf('.gif') > -1) {
				imgsrcON = imgsrc.replace(/.gif$/ig,"-ro.gif"); // strip off extension - Gif
			} else if(imgsrc.indexOf('.jpg') > -1) {
				imgsrcON = imgsrc.replace(/.jpg$/ig,"-ro.jpg"); // strip off extension - Jpg		
			}
			
			$(this).attr("src", imgsrcON);
		}
	});
	
	$(".rollover").mouseout(function() {
		$(this).attr("src", imgsrc);
	});
		
});


function Validator(theForm)
{

    if (theForm.contact_name.value == 0) {
        alert("Please enter something in the \"Name\" field.");
        theForm.contact_name.focus();
        return (false);
    }
    
    if (theForm.email.value == 0)  {
    alert("Please enter something in the \"Email\" field.");
    theForm.email.focus();
    return (false);
    }

    /////////////////////////////////////start of email check
    var emailStr = (document.theForm.email.value);

    var checkTLD=1;

    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

    var emailPat=/^(.+)@(.+)$/;

    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

    var validChars="\[^\\s" + specialChars + "\]";

    var quotedUser="(\"[^\"]*\")";

    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

    var atom=validChars + '+';

    var word="(" + atom + "|" + quotedUser + ")";

    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

    var matchArray=emailStr.match(emailPat);

    if (matchArray==null) {
        alert("Email email address seems incorrect (check @ and .'s)");
        
        var nogo="y";
        theForm.email.focus();
       
       return false;
    }

    var user=matchArray[1];
    var domain=matchArray[2];

    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            alert("Ths username contains invalid characters.");
            var nogo="y";
                theForm.email.focus();
            return false;
        }
    }
    
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            alert("Ths domain name contains invalid characters.");
            var nogo="y";
                theForm.email.focus();
            return false;
        }
    }
}


/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/


function CalculateTotal(frm) {
    var order_total = 0
    var custom = "?"

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i)
    {

        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name

        // Is it a "product" field?
        if (form_name.substring(0,4) == "PROD")
        {
            // Get order ID
            order_id = form_name.substring(5,8)

            // If so, extract the price from the name
            item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0)
            {
                if (order_id == "BET")
                {
                    frm.BET.value = item_quantity
                }
                if (order_id == "PAT")
                {
                    frm.PAT.value = item_quantity
                }
                if (order_id == "SPT")
                {
                    frm.SPT.value = item_quantity
                }
                if (order_id == "ADR")
                {
                    frm.ADR.value = item_quantity
                }
                if (order_id == "ADO")
                {
                    frm.ADO.value = item_quantity
                }
                if (order_id == "SDR")
                {
                    frm.SDR.value = item_quantity
                }
                if (order_id == "SDO")
                {
                    frm.SDO.value = item_quantity
                }
                order_total += item_quantity * item_price
                custom = custom + "&" + order_id + "=" + item_quantity
            }
            else
            {
                if (order_id == "BET")
                {
                    frm.BET.value = 0
                }
                if (order_id == "PAT")
                {
                    frm.PAT.value = 0
                }
                if (order_id == "SPT")
                {
                    frm.SPT.value = 0
                }
                if (order_id == "ADR")
                {
                    frm.ADR.value = 0
                }
                if (order_id == "ADO")
                {
                    frm.ADO.value = 0
                }
                if (order_id == "SDR")
                {
                    frm.SDR.value = 0
                }
                if (order_id == "SDO")
                {
                    frm.SDO.value = 0
                }
                custom = custom + "&" + order_id + "=0"
            }
        }
        if (form_name.substring(0,5) == "DONAT")
        {
            // Get the quantity
            item_quantity = parseFloat(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity
                custom = custom + "&DONA=" + round_decimals(item_quantity,2)
                frm.DONA.value = round_decimals(item_quantity,2)
            }
            else
            {
                custom = custom + "&DONA=0.00"
                frm.DONA.value = "0.00"
            }
        }
    }

    // Display the total rounded to two decimal places
    frm.total_disp.value = round_decimals(order_total, 2)
    frm.total.value = round_decimals(order_total, 2)
    frm.custom.value = custom
}

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else
    {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}