function Product(basenumber, list_price, pgc) {
    this.Basenumber = basenumber;
    this.List_Price = list_price;
    this.PGC = pgc;
}

function Variant(Size_Value, Color_Value, Color_Code, I, S, B, BD, Special_Handling, Kickback, PreOrder_Flag, Expected_Date) {
    this.I = I;
    this.Size_Value = Size_Value;
    this.Color_Value = Color_Value;
    this.Color_Code = Color_Code;
    this.S = S;
    this.B = B;
    this.BD = BD;
    this.Special_Handling = Special_Handling;
    this.Kickback = Kickback;
    this.P = PreOrder_Flag;
    this.Expected_Date = Expected_Date;

    this.setPricing = function(Base_Price, Loyalty_Price, Point_Cost) {
        this.Base_Price = Base_Price;
        this.Loyalty_Price = Loyalty_Price;
        this.Point_Cost = Point_Cost;
    }
}

/**
 * Size specific functions.
 */
function validate_size_menu() {
    var size = null;
    var size_array = new Array();

    if (document.getElementById('size')) {
        var s_el = document.getElementById('size');
        size = (s_el.options[s_el.selectedIndex].value == '') ? null: s_el.options[s_el.selectedIndex].value;
    }

    s_el.length = 0;
    s_el.options[0] = new Option('Elije', '');

    for (var i in sProduct.Variants) {
            if (size_in_menu(s_el, sProduct.Variants[i].Size_Value_Id) === false) {
                s_el.options[s_el.options.length] = new Option(sProduct.Variants[i].Size_Value, sProduct.Variants[i].Size_Value_Id);
            }
        }
    // Sorting function call
    sortSelect(document.getElementById('size'));
}

function size_in_menu(size_object, size) {
    for (var i = 0; i < size_object.options.length; i++) {
        if (size_object.options[i].value == size) {
            return true;
        }
    }
    return false;
}

// size drop-down sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();

    // This keeps choose at the top
    if (value1 === 'elije') return ( - 1);
    if (value2 === 'elije') return (1);

    // Lets handle letter sizes i.e s, m, l
    // basically converting the sizes into a numeric that
    // evaluates to the order we would like them to appear
    var sizeText = new Array('yxs', 'ys', 'ym', 'yl', 'yxl', 'xxs', '2xs', 'xs', 's', 'm', 'l', 'xl', 'xxl', '2xl', 'xxxl', '3xl');
    var tempValue1 = new String(value1);
    var tempValue2 = new String(value2);
    var matchLetters = new RegExp(/[a-z]+/);

    var resultSizeVal1 = tempValue1.match(matchLetters);
    var resultSizeVal2 = tempValue2.match(matchLetters);

    if (resultSizeVal1) {
        for (var abc = 0; abc <= sizeText.length; abc++)
        if (value1 == sizeText[abc]) value1 = abc;
    }
    if (resultSizeVal2) {
        for (var abc = 0; abc <= sizeText.length; abc++)
        if (value2 == sizeText[abc]) value2 = abc;
    }

    // get the length of each value for padding purposes
    var len_value1 = value1.length;
    var len_value2 = value2.length;
    // this makes sure the numbers actually compare correctly
    // since jscript isnt recognizing them as actual numbers
    // basically padding the string so that the digits line up
    if (len_value1 < len_value2) {
        for (var x = len_value1; x < len_value2; x++)
        value1 = ' ' + value1;
    }
    if (len_value2 < len_value1) {
        for (var x = len_value2; x < len_value1; x++)
        value2 = ' ' + value2;
    }

    // makes sure that half sizes line up with the matching
    // whole size
    var tempValue1a = new String(value1);
    var tempValue2a = new String(value2);
    var matchHalf = new RegExp(/\.5/);
    var resultHalfVal1 = tempValue1a.match(matchHalf);
    var resultHalfVal2 = tempValue2a.match(matchHalf);

    if (resultHalfVal1) {
        value1 = '  ' + value1.replace(/\.5/, '');
        // need to see if value 2 is a half size too
        if (tempValue2a.match(/\.5/)) value2 = '  ' + value2.replace(/\.5/, '');
        if (value1 >= value2) return (1);
        if (value1 < value2) return ( - 1);
    }

    if (resultHalfVal2) {
        value2 = '  ' + value2.replace(/\.5/, '');
        // need to see if value 1 is a half size too
        if (tempValue1a.match(/\.5/)) value1 = '  ' + value1.replace(/\.5/, '');
        if (value2 >= value1) return ( - 1);
        if (value2 < value1) return (1);
    }

    // if we make it here, just flat out compare them
    if (value1 > value2) return (1);
    if (value1 < value2) return ( - 1);
    return (0);
}

function sortSelect(selectToSort) {
    // copy options into an array
    var myOptions = [];
    for (var loop = 0; loop < selectToSort.options.length; loop++) {
        myOptions[loop] = {
            optText: selectToSort.options[loop].text,
            optValue: selectToSort.options[loop].value
        };
    }

    myOptions.sort(sortFuncAsc);

    // copy sorted options from array back to select box
    selectToSort.options.length = 0;
    for (var loop = 0; loop < myOptions.length; loop++) {
        var optObj = document.createElement('option');
        optObj.text = myOptions[loop].optText;
        optObj.value = myOptions[loop].optValue;
        selectToSort.options[selectToSort.options.length] = new Option(optObj.text, optObj.value);
    }
}
// End Size Sort functions
/**
 * Color specific functions
 */
function update_image(basenumber) {
    if (update_image_customization()) {
        return true;
    }

    var color = null;
    var color_code = null;
    var i = 0;
    var j = 0;
    var q = 0;

    if (document.getElementById('color')) {
      var c_el = document.getElementById('color');
      color = (c_el.options[c_el.selectedIndex].value == '') ? null: c_el.options[c_el.selectedIndex].value;
    } else if (document.getElementById('selectedsinglecolor')) {
    	var c_el = document.getElementById('selectedsinglecolor');
      color = (c_el.value == '') ? null: c_el.value;
    }

    if (c_el) {
        if (color) {
            for (i in sProduct.Variants) {
                if (color == sProduct.Variants[i].Color_Value_Id) {
                    color_code = sProduct.Variants[i].Color_Code;
                    break;
                }
            }
        }
        else {
            for (i in sProduct.Variants) {
                if (q <= sProduct.Variants[i].I) {
                    q = sProduct.Variants[i].I;
                    color_code = sProduct.Variants[i].Color_Code;
                }
            }
        }

        if (document.images.ProductImage.src != 'http://' + window.location.host + 'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/Images/Catalog/ProductImages/300/' + basenumber + '.' + color_code + '.JPG') {
            document.images.ProductImage.src = 'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/Images/Catalog/ProductImages/300/' + basenumber + '.' + color_code + '.JPG';
        }
    }
}

function update_image_giftcard(basenumber) {
    var color = null;
    var color_code = null;
    var i = 0;
    var j = 0;
    var q = 0;
		
		
		if (document.getElementById('color')) {
      var c_el = document.getElementById('color');
      color = (c_el.options[c_el.selectedIndex].value == '') ? null: c_el.options[c_el.selectedIndex].value;
    } else if (document.getElementById('selectedsinglecolor')) {
    	var c_el = document.getElementById('selectedsinglecolor');
      color = (c_el.value == '') ? null: c_el.value;
    }

    if (c_el) {
        if (color) {
            for (i in sProduct.Variants) {
                if (color == sProduct.Variants[i].Color_Value_Id) {
                    color_code = sProduct.Variants[i].Color_Code;
                    break;
                }
            }
        }
        else {
            for (i in sProduct.Variants) {
                if (q <= sProduct.Variants[i].I) {
                    q = sProduct.Variants[i].I;
                    color_code = sProduct.Variants[i].Color_Code;
                }
            }
        }

        if (document.images.ProductImage.src != 'http://' + window.location.host + 'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/Images/Catalog/ProductImages/300/' + basenumber + '.' + color_code + '.JPG') {
            document.images.ProductImage.src = 'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/Images/Catalog/ProductImages/300/' + basenumber + '.' + color_code + '.JPG';
        }
    }
}

function validate_color_menu() {
    var color = null;
    var color_array = new Array();

    if (document.getElementById('color')) {
      var c_el = document.getElementById('color');
      color = (c_el.options[c_el.selectedIndex].value == '') ? null: c_el.options[c_el.selectedIndex].value;
      c_el.length = 0;
    	c_el.options[0] = new Option('Elije', '');
    } else if (document.getElementById('selectedsinglecolor')) {
    	var c_el = document.getElementById('selectedsinglecolor');
      color = (c_el.value == '') ? null: c_el.value;
    }

    for (var i in sProduct.Variants) {
        if (sProduct.Variants[i].I >= 1 || sProduct.Variants[i].B >= 1 || sProduct.Variants[i].S >= 1) {
            c_el.options[c_el.options.length] = new Option(sProduct.Variants[i].Color_Value, sProduct.Variants[i].Color_Value_Id);
        }
    }
}
// End Color specific functions
function update_image_customization() {
    // Array of available cust_packages with name='Package_Name'
    var cust_packages = document.getElementsByName('Package_Name');
    // They clicked Customize? Yes radio
    if (document.getElementsByName('Customize').length >= 1) {
        if (document.getElementsByName('Customize')[0].checked == true) {
            // There should be 1 or more customization cust_packages to loop through
            for (var p = 0; p < cust_packages.length; p++) {
                // Customer's selected package
                if (cust_packages[p].checked == true) {
                    // Following code is for Club Store cust_packages
                    if (cust_packages[p].getAttribute('value').substring(0, 3).toUpperCase() == 'CS.') {
                        var values = document.getElementsByName(cust_packages[p].getAttribute('value') + '.LOGO');
                        for (var m = 0; m < values.length; m++) {
                            if (values[m].getAttribute('type') == 'radio') {
                                if (values[m].checked == true) {
                                    packageImageUpdateFunctions[cust_packages[p].getAttribute('value')](document.getElementsByName(cust_packages[p].getAttribute('value') + '.LOGO')[m].value);
                                    return true;
                                }
                            }
                        }
                    }
                    // Following code is for DLHERO and REPLICA cust_packages
                    else if (cust_packages[p].getAttribute('value').toUpperCase() == 'DLHERO' || cust_packages[p].getAttribute('value').toUpperCase() == 'ORIGINALES') {
                        var jersey_name = '';
                        var jersey_num = '';
                        // <div> that contains the customization package elements
                        var div_name = '' + cust_packages[p].getAttribute('value').toUpperCase() + '.NameNumber.Section';
                        // All <input> tags under the <div>
                        var div_input_tags = document.getElementById(div_name).getElementsByTagName('input');
                        // Lets find the right <input> tags
                        for (var i = 0; i < div_input_tags.length; i++) {
                            // Want don't want the hidden <input> tags
                            if (div_input_tags[i].getAttribute('type') == 'text') {
                                // Input tags 'name' attribute
                                var input_name_attr = div_input_tags[i].getAttribute('nombre');
                                // Need to know the <input id="?"> value for the NAME customization
                                if (jersey_name == '' && input_name_attr.match("NOMBRE")) {
                                    jersey_name = input_name_attr;
                                }
                                // Need to know the <input id="?"> value for the NUMBER customization
                                else if (jersey_num == '' && input_name_attr.match("N\332MERO")) {
                                    jersey_num = input_name_attr;
                                }
                            }
                        }
                        // If input ids for for exit then make the call to the inline javascript generated by ProductCustomizationPackages.tem
                        if (jersey_name != '' && jersey_num != '') {
                            packageImageUpdateFunctions[cust_packages[p].getAttribute('value')](document.getElementById(jersey_name).value, document.getElementById(jersey_num).value);
                            return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}

/**
 * Tier Pricing specific functions
 */
function open_tierpricing_matrix() {
    if (document.getElementById('matrix')) document.getElementById('matrix').style.display = 'none'
    document.getElementById('tierPricing').style.display = 'block';
    if (document.getElementById('tierPricing').style.display == 'block') {
        document.getElementById('PriceChartLink').style.display = 'none';
    } else {
        document.getElementById('PriceChartLink').style.display == 'block'
    }

}
// End Tier Pricing specific functions

/**
 * Sizes AND Colors exist
 */
function open_availability_matrix() {
    if (document.getElementById('tierPricing')) document.getElementById('tierPricing').style.display = 'none';
    document.getElementById('matrix').style.display = 'block';
}

function update_color_menu() {
    var size = null;
    var color = null;

    if (document.getElementById('size')) {
        var s_el = document.getElementById('size');
        size = (s_el.options[s_el.selectedIndex].value == '') ? null: s_el.options[s_el.selectedIndex].value;
    }

    if (document.getElementById('color')) {
      var c_el = document.getElementById('color');
      color = (c_el.options[c_el.selectedIndex].value == '') ? null: c_el.options[c_el.selectedIndex].value;
      c_el.length = 0;
    	c_el.options[0] = new Option('Elije', '');
    } else if (document.getElementById('selectedsinglecolor')) {
    	var c_el = document.getElementById('selectedsinglecolor');
      color = (c_el.value == '') ? null: c_el.value;
    }

    if (size == null) {
        return;
    }

    for (var i in sProduct.Variants) {
        if (size == sProduct.Variants[i].Size_Value_Id && (sProduct.Variants[i].I >= 1 || sProduct.Variants[i].B >= 1 || sProduct.Variants[i].S >= 1)) {
            c_el.options[c_el.options.length] = new Option(sProduct.Variants[i].Color_Value, sProduct.Variants[i].Color_Value_Id);
        }
    }
}

/**
 * Sizes OR Colors Exist
 */
function update_availability() {
    var size = null;
    var color = null;

    var availability = null;
    var a_el = document.getElementById('availability');

    clearTimeout(pulse_events['availability']);

    if (document.getElementById('size')) {
        var s_el = document.getElementById('size');
        size = (s_el.options[s_el.selectedIndex].value == '') ? null: s_el.options[s_el.selectedIndex].value;
    }

    if (document.getElementById('color')) { // Color dropdown exists
        var c_el = document.getElementById('color');
        color = (c_el.options[c_el.selectedIndex].value == '') ? null: c_el.options[c_el.selectedIndex].value;
    } else if (document.getElementById('singlecolor')) { // No color dropdown, hidden field
		color = document.getElementById('singlecolor').value;
    }

    if (s_el && c_el) availability = '<br><a href="javascript:void();" onclick="open_availability_matrix(); return false;" title="\277Qu\351 significa esto?">Verificar la disponibilidad</a>'
    else availability = '<br><br>'

    for (var i in sProduct.Variants) {
        if (size == sProduct.Variants[i].Size_Value_Id && color == sProduct.Variants[i].Color_Value_Id) {
            if (sProduct.Variants[i].I >= 1) {
                availability = '<br><a onclick="return SEI_createWindow(\'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsInStock.html\', \'WhatIsInStock\', \'height=315,width=300,resizable=0,status=0,menubar=0,toolbar=0,scrollbars=1\');" href="http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsInStock.html" target="_blank">En existencia</a>';
                if (document.getElementById('specialorder')) {
                    document.getElementById('specialorder').style.display = 'none';
                }
            }
            else if (sProduct.Variants[i].S >= 1) {
                availability = '<br><a onclick="return SEI_createWindow(\'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsSpecialOrder.html\', \'WhatIsSpecialOrder\', \'height=315,width=300,resizable=0,status=0,menubar=0,toolbar=0,scrollbars=1\');" href="http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsSpecialOrder.html" target="_blank">Pedido especial</a>';
                if (document.getElementById('specialorder')) {
                    document.getElementById('specialorder').style.display = 'block';
                }
            }
            else if ((sProduct.Variants[i].B >= 1) && (sProduct.Variants[i].P == 0)) {
                availability = '<br><a onclick="return SEI_createWindow(\'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsBackOrder.html\', \'WhatIsBackOrder\', \'height=315,width=300,resizable=0,status=0,menubar=0,toolbar=0,scrollbars=1\');" href="http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsBackOrder.html" target="_blank">Pedido retrasado</a>';
                if (sProduct.Variants[i].Expected_Date != '') {
                    availability += '<br>Esperado para ' + sProduct.Variants[i].Expected_Date + '.';
                }
                if (document.getElementById('specialorder')) {
                    document.getElementById('specialorder').style.display = 'none';
                }
            }
            else if ((sProduct.Variants[i].B >= 1) && (sProduct.Variants[i].P == 1)) {
                availability = '<br><a onclick="return SEI_createWindow(\'http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsPreOrder.html\', \'WhatIsPreOrder\', \'height=315,width=300,resizable=0,status=0,menubar=0,toolbar=0,scrollbars=1\');" href="http://espanol.soccer.com/soccer/enes/24/_www_soccer_com/WhatIsPreOrder.html" target="_blank">Preorden</a>';
                if (sProduct.Variants[i].Expected_Date != '') {
                    availability += '<br>Esperado para ' + sProduct.Variants[i].Expected_Date + '.';
                }
                if (document.getElementById('specialorder')) {
                    document.getElementById('specialorder').style.display = 'none';
                }
            }
        }
    }

    if (a_el.innerHTML.toLowerCase() != availability.toLowerCase()) {
        a_el.style.visibility = 'hidden';
        a_el.innerHTML = availability;
        if (availability == '<br><br>' || availability.indexOf('Verificar la disponibilidad') >= 1) a_el.style.visibility = 'visible';
        else pulse_events['availability'] = setTimeout('pulse_value(\'availability\', true, 3);', 500);
    }
}

function update_GiftCardPrice()
{
	clearTimeout(pulse_events['price']);
	var GiftCard_Amount = document.getElementById('giftcard_amnt').value;

	var p_el = document.getElementById('price');
	p_el.style.visibility = 'visible';

	// ending decimal
	if(GiftCard_Amount.charAt(GiftCard_Amount.length-1) == "."){
		GiftCard_Amount += "00";
		document.getElementById('giftcard_amnt').value = GiftCard_Amount;
	}

	// dollar sign
	if(GiftCard_Amount.charAt(0) == "$"){
		GiftCard_Amount = GiftCard_Amount.substring(1,GiftCard_Amount.length);
		document.getElementById('giftcard_amnt').value = GiftCard_Amount;
	}

	if(GiftCard_Amount >= 5)
	{
		var pricing_copy = 'Precio: $' + round_decimals(GiftCard_Amount, 2);
		var Current_Price = round_decimals(GiftCard_Amount, 2);
		document.getElementById('giftcard_amnt').value = round_decimals(GiftCard_Amount, 2);

		if(p_el.innerHTML != pricing_copy)
		{
			p_el.style.visibility = 'hidden';
			p_el.innerHTML = pricing_copy;
			pulse_events['price'] = setTimeout('pulse_value(\'price\', true, 3);', 500);
		}

		var giftCardRecipientName	= document.getElementById('giftcard_recipientname').value;
		var giftCardSenderName		= document.getElementById('giftcard_sendername').value ;
		var giftCardRecipientEmail	= document.getElementById('giftcard_recipientemail').value;
		var giftCardGreeting		= document.getElementById('giftcard_greeting').value;

		giftCardRecipientName		= giftCardRecipientName.replace(/'/, "\\'");
		giftCardRecipientName		= giftCardRecipientName.replace(/"/, "\\\"");
		giftCardSenderName			= giftCardSenderName.replace(/'/, "\\'");
		giftCardSenderName			= giftCardSenderName.replace(/"/, "\\\"");
		giftCardRecipientEmail		= giftCardRecipientEmail.replace(/'/, "\\'");
		giftCardRecipientEmail		= giftCardRecipientEmail.replace(/"/, "\\\"");
		giftCardGreeting			= giftCardGreeting.replace(/'/, "\\");
		giftCardGreeting			= giftCardGreeting.replace(/"/, "\\\"");

		document.getElementById('Customization_Value').value = GiftCard_Amount + '|' + giftCardRecipientName + '|' + giftCardSenderName + '|' + giftCardRecipientEmail + '|' + giftCardGreeting;
		//document.getElementById('Customization_Value').value = GiftCard_Amount + '|' + document.getElementById('giftcard_recipientname').value + '|' + document.getElementById('giftcard_sendername').value + '|' + document.getElementById('giftcard_recipientemail').value + '|' + document.getElementById('giftcard_greeting').value;
		return true;
	} else {
		alert("Se pueden comprar tarjetas de regalopor un monto m\355nimo de $5.00");
		document.getElementById('giftcard_amnt').value = "5.00";
		document.getElementById('giftcard_amnt').focus();
		return false;
	}
}

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) {

    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    } else {
        decimal_part_length = value_string.length - decimal_location - 1
    }

    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++){
            value_string += "0"
        }
	}
    return value_string
}

function check_GiftCardRecipientEmail() {
    // If they selected delivery by email so we need to check for email address
    if (document.getElementById('sizeValue').value == '3755' && document.getElementById('giftcard_recipientemail').value == '') {
        alert("Has elegido que esta tarjeta de regalo se entregue v\355a email. Proporciona un domicilio postal para el receptor.");
        document.getElementById('giftcard_recipientemail').focus();
        return false;
    } else {
        return true;
    }
}

function check_GiftCardRecipientSenderNames() {
	var giftCardRecipientName	= document.getElementById('giftcard_recipientname').value;
	var giftCardSenderName		= document.getElementById('giftcard_sendername').value ;

	// If they selected delivery by email so we need to check for email address
	if(giftCardRecipientName === "" || giftCardSenderName === ""){
		alert("Debes ingresar el nombre del destinatario y el del emisor");
		document.getElementById('giftcard_recipientname').focus();
		return false;
	} else {
		return true;
	}
}

function validate_GiftCardForm()
{
	if(!check_GiftCardRecipientEmail())
		return false;
	if(!update_GiftCardPrice())
		return false;
	if(!check_GiftCardRecipientSenderNames())
		return false;

	return true;
}

function authenticateCustomName(input) {
	var Regex = new RegExp("^[A-Za-z .'ÀÁÈÉÑ-]+$");
	var val = input.value;
	if( val != '' &&  !val.match( Regex ) ) {
	var newValue = '';
  		alert('Names can only contain the following: letters A – Z, À, Á, È, É, Ñ, spaces, and special characters dot(.), apostrophe (\'), or hyphen (–).');
		newText = new Array(val.length);
		for (i=0; i<newText.length; i++) {
			if(val.charAt(i).match( Regex )) {
				newText[i] = val.charAt(i);
				}
			}
		newValue = newText.join('');
		input.value = newValue;
		//input.value = '';
  		return false;
 	} else {
		return true;
		}
}

function scrubLetter(v) {
	var Regex = new RegExp("^[A-Za-z .'ÀÁÈÉÑ-]+$");
	if( !v.match( Regex ) ) {
		newText = new Array(v.length);
		for (i=0; i<newText.length; i++) {
			if(v.charAt(i).match( Regex )) {
				newText[i] = v.charAt(i);
				}
			}
		newValue = newText.join('');
		return newValue;
		} else {
		return v;
		}
}

function authenticateCustomNumber(input) {
	var Regex = new RegExp("^[0-9]+$");
	var val = input.value;
	if( val != '' && !val.match( Regex ) ) {
  		alert('Los n\372meros s\363lo pueden contener los n\372meros 0-99');
		newText = new Array(val.length);
		for (i=0; i<newText.length; i++) {
			if(val.charAt(i).match( Regex )) {
				newText[i] = val.charAt(i);
				}
			}
		newValue = newText.join('');
		input.value = newValue;
		//input.value = '';
  		return false;

 	} else {
		return true;
		}
}

function scrubNumber(v) {
	var Regex = new RegExp("^[0-9]+$");
	if( !v.match( Regex ) ) {
		newText = new Array(v.length);
		for (i=0; i<newText.length; i++) {
			if(v.charAt(i).match( Regex )) {
				newText[i] = v.charAt(i);
				}
			}
		newValue = newText.join('');
		return newValue;
		} else {
		return v;
		}
}

function CharacterCounter(element_name, max) {
    element = document.forms['OrderByVariantSimple'].elements[element_name];

    if (element.value.indexOf('\r\n') >= 0) {
        element.value = element.value.replace(/\r\n/g, '<br />');
    }

    if (element.value.length > max) {
        element.value = element.value.substring(0, max);
    }
    else {
        document.getElementById(element_name + 'Counter').innerHTML = (max - element.value.length) + ' caracteres disponibles';
    }
}

var pulse_events = new Array();

function pulse_value(object, visible, repeat) {
    if (repeat > 0) {
        if (visible) document.getElementById(object).style.visibility = 'visible';
        else document.getElementById(object).style.visibility = 'hidden';
        pulse_events[object] = setTimeout('pulse_value(\'' + object + '\', ' + !visible + ', ' + (--repeat) + ');', 500);
    }
}

function addslashes(str) {
	//example: addslashes("kevin's birthday"); returns 1: 'kevin\'s birthday'
    return (str + "").replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}