$(function() {
		$("select[id='av']").change(function(){

			//alert(object2String(x));								 
			/*
			var id = $(this).attr('name');
			id = id.replace('product[','');
			id = id.replace(']','');
			*/
			var t;
			t = $(this);
			var id = t.attr('name');
			id = id.replace('product[','');
			id = id.replace(']','');
			var before = new Array() ;
			var beforecount = 0 ;
			var after = new Array() ;
			var aftercount = 0 ;			
			//before[count][0] = [id]
			//before[count][1] = [currentvalue]
			
			//alert(id);
			var flag = true;
			$.each(av, function(key,state){
				if(flag){
					t = "select[name='product["+state['nameid']+"]']" ;
					before[beforecount] = new Array();
					before[beforecount][0] = state['nameid']
					before[beforecount++][1] = $(t).val() ;
				}else{
					t = "select[name='product["+state['nameid']+"]']" ;
					after[aftercount] = new Array();
					after[aftercount][0] = state['nameid']
					after[aftercount++][1] = $(t).val() ;
				}
				if(state['nameid'] == id){
					flag = false;
				}					
			})
			//alert(before);
			var matched = new Array();
			var matched_count = 0;
			var st;
			$.each(x, function(key,state){
				st = compare(before, state);
				if(st == true){
					matched[matched_count++] = state ;
				}
			})
			//alert(matched);
			if(matched.length != 0){
				//alert(object2String(matched));
				//alert(after);

			
				$.each(after, function(key, value){
					var html = '';
					var x, d ;
					var todo = $("select[name='product["+value[0]+"]']");
					todo.empty();
					$.each(matched, function(index, data){
						x = data[value[0]] ;
						d = x.split('|');
						x = todo.children("option[value='"+ d[1] +"']").val();
						if(x == null){
							html = '<option value="'+ d[1] +'">'+ d[1] +'</option>';
							todo.append(html);
						}
					})
				})
			 }
			/*
			var flag = false;
			if($("select[name='product[12]']").attr('id') != null){
				alert($("select[name='product[1]']").attr('name'));
			}
			*/
			/*
			for(var i=0;i<1;++i){
				t = x[i];
				$.each(t, function(key,state){
					alert(key+"  "+state);			   
				})
			}
			*/
		});   
		
		$("input[name='add']").click(function(){
			//alert('d');
			var option = new Array();		//  0 index is the NAmeID
											// 1  index is the Value Selected
			var option_count = 0;
			$.each(av, function(key,state){
				t = "select[name='product["+state['nameid']+"]']" ;
				option[option_count++] = $(t).val() ;
				//option[option_count++][0] = state['nameid'] ;
				//option[option_count++][1] = $(t).val() ;
			})
			var id = $("input[name='ProductID']").val();
			//alert(option.join('|'));
			
			option = option.sort();
			var selected = option.join('|');
			//alert(selected);
			
			var scode_value = id + ',';
			var temp = Array();
			$.each(inventory_option, function(key,state){
				temp = state.split('|');
				temp = temp.sort();
				state = temp.join('|');
				if(state == selected){
					scode_value += key ;
					return false;
				}
			})
			//alert(scode_value);
			$("input[name='scode']").val(scode_value);
			return true;
		});
});

function compare(before, available){
	// before is an array
	// available is an object
	var id;
	var val_bef;
	var val_av;
	var count = before.length;
	var matched = true;
	for(var i=0;i<count;++i){
		id = before[i][0];
		val_bef = before[i][1];
		val_av = available[id].split('|');
		if(val_bef != val_av[1]){
			matched = false;
			break;
		}
	}
	
	if(matched == true){
		// there is no mismatch in any of them
		return true;
	}else{
		return false;
	}
}

function object2String(obj) {
    var val, output = "";
    if (obj) {    
        output += "{";
        for (var i in obj) {
            val = obj[i];
            switch (typeof val) {
                case ("object"):
                    if (val[0]) {
                        output += i + ":" + array2String(val) + ",";
                    } else {
                        output += i + ":" + object2String(val) + ",";
                    }
                    break;
                case ("string"):
                    output += i + ":'" + escape(val) + "',";
                    break;
                default:
                    output += i + ":" + val + ",";
            }
        }
        output = output.substring(0, output.length-1) + "}";
    }
    return output;
}
