// $Id: exceptions.js 1161 2006-01-25 15:08:17Z imac $

var except = new Array();
var exceptnone = new Array();
var pr_options = new Array();
var exceptions = new Array();
var dni = 0;
var ind = 0;
var i = 0; 
var j = 0; 

//
// Check all products for exceptions
//
function fn_check_all_exceptions(one)
{
	for (i in pr_options) {
		if (fn_check_exceptions(i) == false) {
			if (one == true) {
				return false;
			}
		}
	}
	return true;
}

//
// Check all selected options variants in exceptions
//
function fn_check_exceptions(id)
{
var m = 0; n = 0; k = 0; index = 0;
	if (typeof(pr_options[id]) == 'undefined') {
		return true;
	}
	// Define Selected values for each option
	for (i = 0; i < pr_options[id].length; i++) {
		if (pr_options[id][i]['type'] == 'S') {
			pr_options[id][i]['selected_value'] = document.getElementById(pr_options[id][i]['id']).value;
		} else if (pr_options[id][i]['type'] == 'C') {	
			document.getElementById('unchecked_'+pr_options[id][i]['id']).value = pr_options[id][i]['variants'][0]['variant_id'];
			if (document.getElementById(pr_options[id][i]['id']).checked) {
				pr_options[id][i]['selected_value'] = document.getElementById(pr_options[id][i]['id']).value;
			} else {
				pr_options[id][i]['selected_value'] = document.getElementById('unchecked_'+pr_options[id][i]['id']).value;
			}
		} else if (pr_options[id][i]['type'] == 'R'){
			for (var k = 0; k < document.getElementById(pr_options[id][i]['id']).getElementsByTagName("INPUT").length; k++) {
				if (document.getElementById(pr_options[id][i]['id']).getElementsByTagName("INPUT")[k].checked == true) {
					pr_options[id][i]['selected_value'] = document.getElementById(pr_options[id][i]['id']).getElementsByTagName("INPUT")[k].value;
				}		
			}
		}
	}

	// Resets all variants in the selectboxes
	fn_empty_selectboxes(id);
	// Function that calculates how many options in exception
	if (fn_calculate_matches(id) == false) {
		return false;
	}

	if (exception_style == 'warning') {
		document.getElementById(id + '_warning').style.display='none';
		fn_disable(id);
		return true;
	}

	// Disables options that have status disabled in exception and other exceptions sutisfied
	fn_disable(id);

	// This section fills the array with options considering the exceptions that should be hided
	for (m = 0; m < exceptions[id].length; m++) { // Cylcle all exceptions for the product
		if (except[id][m] == pr_options[id].length-1) { 
			for (k = 0; k < pr_options[id].length; k++) { // Cycle all options of the product
				if ((pr_options[id][k]['selected_value'] != exceptions[id][m][k]) && (exceptions[id][m][k] != '-1')) {
					for (var j = 0; j < pr_options[id][k]['variants'].length; j++) { // Cycle all option variants
						if (pr_options[id][k]['variants'][j]['variant_id'] == exceptions[id][m][k]) {
							if (pr_options[id][k]['type'] == 'S') {
								pr_options[id][k]['variants'][j]['hide'] = 'Y';
							}
							if (pr_options[id][k]['type'] == 'C') {
								document.getElementById('unchecked_'+pr_options[id][k]['id']).value = pr_options[id][k]['selected_value'];
								document.getElementById(pr_options[id][k]['id']).disabled = true;
							}
							if (pr_options[id][k]['type'] == 'R') {
								document.getElementById(pr_options[id][k]['id']).getElementsByTagName("INPUT")[j].disabled = true;
							}
						}
					}
				}
			}
		}
	}

	// The sections fills the selectboxes with values considering excepted variants
	for (m = 0; m < pr_options[id].length; m++) {
		j = 0;
		for (var k = 0; k < pr_options[id][m]['variants'].length; k++) {
			// If this variant shouldn't be hided and this option is selectbox than add this variant
			if ((pr_options[id][m]['variants'][k]['hide'] != 'Y') && (pr_options[id][m]['type'] == 'S')) {
				document.getElementById(pr_options[id][m]['id']).options[j] = new Option(pr_options[id][m]['variants'][k]['variant_name'], pr_options[id][m]['variants'][k]['variant_id']);
				// Check if this variants was selected before the check exceptions function called
				if (pr_options[id][m]['selected_value'] == pr_options[id][m]['variants'][k]['variant_id']) {
					document.getElementById(pr_options[id][m]['id']).options[j].selected = true;
				}
				j ++;
			}
			pr_options[id][m]['variants'][k]['hide'] = '';
		}
	}
}

function fn_calculate_matches(id)
{
	// Calculate the mathes between exceptions and selected variants at the moment
	except[id] = new Array;
	exceptnone[id] = new Array;
	if (exception_style == 'warning') {
		document.getElementById(id+'_warning').style.display='none';
	}
	for (var m = 0; m < exceptions[id].length; m++) {
		k = 0;
		n = 0;
		except[id][m] = 0;
		exceptnone[id][m] = 0;
		for (var k = 0; k < pr_options[id].length; k++) {
			if (pr_options[id][k]['selected_value'] == exceptions[id][m][k]) {
				n ++; 
				except[id][m] = n;
			}
			if (exceptions[id][m][k] == '-1') {
				n ++; 
				except[id][m] = n;
			}
			if (exceptions[id][m][k] == '-2') {
				exceptnone[id][m] += 1;
			}
	    }
		if (except[id][m] == pr_options[id].length) {
			if (exception_style == 'warning') {
				document.getElementById(id+'_warning').style.display='';
				fn_empty_selectboxes(id);
				fn_disable(id);
				return false;	
			}
			// Change selected variants to avoid exception
			fn_all_excepted(id);
		}
	}
}
//
// Cleans all values from selectboxes and enables all options
//
function fn_empty_selectboxes(id) 
{
	for (i = 0; i < pr_options[id].length; i++) {
		if (pr_options[id][i]['type'] == 'S') {
			if (exception_style != 'warning') {
				document.getElementById(pr_options[id][i]['id']).options.length = 0;
			}
		   document.getElementById(pr_options[id][i]['id']).disabled = false;
		} else if (pr_options[id][i]['type'] == 'C') {
			document.getElementById(pr_options[id][i]['id']).disabled=false;	
		} else if (pr_options[id][i]['type'] == 'R'){
			for (var k = 0; k < document.getElementById(pr_options[id][i]['id']).getElementsByTagName("INPUT").length; k++) {
			   document.getElementById(pr_options[id][i]['id']).getElementsByTagName("INPUT")[k].disabled = false;
		   }
		}
	}
}

//
// Disables options if the the other exception variants are selected and the option has "disable" status
//
function fn_disable(id) 
{
	for (i = 0; i < exceptions[id].length; i++) {
		if (except[id][i] == pr_options[id].length - exceptnone[id][i]) {
			for (var k = 0; k < pr_options[id].length; k++) {
				if (exceptions[id][i][k] == '-2') {
					if (pr_options[id][k]['type'] == 'S') {
						document.getElementById(pr_options[id][k]['id']).disabled = true;
					} else if (pr_options[id][k]['type'] == 'C') {	
						document.getElementById(pr_options[id][k]['id']).disabled = true;
					} else if (pr_options[id][k]['type'] == 'R'){
						for (var j = 0; j < document.getElementById(pr_options[id][k]['id']).getElementsByTagName("INPUT").length; j++) {
							document.getElementById(pr_options[id][k]['id']).getElementsByTagName("INPUT")[j].disabled = true;
						}				
					}
				}
			}
		}
	}
}

//
// Function that called if curent selected values are in exception
//
function fn_all_excepted(id)
{
	for (var i = 0; i < exceptions[id].length; i++) {
		if (except[id][i] == pr_options[id].length) {
			// if option has -disregard- exception variant
			if (exceptions[id][i][dni]=='-1') {
				  dni ++;
				  ind = 0;
			}
			if (pr_options[id][dni]['type'] == 'S') {
				pr_options[id][dni]['variants'][ind]['hide'] = 'Y';
			} else if (pr_options[id][dni]['type'] == 'C') {	
				document.getElementById(pr_options[id][dni]['id']).checked=true;
			} else if (pr_options[id][dni]['type'] == 'R') {
				document.getElementById(pr_options[id][dni]['id']).getElementsByTagName("INPUT")[ind].disabled = true;				
			}			

			// Next option variant index is selected else Change option
			if ( ind != pr_options[id][dni]['variants'].length-1) {
				ind++;
			} else {
				if (pr_options[id][dni]['type'] == 'S') {
					pr_options[id][dni]['selected_value'] = pr_options[id][dni]['variants'][0]['variant_id'];
				} else if (pr_options[id][dni]['type'] == 'C') {
					document.getElementById(pr_options[id][dni]['id']).disabled=false; 
					document.getElementById(pr_options[id][dni]['id']).checked=false;
				} else if (pr_options[id][dni]['type'] == 'R') {
					document.getElementById(pr_options[id][dni]['id']).getElementsByTagName("INPUT")[0].checked = true;		
				}
				ind = 0;
				dni ++;
			}

			// This section select the next variant in the option
			if (pr_options[id][dni]['type'] == 'S') {
				pr_options[id][dni]['selected_value'] = pr_options[id][dni]['variants'][ind]['variant_id'];
			} else if (pr_options[id][dni]['type'] == 'C') {
				document.getElementById(pr_options[id][dni]['id']).checked=true
				document.getElementById(pr_options[id][dni]['id']).disabled=true;
				pr_options[id][dni]['selected_value'] = pr_options[id][dni]['variants'][1]['variant_id'];
				document.getElementById('unchecked_'+pr_options[id][dni]['id']).value = pr_options[id][dni]['selected_value'];
			} else if (pr_options[id][dni]['type'] == 'R') {
				document.getElementById(pr_options[id][dni]['id']).getElementsByTagName("INPUT")[ind].checked = true;
				pr_options[id][dni]['selected_value'] = pr_options[id][dni]['variants'][ind]['variant_id'];
			}

		}
	}
	fn_calculate_matches(id);
}


