/* CLASSES */
var g_products = null;
var g_combinationsByProduct = new Array();

function avCombinationCache(product, value) {
 this.Product = product;
 this.Value = value;
}

function avGroup(name) {
    this.Name = name;
    this.Values = new Array()
    this.Previous = "";
}

function avCombination(product, name, value) {
    this.Product = product;
    this.Name    = name;
    this.Value   = value;
}

/* FUNCTIONS */
function avInitialize()
{
    avGetProducts();
    avUpdateCombo(0);
}

function avGetParams(combono)
{
    vParams = Array();

    for (vi = 0; vi < combono;vi++) 
    {
        vParams[vi] = document.getElementById('avCombo' + vi).value;
    }
    return vParams;
}

function avHasCombination(group, value)
{
    for (ci = 0; ci < avCombinations.length;ci++)
    {
        if ((avCombinations[ci].Name == group) && (avCombinations[ci].Value == value)) return true;
    }
    
    return false;
}


function avUpdateOptions(combono)
{
    var options = new Array() 

    if (combono == 0)
    {
    for (i = 0; i < avGroups[combono].Values.length;i++)
    {
        if (avHasCombination(avGroups[combono].Name,avGroups[combono].Values[i]))
        {
        
        options.push(avGroups[combono].Values[i])
        
        }
    }
    }
    else 
    {
    params = Array();

    for (i = 0; i < combono;i++)
    {
        params[i] = document.getElementById('avCombo' + i).value;
    }

    for (vi = 0; vi < avGroups[combono].Values.length;vi++)
    {
        x = avGroups[combono].Values[vi];
        params.push(x);
        m = avGetMatches(params,true);
        if (m.length > 0) options.push(x);
        params.pop();
    }

    }
    return options; 
}

function avGetProducts()
{ 
    if (g_products == null) {
     products = Array();
     for (i = 0; i < avCombinations.length;i++)
     {
        exists = false;
        for (p = 0; p < products.length;p++)
        {
        if (products[p] == avCombinations[i].Product) { exists = true; break;}
        }
        if (!exists) products[products.length] = avCombinations[i].Product;
     }
     g_products = products;
    }
    return g_products;
}

function avGetCombinationsByProductFromCache(prodno) {
 for (i = 0; i < g_combinationsByProduct.length; i++) {
  if (g_combinationsByProduct[i].Product == prodno) {
    var cacheObj = g_combinationsByProduct[i];
    if (cacheObj == null) return null;
    return cacheObj.Value;
   }
 }
 return null;
}

function avGetCombinations(prodno)
{
    var cacheCombs = avGetCombinationsByProductFromCache(prodno);
    if (cacheCombs != null) return cacheCombs;
    
    var combs = Array();
    for (ci = 0; ci < avCombinations.length;ci++)
    {
        if (avCombinations[ci].Product == prodno) combs[combs.length] = avCombinations[ci]
    }
    //add to cache
    g_combinationsByProduct[g_combinationsByProduct.length] = new avCombinationCache(prodno, combs);
    
    return combs;
}

function avIsMatch(prodno, params)
{
    hasmatch = true;
    combs = avGetCombinations(prodno);

    for (pa = 0; pa < params.length;pa++)
    {
        ParamName = avGroups[pa].Name;
        ParamValue = params[pa];
        internalmatch = false;
        for (c = 0; c < combs.length;c++)
        {
        if ((combs[c].Name == ParamName) && (combs[c].Value == ParamValue))
        {
            internalmatch = true;
        }
        }

        if (internalmatch == false) {hasmatch = false; break;};
    }
    return hasmatch; 
}


function avGetMatches(params,fast)
{
    var matched = Array(); 
    var prods = Array();

    prods = avGetProducts();
    for (p = 0; p < prods.length;p++)
    {
    if (avIsMatch(prods[p],params))
    {
        matched[matched.length] = prods[p];
        if (fast) return matched;
    }
    }
    return matched; 
}


function avUpdateCombo(combono)
{
    var ElCombo = document.getElementById('avCombo' + combono);

    avClearCombo(combono);
    newoptions = avUpdateOptions(combono);

    for (i = 0; i < newoptions.length;i++)
    {
    avInsertComboValue(combono, newoptions[i]);
    }

    if (document.getElementById('avCombo' + combono).options.length > 1)
    {
    document.getElementById('avCombo' + combono).disabled = false;
        
    }
    

    for (i = combono+1; i < avGroups.length;i++)
    {
    avClearCombo(i);
    }
}


function avInsertComboValue(combono, strvalue)
{
   var ElCombo = document.getElementById('avCombo' + combono);
 	 var optionObject = new Option(strvalue,strvalue)
	 ElCombo.options.add(optionObject);
}

function avComboSelected(combono)
{
    
    if (combono < avGroups.length-1) 
    {
    avUpdateCombo(combono+1);
    }

    if (combono == avGroups.length-1) 
    {
    mp = avGetMatches(avGetParams(combono+1),false);
    if (mp != '') avRedirect(mp);
    }

    UpdateBuyControls();
}

function avSelect(prodnumber)
{
    pcomb = avGetCombinations(prodnumber);

    for (gi = 0; gi < avGroups.length;gi++)
    {
    for (pi = 0; pi < pcomb.length;pi++)
    {
        if (pcomb[pi].Name == avGroups[gi].Name)
        {
            document.getElementById('avCombo' + gi).value = pcomb[pi].Value;
            avComboSelected(gi);
        }
    }
    }
}

function avClearCombo(combono)
{
    var ElCombo = document.getElementById('avCombo' + combono);
    ElCombo.options.length = 0;
    avInsertComboValue(combono,SelectVarTxt)
    ElCombo.disabled = true;
}

function avRedirect(prodno)
{
    if (ProductNumber != prodno)
    { 
    document.getElementById('BUYSECTION').style.visibility = 'hidden';

		if(DontUseFrames=='')
		{
			document.location.href = "/shop/productinfo.asp?id=" + replace(escape(prodno),"+","%2B")
			
		}
		if(DontUseFrames=='1')
		{
			document.location.href = "/shop/JumpPage.asp?action=ProductRedirect&Productnumber=" + replace(escape(prodno),"+","%2B") + "&LangID=" + LanguageID 
		}
    }
}


    function UpdateBuyControls() {

      GoOn = true;
       
	  if(ReqVariantSelect=='true')
	  {
 
		for (i = 0;i < avNumVariants;i++)
		{
			if ( (document.getElementById('avCombo' + i).value == '') || (document.getElementById('avCombo' + i).value == SelectVarTxt) ) {GoOn = false};
		}


		for (i = avNumVariants+1;i < (avNumVariants + svNumVariants+1);i++)
		{
			if ( (document.getElementById('variant' + i).value == '') || (document.getElementById('variant' + i).value == SelectVarTxt) ) 
			{
				GoOn = false;
			};
		}

      }

      if (!loaded) return;
            
      if (GoOn == true) 
      {    
        document.getElementById('BUYSECTION').disabled = false;
        try
        {
            //document.myform.getElementById('amount').disabled = false;
            //document.getElementById('amount').disabled = false;
            document.myform.amount.disabled = false
        }
        catch(e)
        {
        }
        CanBuy = true;
      } 
      else 
      {
        
        document.getElementById('BUYSECTION').disabled = true;
        try
        {
            //document.myform.getElementById('amount').disabled = true;
            //document.getElementById('amount').disabled = true;
            document.myform.amount.disabled = true
        }
        catch(e)
        {
        }
        CanBuy = false;
      }


    }

function SimpleVariantsValidate(MinValue,MaxValue,DefaultValue,RegExStr,ValidateFailText,Field)
{
	try{
		
		var ValidFieldEntry = true;
		
		MinValue = parseInt(MinValue);
		MaxValue = parseInt(MaxValue);
			

		//MaxValue Validation
		if(eval(Field.value > MaxValue) && MaxValue!=0 && MaxValue!='NaN')
		{
			ValidFieldEntry = false
		}
		
		//MinValue Validation
		if(eval(Field.value < MinValue) && MinValue!=0 && MinValue!='NaN')
		{
			ValidFieldEntry = false
		}
		
		//RegEx Validation
		if(RegExStr!='')
		{
			var RegEx = new RegExp(eval(RegExStr));
			if(Field.value.match(RegEx)==null)
			{
			ValidFieldEntry = false
			}
		}
		
		if(ValidFieldEntry==true)
		{
			FormulaCalc()
			UpdateBuyControls();
		}
		else
		{
			Field.value = DefaultValue;
			if(ValidateFailText != '')
			{
				alert(ValidateFailText);
			}
		}
	}
	catch(e)
	{
	alert(e);
	}
}

function FormulaCalc()
{
	
	if(!document.getElementById('FormulaPrice_ProductInfo_SPAN'))
	{
		return;
	}

	try
		{
		var elem = document.myform.elements;
		var emptyfields = false;
		var variantstr = '';
		var amount;
		for(var i = 0; i < elem.length; i++)
		{

			if(elem[i].type =='hidden' && elem[i].name.indexOf("Variantgroup") != -1)
			{
				variantstr = variantstr + elem[i].value + ':';
			}
			
			// Freetext variant
			if(elem[i].type =='text' && elem[i].name.indexOf("variant") != -1)
			{
				variantstr = variantstr + elem[i].value + '_';		
				if(elem[i].value=='')
				{
				emptyfields = true;
				break;
				}
			}
			
			// Simple variant
			if(elem[i].type =='select-one' && elem[i].name.indexOf("variant") != -1)
			{
				var simplevariantValue = elem[i][elem[i].selectedIndex].value;
				variantstr = variantstr + simplevariantValue + '_';		
				if(simplevariantValue=='')
				{
				    emptyfields = true;
				    break;
				}
			}

		} 

		amount = document.getElementById('amount').value;
		if(amount=='')
		{
		emptyfields = true
		}
		
		if(emptyfields == false)
		{	
			var objReq ;
			var poststr;
	   
  			poststr = "FC=GetProdPrice" +
			"&ProdNumber=" + escape( ProductNumber ) +
			"&VariantsStr=" + escape(variantstr) +
			"&Amount=" + amount;
	        				
			// for Mozilla, Firefox, Safari, and Netscape
			if (window.XMLHttpRequest) 
			objReq = new XMLHttpRequest();

			// for Internet Explorer
			if (window.ActiveXObject) objReq = new ActiveXObject("microsoft.xmlhttp");
			
			// set the call back
			objReq.onreadystatechange  = function() 
			{
				if (objReq.readyState == 4)
				{
					document.getElementById('FormulaPrice_ProductInfo_SPAN').innerHTML = objReq.responseText;
				}
			}; 
			objReq.open("GET", "/shop/AjaxWrapper.asp?"+poststr,true);		 
			objReq.send(null);
		}
		else
		{
		document.getElementById('FormulaPrice_ProductInfo_SPAN').innerHTML = '';
		}
	}
	catch(e)
	{
	}
}
