/* 
Author: Robbie Mast en Bas van Elburg
Purpose of script: This Script determine if questions on a contract should be visable or invisable. 
Important: This script uses the Mootools framework.
*/

//Stores all dependencies from the invisible input field with id “DynamicContentList” to a Array.
function buildDependencies()
{
    var dependencyArray = new Array();
    var list = document.getElementById("DynamicContentList");

    if (list!=null)
    {            
        for (var i=0;i<list.length;i++)
	    {
	        dependencyArray[dependencyArray.length] = list.options[i].text.toUpperCase().split(";");
	    }
    }
    return dependencyArray;   
}

//Stores all DOM elements which contains the id value of “InsOption_” to a Array.
function getOptionElements()
{
    var arrayOptionElements = new Array();
    var elementList = $$('*');
          
    for(var i=0;i<elementList.length;i++)
    {
        var elementHasName = $chk(elementList[i].name);
                
        if(elementHasName == true)
        {
            var indexOfInsOption = elementList[i].name.indexOf("InsOption_");
            var indexOfInsOptionDay = elementList[i].name.indexOf("Day");
            var indexOfInsOptionMonth = elementList[i].name.indexOf("Month");
            var indexOfInsOptionYear = elementList[i].name.indexOf("Year");
            
            if(indexOfInsOption == 0 && indexOfInsOptionDay ==-1 && indexOfInsOptionMonth ==-1 && indexOfInsOptionYear ==-1)
            {
                arrayOptionElements[arrayOptionElements.length] = elementList[i].name.substr(10, 6);
            }
            else
            {
                //do nothing.
            }                                    
        }                        
    }
    return arrayOptionElements;
}

function getInsOption(elm_i)
{
	var insOption = document.getElementsByName(elm_i)[0];
	return insOption;	
}

// This is de main function that initializes the dependencies from the do_dependencyArray to the webpage.
function do_init() {

	do_dependencyArray = buildDependencies();
	do_arrayOptionElements = getOptionElements();
	do_updateStatus();
		
	do_arrayOptionElements.each(function(el) {
		var elementName = '';
		if($chk(getInsOption('InsOption_'+el+'DateField'))) 
		{
			elementName='InsOption_'+el+'DateField';
		}
		else if($chk(getInsOption('InsOption_'+el))) 
		{ 
		    elementName = 'InsOption_'+el;
		}
		else
        {
        	elementName = 'undefined';
        }
		//Ceck for empty elementName	
		if(elementName == 'undefined')
		{
			//Do Nothing.	
		}
		else
		{
			getInsOption(elementName).addEvent('change',function(){ do_updateStatus();});
			getInsOption(elementName).addEvent('keyup',function(){ do_updateStatus();});
			getInsOption(elementName).addEvent('DIVOptionChangeEvent',function(){ do_updateStatus();});
		}		
	});	
}

// Update the visible or invisible status.
function do_updateStatus() 
{
	do_arrayOptionElements.each(function(el) 
	{
		do_setStatus(el);
	});
}

// Sets the visible or invisible status.
function do_setStatus(elem) 
{
	result = do_getVisibility(elem);
	if($chk($('InsOptionRow_'+elem)))
	{
	    $('InsOptionRow_'+elem).style.display = (result?'':'none');
	}
	else
	{
	    //do nothing
	}
}

function do_getVisibility(elm_i)
{
    var visibility = true; //Status of the childID visibility.
    var visibilityOfInsOptionRow; //Visibility status of the InsOptionRow
    var parentID; //ParentID to determines which InsOptionRow should not be visable.
    var loopready = false; //Loopready determines if de forloop should continue to run.
    var elementName = '';
        
    for(var i = 0; i<do_dependencyArray.length && loopready == false; i++) //Loop through the do_linkArray.
    {
        if(do_dependencyArray[i][3] == elm_i)//Check if the dom element is a Chil in de do_linkArray.
        {
           parentID = do_dependencyArray[i][0];
           var parentOptionRow = $('InsOptionRow_'+parentID);
           visibilityOfInsOptionRow = 'none';

           if (parentOptionRow != null)
           {
			visibilityOfInsOptionRow = (parentOptionRow.getStyle('display')); //Block = visable
           }
         
           if(visibilityOfInsOptionRow == 'none') //Check the visibility status of the InsOptionRow.
           {
                visibility = false; //If the visibility status is equal to "none" than the Child should not be visable. 
           }
           else
           {    
	            //determine the name for the Parent element.
                if($chk(getInsOption('InsOption_'+do_dependencyArray[i][0]+'DateField')))
				{
				    elementName='InsOption_'+do_dependencyArray[i][0]+'DateField';
				} 
				else if($chk(getInsOption('InsOption_'+do_dependencyArray[i][0])))
				{ 
					elementName = 'InsOption_'+do_dependencyArray[i][0];
				}
				var optionElement = getInsOption(elementName);
				var optionValue = optionElement.value;
				if (optionElement.type=="select-one")
				{
					for (var k=0; k<optionElement.length; ++k)
					{
						if (optionElement.value==optionElement[k].value)
						{
							optionValue = optionElement[k].text;
							break;
						}
					}
				}
				optionValue = optionValue.toUpperCase();
				//Determine if the child element should be visable.
				switch (do_dependencyArray[i][1])
				{
				    case "ISEQUALTO":
				    {
					    if(compareValues(do_dependencyArray[i][2], optionValue)==0)
				        {
				            visibility = true;
				            loopready = true;
				        }
				        else
				        {
				            visibility = false;
				        }
				        break;
				    }
				    case "ISNOTEQUALTO":
					{
						if(compareValues(do_dependencyArray[i][2], optionValue)!=0)
					    {
					    	visibility = true;
					        loopready = true;
					    }
					    else
					    {
					    	visibility = false;
					    }
					    break;
					}
				    case "ISCONTAINING":
				    {
				   
				        if (optionValue.indexOf(do_dependencyArray[i][2])!=-1)
				        {
				            visibility = true;
				            loopready = true;
				        }
				        else
				        {
				            visibility = false;
				        }
				        break;
				    }
				    case "ISLARGERTHEN":
				    {
				        if(compareValues(do_dependencyArray[i][2], optionValue)==1)
				        {
				            visibility = true;
				            loopready = true;
				        }
				        else
				        {
				            visibility = false;
				        }
				        break;
				    }
				    case "ISSMALLERTHEN":
				    {
						if(compareValues(do_dependencyArray[i][2], optionValue)==-1)
				        {
				            visibility = true;
				            loopready = true;
				        }
				        else
				        {
				            visibility = false;
				        }
				        break;
				    }
				    default:
				    {
				        //FOUT!!!
				        break;
				    }
				}
           }                       
        }
        else
        {
            //do nothing!
        }
    }  
    return visibility;
}

function compareValues(v1, v2)
{
    var cmpV1 = parseFloat(v1.replace(".", "").replace(",","."));
    var cmpV2 = parseFloat(v2.replace(".", "").replace(",","."));
    var rv = "";
    
    if(isNaN(cmpV1))
    {   
        cmpV1 = v1;
        cmpV2 = v2;
    }
    else
    {
        if(isNaN(cmpV2))
        {   
            cmpV2 = 0;
        }
    }
            	    
    if (cmpV2==cmpV1)
    {
	    rv = 0;
    }
    else if (cmpV2>cmpV1)
    {
	    rv = 1;
    }
    else if (cmpV2<cmpV1)
    {
	    rv = -1;
    }
    else 
    {
	    rv = 'Fail to compare.';
    }
    return rv;
}