﻿
// for the current weight system, Z10 - Z20 weight upto 200
// Z26 upto 230, S26j weight upto 350 when outReach <= 10, only upto 100 otherwise

jQuery.noConflict();
jQuery(document).ready(function() {
    var the_data = [];
    var the_data_str = jQuery('input:hidden[id*="ExcelHiddenField"]').val();
    if (the_data_str.length && the_data_str.length > 0) {
        var the_data_arr = the_data_str.split(';');
        for (var i = 0, length = the_data_arr.length; i < length; ++i) {
            the_data[i] = the_data_arr[i].split(',');
        }
    }
    // if out reach <= 10
    var weight_data1 = ['Z10, Z14, Z17, Z20, Z26, S26j',	// 0 - 100 kgs
						'Z10, Z14, Z17, Z20, Z26, S26j', 	// 100 - 200 kgs
						'Z19, Z26, S26j', 	// 200 - 230 kgs
						'S26j'];	// 230 - 350 kgs
    // if out reach >= 11
    var weight_data2 = ['Z26, S26j', 'Z26', 'Z26', ''];

    jQuery('select').change(function() {	// observe all the dropdown lists
        var weight = 0, height = 0, outReach = 0;   // make 0 the default since 0 does not limit the choices
        jQuery("select option:selected").each(function() {
            var parentID = jQuery(this).parent()[0].id;
            if (parentID.indexOf('weight') > -1) {  // weight;
                weight = GetSelectValueAsInt(this);  //parseInt(jQuery(this).val());
            }
            else if (parentID.indexOf('height') > -1) {
                height = GetSelectValueAsInt(this);
            }
            else if (parentID.indexOf('outReach') > -1) {
                outReach = GetSelectValueAsInt(this);
            }
        });

        var weightString = '', choices = '';
        if (outReach >= 11) {
            weightString = weight_data2[weight];
        }
        else {
            weightString = weight_data1[weight];
        }
		
		// if it 200 - 230 kgs
		if (weight == 2) {
			var tmpType = the_data[height][outReach];
			if (parseInt(tmpType.substring(1, 3)) <= 19) {
				choices += 'Z19';
			}
		}
		
        if (choices.length < 3) {	// if above logic has not get a result
			for (var i = height, hLength = the_data.length; i < hLength; ++i) {
				for (var j = outReach, oLength = the_data[i].length; j < oLength; ++j) {
					var type = the_data[i][j];
					if (type.length >= 3) {
						if (weightString.indexOf(type) > -1 && choices.length < 3) {
							choices += type;    // logic changed to add only one type
						}
					}
				}
			}
		}
        if (choices.length >= 3) {
            jQuery('#message').text(choices);
        }
        else {
            jQuery('#message').text("Sorry, but we could not find a suitable type.");
        }
		
		jQuery('#result_notice').show('slow');
    });

    function GetSelectValueAsInt(that) {
        var value = parseInt(jQuery(that).val());
        return value > 0 ? value : 0;
    }

    // for the curves
    jQuery('#out_wrapper').corner({
        tl: { radius: 16 },
        tr: { radius: 16 },
        bl: { radius: 16 },
        br: { radius: 16 }
    });
});