function returnAddressToggle(event) {
	var status = $('returnAddressToggle').getValue();
	if (status == 'on') {
		Effect.BlindDown('returnAddressBox');
		validateForm();
	} else {
		Effect.BlindUp('returnAddressBox');
		$('addToCartBtn').enable();
	}
}

function validateField(rowId, fieldId, value, required, maxLength, numeric, ucfirst) {
	validateForm();
	//validate(rowId, fieldId, value, required, maxLength, numeric, ucfirst, false);
	if (typeof(errorInField[fieldId]) != "undefined" && errorInField[fieldId] != null) {
		if (errorInField[fieldId] == "required") {
			$(rowId + 'Error').update(requiredString);
		} else if (errorInField[fieldId] == "maxLength") {
			if (rowId == "zip") {
				var maxLength = 10;
			} else {
				var maxLength = 40;
			}
			$(rowId + 'Error').update(maxLengthString + maxLength);
		} else if (errorInField[fieldId] == "nan") {
			$(rowId + 'Error').update(nanString);
		}
		$(fieldId).addClassName('errorInField');
		Effect.Appear(rowId + 'Error');
		$(rowId + 'ErrorIcon').show();
	} else {
		$(fieldId).removeClassName('errorInField');
		if (fieldId == 'name_first' && (typeof(errorInField['name_last']) == "undefined" || errorInField['name_last'] == null)) {
			$(rowId + 'ErrorIcon').hide();
			$(rowId + 'Error').hide();
		} else if (fieldId == 'name_last' && (typeof(errorInField['name_first']) == "undefined" || errorInField['name_first'] == null)) {
			$(rowId + 'ErrorIcon').hide();
			$(rowId + 'Error').hide();
		} else if (fieldId != 'name_first' && fieldId != 'name_last') {
			$(rowId + 'ErrorIcon').hide();
			$(rowId + 'Error').hide();
		}
	}
}

function validateForm() {
	validate('name_first',$('name_first').value, true, 40, false, true, true);
	validate('name_last',$('name_last').value, true, 40, false, true, true);
	validate('address1Field',$('address1Field').value, true, 40, false, true, true);
	validate('address2Field',$('address2Field').value, false, 40, false, true, true);
	validate('cityField',$('cityField').value, true, 40, false, true, true);
	validate('zipField',$('zipField').value, true, 10, true, true);

	if (Object.values(errorInField).length == 0) {
		$('addToCartBtn').enable();
	} else {
		$('addToCartBtn').disable();
	}
}

function validate(fieldId, value, required, maxLength, numeric, ucfirst, validateForm) {
	if (required && value.length == 0) {
		// Required field violation
		errorInField[fieldId] = 'required';
	} else if (value.length > maxLength) {
		// maxLength violation
		errorInField[fieldId] = 'maxLength';
	} else if (numeric && isNaN(+ value)) {
		// Non-numeric violation
		errorInField[fieldId] = 'nan';
	} else {
		delete errorInField[fieldId];
	}

	if (value.length == 0) {
		$(fieldId).value = "";
	} else {
		if (ucfirst) {
			$(fieldId).value = value.substr(0, 1).toUpperCase() + value.substr(1);
		} else {
			$(fieldId).value = value;
		}
	}
}
function updateUvOption(e) {
	approvalPage.sides = $('sides').value;
	var value;
	if ($(e.findElement().id).type == 'checkbox') {
		value = ($(e.findElement().id).checked) ? 'front_uv' : 'no_uv';
	} else {
		value = e.findElement().value;
	}
	new Ajax.Request(location.protocol + '//' + location.host + location.pathname,
		    {
		        method: 'post',
		        parameters: {X : 'updateUvOption', option: value},
		        onSuccess: function() {
		        	approvalPage.swapSelect();
		        }
		    }
		    );
}
function initUvOptions() {
	if ($$('#oldApprovalOptions input')) {
		$$('#oldApprovalOptions input').each( function(e) {
				Event.observe(e, 'click', updateUvOption);
		});
	}
}
function changeMagnetQty(selectId, side) {
    new Ajax.Request(location.protocol + '//' + location.host + location.pathname,
    {
        method: 'post',
        parameters: {X : 'updateMagnetQuantity', magnetId: $(selectId).value, side: side},
        onLoading: function() {
            $('magnetQtySelectorWaiting').show();
        },
        onSuccess: function(transport, json) {
			$('magnetQtySelectorWaiting').hide();
            // We don't really need/can do anything if there was an error
			var a = transport.responseText.evalJSON();
        },
        onFailure: function() { $('qtySelectorWaiting').show(); alert('Error'); }
    }
    );
}
function changeQty(event) {
    var selectedItemId = $('itemId_selector').getValue();

    new Ajax.Request(location.protocol + '//' + location.host + location.pathname,
    {
        method: 'post',
        parameters: {A : 'approvalUpdateQty', itemId: selectedItemId},
        onLoading: function() {
            $('qtySelectorWaiting').show();
        },
        onSuccess: function(transport, json) {
			$('qtySelectorWaiting').hide();
            // We don't really need/can do anything if there was an error
			var a = transport.responseText.evalJSON();
			if (mailingListLeads) {
                if (a.status == 'ok' && a.quantity > 0) {
                    if (a.quantity - mailingListLeads > 99) {
                        new Effect.Appear('shipExtraContainer');
                    } else {
                        new Effect.Fade('shipExtraContainer');
                        $('shipExtras').checked = false;
                    }
                }
            }
            //if (a.status == 'Error') {
            //    alert('Received error');
			//}
        },
        onFailure: function() { $('qtySelectorWaiting').show(); alert('Error'); }
    }
    );
}

function updateOptionsGC(event) {
    var element = Event.element(event);            // The element that was changed.
    var waiting = null;

    params = new Array();
    params['A'] = 'updateOptionsGC';
    if (element == $('shipExtras')) {
        params['shipExtras'] = $('shipExtras').getValue();
        waiting = 'cardOptionsWaiting';
    } else if (element == $('sendToSelf')) {
        params['sendToSelf'] = $('sendToSelf').getValue();
        waiting = 'cardOptionsWaiting';
    } else if (element == $('postageRateSelector')) {
        params['postageRate'] = $('postageRateSelector').getValue();
        waiting = 'postageRateWaiting';
    }

    new Ajax.Request(location.protocol + '//' + location.host + location.pathname,
    {
        method: 'post',
        parameters: params,
        onLoading: function() { $(waiting).show(); $(waiting).style.display = 'inline'; },
        onSuccess: function(transport, json) {
            // We don't really need/can do anything if there was an error
			var a = transport.responseText.evalJSON();
			if (a.status == 'Error') {
                alert('Received error');
			}
            $(waiting).hide();
        },
        onFailure: function() { $(waiting).hide(); alert('Error'); }
    }
    );
}

function updateUVthreshold(val, uvside)
{
    var curval = $(uvside + 'UVthreshold').value;
    var newval = 0;
    if(val == "up"){
        newval = parseInt(curval) + 1000;
    }else if(val == "down"){
        newval = parseInt(curval) - 1000;
    }else if(val == "invert"){
        newval = parseInt(curval) ;
    } else {
        $(uvside+'dot'+curval).innerHTML = "";
        $(uvside+'dot'+val).innerHTML = '<div style="border:1px solid #FF6600; width:8px; height:12px; background-color:#AABBCC"></div>';
        newval = val;
    }

    $(uvside + 'UVthreshold').value = newval;
    var invert = $(uvside+'UVinvert').checked;

    var side_img = $(uvside+'UV');
    var d = new Date();
    var currentTime = d.getTime();

    new Ajax.Request(location.protocol + '//'+ location.host + location.pathname,
    {
        method: 'post',
        parameters: {A:'updateUVthreshold', side:uvside, threshold:newval , invert:invert},
        onLoading: function() {
            side_img.innerHTML = '<br/><br/><br/><span class="txt_02">Please Wait . . .</span><span class="tx_sm_grey_04"> Image Rendering</span>';
        },
        onSuccess: function(transport, json)
        {
            var obj = transport.responseText.evalJSON();
            side_img.innerHTML = obj.img;
        },
        onFailure: function() { alert('Error');  }
    }
    );
}

