var photoCount = 1;
var maxImageUpload = 1;

function PackageSelected() {

    var number = 1;
        
    var AddOnsNoneMessage = "(Please choose a package above first.)"
    var AddOnsAllMessage = "The All Inclusive Retouch Package includes all possible add ons."
    var AddOnsCustomMessage = "Because you want a custom package, we will need to work with you 1 on 1 to come up with what is right for you."
    
    var packageType = "";
    
    var rdolist = document.getElementsByName("ctl00$ctl00$cphMain$cphInsideMain$rblPackageTypeN" + number);

    if (rdolist[0].checked) { packageType = "Facial"; }
    if (rdolist[1].checked) { packageType = "Body"; }
    if (rdolist[2].checked) { packageType = "All"; }
    
    var addOnsMessage = document.getElementById('addOnsMessageN' + number);
    var addOnsFacial = document.getElementById('addOnsFacialN' + number);
    var addOnsBody = document.getElementById('addOnsBodyN' + number);
    
    switch(packageType)
    {
        case "Facial":
            addOnsMessage.style.display = 'none';
            addOnsFacial.style.display = 'block';
            addOnsBody.style.display = 'block';
            break;
        case "Body":
            ClearAddOnsA();
            addOnsMessage.style.display = 'none';
            addOnsFacial.style.display = 'none';
            addOnsBody.style.display = 'block';
            break;
        case "All":
            ClearAddOnsA();
            ClearAddOnsB();
            addOnsMessage.innerHTML = AddOnsAllMessage;
            addOnsMessage.style.display = 'block';
            addOnsFacial.style.display = 'none';
            addOnsBody.style.display = 'none';
            break;
        default:
            addOnsMessage.innerHTML = AddOnsNoneMessage;
            addOnsMessage.style.display = 'block';
            addOnsFacial.style.display = 'none';
            addOnsBody.style.display = 'none';
            break;
    }
    
    UpdateTotal();
}

function ClearAddOnsA() {

	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_0").checked = false;
	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_1").checked = false;
	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_2").checked = false;
	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_3").checked = false;
}

function ClearAddOnsB() {

	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsBN1_0").checked = false;
	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsBN1_1").checked = false;
	document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsBN1_2").checked = false;
}

function UpdateTotal() {

    var txtTotal = document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_txtTotal");
	
	var total = 0;
	
	var rdolist = document.getElementsByName("ctl00$ctl00$cphMain$cphInsideMain$rblPackageTypeN1");

    if (rdolist[0].checked) { 
        total += 24.99;
    } else if (rdolist[1].checked) {  
        total += 34.99;
    } else if (rdolist[2].checked) { 
        total += 44.99;
    }
    
    if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_0").checked) total += 9.99; // Virtual Weight Loss ($9.99)
	if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_1").checked) total += 4.99; // Virtual Breast Enhancement ($4.99) 
	if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_2").checked) total += 4.99; // Virtual Makeup ($4.99) 
	if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsAN1_3").checked) total += 4.99; // Hair and Eye Color Change ($4.99) 
    
    if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsBN1_0").checked) total += 4.99; // Clothing Changes ($4.99) 
	if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsBN1_1").checked) total += 4.99; // Set Clean Up or Removal ($4.99) 
	if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cblAddOnsBN1_2").checked) total += 14.99; // Background Changes ($14.99) 
	
	if (document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_cbDiscount").checked) total += -5.0; // -5			
    
    txtTotal.value = "$" + Math.round(total*100)/100;
}

function FindErrors() {		

	var errorMessage = "";
	
	// check first/last name
	errorMessage += ValidateField("ctl00_ctl00_cphMain_cphInsideMain_txtFirst", false, ".+", "First Name", "");
	errorMessage += ValidateField("ctl00_ctl00_cphMain_cphInsideMain_txtLast", false, ".+", "Last Name", "");
	
	// check emails
	var emailReqEx = "[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?";
	errorMessage += ValidateField("ctl00_ctl00_cphMain_cphInsideMain_txtEmail", false, emailReqEx, "Email", "");
	errorMessage += ValidateField("ctl00_ctl00_cphMain_cphInsideMain_txtEmailConfirm", false, "^" + document.getElementById('ctl00_ctl00_cphMain_cphInsideMain_txtEmail').value + "$", "Retype Email", "'Retype Email' does not correctly match 'Email'.");
				
	// check files
	for (x = 1; x <= maxImageUpload; x++)
	{
		// check package type
        var rdolist = document.getElementsByName("ctl00$ctl00$cphMain$cphInsideMain$rblPackageTypeN" + x);
        
        var packageType = "";       
        if (rdolist[0].checked) { packageType = "Facial" }
        if (rdolist[1].checked) { packageType = "Body" }
        if (rdolist[2].checked) { packageType = "All" }
        var rdolistId = document.getElementById("ctl00_ctl00_cphMain_cphInsideMain_rblPackageTypeN" + x);
        rdolistId.className = "field fieldNoBorder";
        if (packageType == "") { 
            errorMessage += ShowError(rdolistId, "", "Retouch Package", "required");
        }
        
        // check the file type
        errorMessage += CheckFile("ctl00_ctl00_cphMain_cphInsideMain_fileUploadN" + x, x);
	}
	
	// instructions
	errorMessage += ValidateField("ctl00_ctl00_cphMain_cphInsideMain_txtInstructionsN1", false, ".+", "Retouch Instructions", "");
	
	// check certify question
	var certifyEl = document.getElementById('ctl00_ctl00_cphMain_cphInsideMain_cbCertify');
	certifyEl.className = "field fieldNoBorder";
	if (certifyEl.checked == false) { 
        errorMessage += ShowError(certifyEl, "You must check the checkbox at the bottom certifying that you have permission to use this photo and that you understand that the order is commissioned work.", "", "");
    }

	return errorMessage;
}

function CheckFile(elementId, number) {

    var fileUploadEl = document.getElementById(elementId);
    
    if (fileUploadEl.value == "") {
    
        return ShowError(fileUploadEl, "", "Photo File", "required");
        
    } else {
        
        var filename = document.getElementById(elementId).value.toLowerCase();
        var hash = {  
            '.jpg' : 1,
            '.jpeg': 2, 
            '.bmp' : 3, 
            '.eps' : 4, 
            '.png' : 5, 
            '.tif' : 6
            };

        //var hash = new Hash('.jpg', 1, '.jpeg', 2, '.bmp', 3, '.eps', 4, '.png', 5, '.tif', 6);

        var re = /\..+$/;      
        var ext = filename.match(re);
        
        if (!hash[ext]) {
            return ShowError(fileUploadEl, "'Photo File' is not a type that we accept (.jpg, .bmp, .eps, .png, .tif).", "", "");
        }
    }
    
    fileUploadEl.className = "field";
    return "";
}

function ValidateForm() {

	// Error Div.
	var errorEl = document.getElementById('errorDiv');
	var errorMessage = "";
		
	// Clear out the error.
	errorEl.innerHTML = "";
	errorEl.style.display = "none";
	
	// Get error messages if any.
	errorMessage = FindErrors();
	
	// If there was an error.
	if (errorMessage != "") {
		errorEl.innerHTML = "<b>Please correct the following error(s):</b><ul>" + errorMessage + "</ul>";
		errorEl.style.display = "block";
		
		return false;
	}
	
	return true;
}

function ValidateField(controlID, skipIfBlank, strRegex, fieldName, customError) {
	
	// Fields for the element and error element.
	var Field = document.getElementById(controlID);

	if (Field != null) {
	
		Field.className = 'field';
		
		// Skip the reg ex check if the field is blank if the parameter set to true.
		if (Field.value == '') 
		{ 
			if (skipIfBlank) 
			{
				return "";
			} 
			else 
			{
			    return ShowError(Field, customError, fieldName, "required");
			}
		}
    	
		if (strRegex) {
		    if (! RegExCheck(Field.value, strRegex, true)) 
		    {
		        return ShowError(Field, customError, fieldName, "invalid");
		    }
		}
	}
	
	return "";
}

function ShowError(Field, customError, fieldName, word) {

    Field.className = 'field fieldError';
	if (customError != '') {
		return "<li class='errorLi'>" + customError + "</li>";
	} else {
		return "<li class='errorLi'>'" + fieldName + "' is " + word + ".</li>";
	}
}

function RegExCheck(valueToCheck, strRegex) {
	var regex = new RegExp(strRegex);
	var isValid = false;
	isValid = regex.test(valueToCheck);
	return isValid;
}
