﻿addLoadListener(init);

//==================================
//INITIATE FUNCTION
//==================================
function init()
{
	clearChecks();
    //alert("working");
  return true;
};


//==================================================================//
//INTEREST FILTER FUNCTION
//=================================================================//
function interestFilter(){
//Get all spans on page
var spans = document.getElementsByTagName("span");	
//Get all hidden fields on page
var hiddenFields = document.getElementsByName("hdnField");


//Loop through all the checkboxes and assign selected to variables
//Test against variables
//If INTEREST group ad spans to the page based on value in 'title' of hidden field
//InterestID = 'title' value
//If AGE group ad spans to the page based on value in 'id' of hidden field
//ageID = 'id' value
//If GENDER group ad spans to the page based on value in 'value' of hidden field
//Gender = 'value' 

        //////////////////////////////////
       //Test which gender is chosen
       /////////////////////////////////
	   
	   	//Get gender checkboxes
        var radioBoxes = document.forms["appForm"].radioGender;
			 
	    //Assign checked box value
        if (radioBoxes[0].checked == true){
	           var checkedGender = "male";
	        }
	    if (radioBoxes[1].checked == true){
	           var checkedGender = "female";
	        }
	    if (radioBoxes[0].checked == true && radioBoxes[1].checked == true){
	           var checkedGender = "both";
	        }
		if (radioBoxes[0].checked == false && radioBoxes[1].checked == false){
				var checkedGender = "none";
			}
			
		
	  //////////////////////////////////////             
	 //Check which AGE ID's are checked
	 /////////////////////////////////////
	var ageCheckBoxes = document.forms["appForm"].ageGroup;
	var  checkedAgeIds = [];
	var ageCount = 0;
	 //Loop through checkboxes
     //Add checked ageID's to the array
    for (i=0;i<ageCheckBoxes.length;i++){
           if (ageCheckBoxes[i].checked == true){
	           var ageCheckBoxId = ageCheckBoxes[i].getAttribute("title");
	             //Add checked attribute IDs to array
	               checkedAgeIds[i] = ageCheckBoxId;
	               ageCount += 1;
	              }else {
	                checkedAgeIds[i] = "";
	            }
    } 


	
	 /////////////////////////////////////////////
	 //Check which INTEREST ID's are checked
	 /////////////////////////////////////////////    
	  var intCheckBoxes = document.forms["appForm"].interestGroup; 
	  var  checkedIntIds = [];
	  var intCount = 0;
	//Loop through checkboxes
    //Add checked interstID's to the array
  for (i=0;i<intCheckBoxes.length;i++){
           if (intCheckBoxes[i].checked == true){
	           var intCheckBoxId = intCheckBoxes[i].getAttribute("title");
	             //Add checked attribute IDs to array
	               checkedIntIds[i] = intCheckBoxId;
	               intCount += 1;
	              }else {
	                checkedIntIds[i] = "";
	            }
   }  
   
   
/////////////////////////////////////////////
//Testing function
//Loop through everything if all checkboxes equal hidden field values
//Turn on 
//Otherwise turn off
/////////////////////////////////////////////
                
for (var i=0;i<hiddenFields.length;i++){

            var modelsInterestValues = hiddenFields[i].getAttribute("title");
            var modelsInterestIds = modelsInterestValues.split("");
            var modelsAgeValue = hiddenFields[i].getAttribute("id");
            var modelsGenderValue = hiddenFields[i].getAttribute("value");
			
			
			
           
             for(var j=0;j<checkedAgeIds.length;j++){
                  for (var k=0;k<checkedIntIds.length;k++){
                            for (var l=0;l<modelsInterestIds.length;l++){
								
				//Test NOTHING SELECTED		
					if(checkedGender == "none" && ageCount == 0 && intCount == 0){
										  	hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
									}
								
					//Test GENDER only selected
					if (checkedGender == modelsGenderValue && ageCount == 0 && intCount == 0){
                                    	
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
									
					//Test if BOTH GENDERS selected and nothing else
                    if (checkedGender == "both" && ageCount == 0 && intCount == 0){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
									
									
					 //Test if BOTH GENDERS selected and AGE selected
	                   if (checkedGender == "both" && checkedAgeIds[j] == modelsAgeValue && intCount == 0){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }				
					
					 //Test if BOTH GENDERS selected and INTEREST selected
	                   if (checkedGender == "both" && checkedIntIds[k] == modelsInterestIds[l] && checkedAgeIds[j] == modelsAgeValue){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }	
									
					 	//Test if BOTH GENDERS INTEREST and AGE are selected
	                   if (checkedGender == "both" && checkedIntIds[k] == modelsInterestIds[l] && ageCount == 0){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
						
						
					//Test AGE only selected
					if (checkedAgeIds[j] == modelsAgeValue && checkedGender == "none" && intCount == 0){
                                    	
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
									
					//Test INTEREST only selected
					if (checkedIntIds[k] == modelsInterestIds[l] && checkedGender == "none" && ageCount == 0){
                                    	
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }	
					
                            
                    //Test GENDER and AGE selected NO INTEREST
                    if (checkedAgeIds[j] == modelsAgeValue && checkedGender == modelsGenderValue && intCount == 0){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
									
					//Test GENDER and INTEREST selected NO AGE
                    if (checkedGender == modelsGenderValue && checkedIntIds[k] == modelsInterestIds[l] && ageCount == 0){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
									
					//Test AGE and INTEREST selected NO GENDER
                    if (checkedAgeIds[j] == modelsAgeValue && checkedIntIds[k] == modelsInterestIds[l] && checkedGender == "none"){
                                    
                                            hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                 
	                                         j = 5;  
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                    }
									
									
                    //Test ALL SELECTED
                    if (checkedAgeIds[j] == modelsAgeValue && checkedIntIds[k] == modelsInterestIds[l] && checkedGender == modelsGenderValue){
                                             hiddenFields[i].className = "selected";//Change the hidden fields class name 
	                                         var parentSpan = hiddenFields[i].parentNode;
	                                         parentSpan.className = "selected";
                        	                 //This breaks out the loop if condition is true                                                                  
	                                         j = 5; 
	                                         k = 5;
	                                         l = 10;
	                                         break;
                                        }
										
			
					//This turns spans off if nothing above satisfied
					else {
											  hiddenFields[i].className = "notSelected";//Change the hidden fields class name 
											  var parentSpan = hiddenFields[i].parentNode;
											  parentSpan.className = "notSelected";
                                        }
                        
                                }
                        }
             
                }
//END HIDDEN FIELD LOOP
 }        
//END INTTEREST FILTER FUNCTION
};

//==================================================================//
//RESET BUTTON
//=================================================================//
function clearChecks(){
	var radioBoxes = document.forms["appForm"].radioGender;
	var ageCheckBoxes = document.forms["appForm"].ageGroup;
	var intCheckBoxes = document.forms["appForm"].interestGroup; 
	var selectBoxes = document.getElementsByName("selectMeGroup");
	
	//Get all spans on page
	var spans = document.getElementsByTagName("span");	

	for (var i=0;i<ageCheckBoxes.length;i++){
		ageCheckBoxes[i].checked = false;
	}
	for (var j=0;j<selectBoxes.length;j++){
		selectBoxes[j].checked = false;
	}
	for (var k=0;k<intCheckBoxes.length;k++){
		intCheckBoxes[k].checked = false;
	}
	for (var l=0;l<radioBoxes.length;l++){
		radioBoxes[l].checked = false;
	}
	
	//Turn everything on
	for (var i=0;i<spans.length;i++){
						spans[i].className = "selected";//Change the hidden fields class name 
								//var parentSpan = hiddenFields[i].parentNode;                
					}
	
}



//=============================
//COMPCARD LINK FUNCTION
//=============================
function openImageWindow(modelID){
		var myWindow = window.open('model.aspx', 'modelPage', 'width=880,height=760' 
								   + 'scollbars=yes,resizable=no,status=yes');
		myWindow.document.location = "model.aspx?Model=" + modelID;
}

//=============================
//OPEN NEW WINDOW FUNCTION
//=============================
function openNewWindow(url){
		window.open(url, 'newwindow', 'width=540,height=760,scollbars=yes,resizable=no,status=yes');
		//myWindow.document.location = "model.aspx?Model=" + modelID;
}


//==================================================================//
//REQUEST MODELS FUNCTION
//=================================================================//
function checkedModels () {
var selectBoxes = document.getElementsByName("selectMeGroup");
var chosenCount = 0;

var allModels = "";
for (var i=0;i<selectBoxes.length;i++){
    if (selectBoxes[i].checked == true){
        var selectedModel = selectBoxes[i].getAttribute("title");
        chosenCount += 1;
        
       allModels = allModels + selectedModel + "&";
    }
}

if (chosenCount <= 0) {
    alert("WOOPS! You have not chosen any models....");

}
if (chosenCount > 0) {
    //alert(allModels.toString());
    allModels = allModels.slice(0, -1)
    location.href = "model-request.aspx?" + allModels;
}


 

};



//==================================================================//
//FRONT IMAGE SLIDESHOW x 2
//=================================================================//

var slideimages=new Array()
var slidelinks=new Array()
function slideshowimages(){
for (i=0;i<slideshowimages.arguments.length;i++){
slideimages[i]=new Image()
slideimages[i].src=slideshowimages.arguments[i]
}
}

function slideshowlinks(){
for (i=0;i<slideshowlinks.arguments.length;i++)
slidelinks[i]=slideshowlinks.arguments[i]
}

function gotoshow(){
if (!window.winslide||winslide.closed)
winslide=window.open(slidelinks[whichlink])
else
winslide.location=slidelinks[whichlink]
winslide.focus()
}

var slideimages2 = new Array()
var slidelinks2 = new Array()
function slideshowimages2() {
    for (i = 0; i < slideshowimages2.arguments.length; i++) {
        slideimages2[i] = new Image()
        slideimages2[i].src = slideshowimages2.arguments[i]
    }
}

function slideshowlinks2() {
    for (i = 0; i < slideshowlinks2.arguments.length; i++)
        slidelinks2[i] = slideshowlinks2.arguments[i]
}

function gotoshow2() {
    if (!window.winslide || winslide.closed)
        winslide = window.open(slidelinks2[whichlink2])
    else
        winslide.location = slidelinks2[whichlink2]
    winslide.focus()
}





//================================================
//LOAD FUNCTION - ALLOWS MULTIPLE FUNCTIONS TO RUN
//================================================
function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
};

//===============================================
//TESTING FUNCTIONS
//===============================================
function writeToPage(input){

    	var test = document.getElementById("testDiv");
    	test.innerHTML += '<ul>';
        for (i=0;i<input.length;i++){ 
    		    test.innerHTML += '<li>' + i + '=' +  input[i] + '</li>'; 
    		} 
    	test.innerHTML += '</ul>'; 
}

//Test function to produce an alert based on input 
function test(string) {alert(string);return true;};
