// JavaScript functions to change edit status of web form controls
// and popup functions  
 
function PopUpProof(element) {
    var newPopUpWindow = PopupWindow('', 400, 900, 'yes', 'popUpProof')

    var theForm = document.forms[0];
    theForm.target= "popUpProof";
    theForm.method = "post";

    document.getElementById("ctl00_cph1_hdnPreview").value = 'PopUp';
    var strViewStateVal = theForm.__VIEWSTATE.value
    theForm.__VIEWSTATE.value = '';
    //theForm.__VIEWSTATE.name = '';
    //theForm.__VIEWSTATE.disabled = true ;
    theForm.action = "RenderCocktailMenu.aspx"
  
    theForm.submit();
    theForm.__VIEWSTATE.value = strViewStateVal;
} 
 
 
function updatePriceCurrency(curvalue) {
	for(intCounter=0;intCounter<=9;intCounter++) {

		var spnPrice = document.getElementById("ctl00_cph1_C_" + intCounter + "_spnPrice");
		//alert(spnPrice.id)
		spnPrice.innerHTML = curvalue
		}
}
 
 
function setValueFromPopup(poz,optionNumber) {
	theForm = document.forms[0];
	var itemCategory = theForm.elements[poz-2][theForm.elements[poz-2].selectedIndex].text;
	
	theForm.elements[poz-1].options[optionNumber+1].selected = true;
	//alert(theForm.elements[poz-1].id)
	
	theForm.elements[poz+1].value = arrCocktailNames[itemCategory][optionNumber]
	theForm.elements[poz+1].readOnly = true;
	theForm.elements[poz+4].value = arrCocktailDescriptions[itemCategory][optionNumber]
	theForm.elements[poz+4].readOnly = true;	
	theForm.elements[poz+3].checked = false;
}


function allowEdit(element) {
	var poz = GetFormArrayIndex(element);
	var theForm = document.forms[0];

	if (!theForm.elements[poz-4].selectedIndex-1) {					
		if (theForm.elements[poz+1].readOnly) {
			theForm.elements[poz-2].readOnly = false;
			theForm.elements[poz+1].readOnly = false;
			
			theForm.elements[poz-2].style.backgroundColor = "white";
			theForm.elements[poz+1].style.backgroundColor = "white"
		}else{
			theForm.elements[poz-2].readOnly = true;
			theForm.elements[poz+1].readOnly = true;				
			theForm.elements[poz-2].style.backgroundColor = "#ebebe4";
			theForm.elements[poz+1].style.backgroundColor = "#ebebe4"


		}
	}else {
		theForm.elements[poz].checked = true;
		theForm.elements[poz+1].style.backgroundColor = "white"		
	}
}
 




function PopupScrollWindow(width,height,strUrl) {
	var PopupWin = window.open(strUrl,"SutchandSutch","scrollbars=yes,toolbar=no,directories=no,status=no,menubar=no,resizable=no,width=" + width + ",height=" + height)
	PopupWin.focus();
	
}



function updateCocktailList(element) {

	var poz = GetFormArrayIndex(element);
	var theForm = document.forms[0];
	var ddlCats = element
    var selectedValue = element.options[element.selectedIndex].value	
	var ddlCocktails = theForm.elements[poz+1];

	ddlCocktails.options.length = 0; // clear options list
	selectOption0 = new Option("Select Cocktail >>", ""); //Create new item
	ddlCocktails.options[ddlCocktails.options.length] = selectOption0; 	// Add to list

	// check if options array index is 0
	if(ddlCats.selectedIndex != 0 ) {
		// loop through selected array and add to options list 
		for(var i = 0; i < arrCocktailNames.length; i++) {
		    if (arrCocktailCategoryIds [i] == selectedValue) {
			    // Create new option item for each item in the array
			    var selectOption;
			    selectOption = new Option(arrCocktailNames[i], arrCocktailIds[i]);
			    // Add to list
			    ddlCocktails.options[ddlCocktails.options.length] = selectOption;		    
		    }
		}
	}else {
		theForm.elements[poz+4].checked = true;		
		theForm.elements[poz+3].value = "";
		theForm.elements[poz+3].readOnly = false;
		theForm.elements[poz+3].style.backgroundColor = "white";		
		theForm.elements[poz+5].value = "";
		theForm.elements[poz+5].readOnly = false;	
		theForm.elements[poz+5].style.backgroundColor = "white";		
	}
}

function updateCocktailFields(element) {
	var poz = GetFormArrayIndex(element);
	var theForm = document.forms[0];
	var txtCocktail = theForm.elements[poz+1];
    var txtPrice = theForm.elements[poz+2];
    var txtDescription = theForm.elements[poz+3];
    var selectedValue = element.options[element.selectedIndex].value;

    if (element.selectedIndex == 0) {
        txtCocktail.value = "cocktail name"
        //txtCocktail.readOnly = false
        //txtCocktail.style.backgroundColor = "white";
        txtDescription.value  = "description"
        //txtDescription.readOnly  = false 
        //txtDescription.style.backgroundColor = "white";
        //cbxCustom.checked = true 
        txtPrice.value = "price";     
    }

	for(var i = 0; i < arrCocktailNames.length; i++) {
        if (arrCocktailIds[i] == selectedValue) {
            txtCocktail.value  = arrCocktailNames[i]
            txtDescription.value  = arrCocktailDescriptions[i]
            //txtCocktail.readOnly = true
            //txtCocktail.style.backgroundColor = "#ebebe4";
            //txtDescription.readOnly  = true
            //txtDescription.style.backgroundColor = "#ebebe4";
            //cbxCustom.checked = false
        }
	}	    		
}


function SetCocktailViewstate() {
	var theForm = document.forms[0];
	var intCounter = 1;
	var poz = -1
	var selectedOptionText

	for(intCounter=0;intCounter<=9;intCounter++) {

		ddlCats = document.getElementById("ctl00_cph1_C_" + intCounter + "_ddlCats");
		cocktailsDropdown = document.getElementById("ctl00_cph1_C_" + intCounter + "_ddlDrinks");
		currentCat = ddlCats[ddlCats.selectedIndex].value;
		currentCocktail = document.getElementById("ctl00_cph1_currentCocktailDdl_" + intCounter).value;

		//Add default item
		cocktailsDropdown.options.length = 0; // clear options list
		selectOption0 = new Option("Select Cocktail >>", ""); //Create new item
		cocktailsDropdown.options[cocktailsDropdown.options.length] = selectOption0; // Add to list


        //Add items if they match cat array
		for(var i = 0; i < arrCocktailNames.length; i++) {
		    if (arrCocktailCategoryIds [i] == currentCat) {
			    // Create new option item for each item in the array
			    var selectOption;
			    selectOption = new Option(arrCocktailNames[i], arrCocktailIds[i]);
			    // Add to list
			    cocktailsDropdown.options[cocktailsDropdown.options.length] = selectOption;		    
		    }
		}        

		//set the ddl selected value from the hidden field
		for(var i = 0; i < cocktailsDropdown.options.length; i++) {
					
			if (currentCocktail == cocktailsDropdown.options[i].value) {
				cocktailsDropdown.selectedIndex = i;	
			}
		}

		if (ddlCats.selectedIndex == "0") {
		    if  (!(currentCocktail == "")) {   		    
                //Add items if they match cat array
		        for(var i = 0; i < arrCocktailNames.length; i++) {
        		     
		            if (arrCocktailIds [i] == currentCocktail) {
		                cocktailsDropdown.options.length = 0; // clear options list
			            // Create new option item for each item in the array
			            var selectOption;
			            selectOption = new Option(arrCocktailNames[i], arrCocktailIds[i]);
			            // Add to list
			            cocktailsDropdown.options[cocktailsDropdown.options.length] = selectOption;		    
		            }
		        }
		    } 
        }		             		    
	}
}

//		//Add default item
//		cocktailsDropdown.options.length = 0; // clear options list
//		selectOption0 = new Option("Select Cocktail >>", ""); //Create new item
//		cocktailsDropdown.options[cocktailsDropdown.options.length] = selectOption0; // Add to list


//        //Add items if they match cat array
//		for(var i = 0; i < arrCocktailNames.length; i++) {
//		    if (arrCocktailCategoryIds [i] == currentCat) {
//			    // Create new option item for each item in the array
//			    var selectOption;
//			    selectOption = new Option(arrCocktailNames[i], arrCocktailIds[i]);
//			    // Add to list
//			    cocktailsDropdown.options[cocktailsDropdown.options.length] = selectOption;		    
//		    }
//		}        

//		//set the ddl selected value from the hidden field
//		for(var i = 0; i < cocktailsDropdown.options.length; i++) {
//					
//			if (currentCocktail == cocktailsDropdown.options[i].value) {
//				cocktailsDropdown.selectedIndex = i;	
//			}
//		}	


function updateLogosDropDown(fileString, idString) {
    var arrFileNames = fileString.split(/,/); 
	var arrIds = idString.split(/,/); 	
	var theForm = document.forms[0];
	var ddlLogos = theForm.ctl00_cph1_lstLogos;

	ddlLogos.options.length = 0; // clear options list
	selectOption0 = new Option("No Logo", "No Logo"); //Create new item
	ddlLogos.options[ddlLogos.options.length] = selectOption0; // Add to list


	// loop through selected array and add to options list
	for(var i = 0; i < arrFileNames.length; i++) {
		if (!arrFileNames[i] == "") {
			var selectOption; // Create new option item for each item in the array
			selectOption = new Option(arrFileNames[i],arrIds[i]);
			ddlLogos.options[ddlLogos.options.length] = selectOption; // Add to list
		}	
	}	
}



function setSelectedLogo(strLogo) {
	logosDdl = $("ctl00_cph1_lstLogos");
	for(var i = 0; i < logosDdl.options.length; i++) {
		if (strLogo == logosDdl.options[i].value) {
			logosDdl.selectedIndex = i;
			$("ctl00_cph1_selectedLogo").value = logosDdl.options[i].value
		}
	}	
}

function updateHiddenLogo(strLogo) {
	//put the ddl value into hidden form field
	var selectedLogo = $("ctl00_cph1_selectedLogo");
	selectedLogo.value = strLogo
}


function changePageTab(tabNumber) {	
	document.getElementById("tab1").style.backgroundImage = "url(/images/MenuMaker/Tab.gif)";
	document.getElementById("tab2").style.backgroundImage = "url(/images/MenuMaker/Tab140.gif)";
	document.getElementById("tab3").style.backgroundImage = "url(/images/MenuMaker/Tab140.gif)";
	document.getElementById("tab4").style.backgroundImage = "url(/images/MenuMaker/Tab140.gif)";
	if (tabNumber == 'tab2' || tabNumber == 'tab3' || tabNumber == 'tab4') {
		document.getElementById(tabNumber).style.backgroundImage = "url(/images/MenuMaker/Tab140_Over.gif)";
	}else{
	document.getElementById(tabNumber).style.backgroundImage = "url(/images/MenuMaker/Tab_Over.gif)";
	}
}	

// fires on form submit	
function formSubmit() {
    var theForm = document.forms[0];
    theForm.target= "_self"
    theForm.action = "default.aspx";
    theForm.method = "post";
}

function submitIframe() {
	var theForm = document.forms[0];
    iframe1.src = "RenderCocktailMenu.aspx"
    document.getElementById("ctl00_cph1_hdnPreview").value = 'true';
    theForm.target= "iframe1";
    theForm.method = "post";
    var strViewStateVal = theForm.__VIEWSTATE.value
    theForm.__VIEWSTATE.value = '';
    //theForm.__VIEWSTATE.name = '';
    //theForm.__VIEWSTATE.disabled = true ;
    theForm.action = "RenderCocktailMenu.aspx"
    


    theForm.submit();
    theForm.__VIEWSTATE.value = strViewStateVal;
}





function submitSelf() {
	var theForm = document.forms[0];
    document.getElementById("ctl00_cph1_hdnPreview").value = 'false';
    theForm.target= "_self";
    theForm.method = "post";
    var strViewStateVal = theForm.__VIEWSTATE.value
    theForm.__VIEWSTATE.value = '';
    //theForm.__VIEWSTATE.name = '';
    //theForm.__VIEWSTATE.disabled = true ;
    theForm.action = "RenderCocktailMenu.aspx"
    theForm.submit();
    theForm.__VIEWSTATE.value = strViewStateVal;
}

function submitSelfRecipe() {
	var theForm = document.forms[0];
    document.getElementById("ctl00_cph1_hdnPreview").value = 'false';
    theForm.target= "_self";
    theForm.method = "post";
    var strViewStateVal = theForm.__VIEWSTATE.value
    theForm.__VIEWSTATE.value = '';
    //theForm.__VIEWSTATE.name = '';
    //theForm.__VIEWSTATE.disabled = true ;
    theForm.action = "CreateRecipeCard.aspx"
    theForm.submit();
    theForm.__VIEWSTATE.value = strViewStateVal;
}


function scrollIframe() {	
    var imagePos = 230;	
	currentScrollPos = document.documentElement.scrollTop
	menuPreview = document.getElementById('ctl00_cph1_IFrameDiv')
    if (GetWindowHeight() > 640) {
	    var browseHeight;
	    if (currentScrollPos > imagePos) {			
		    newImagePos = (currentScrollPos + 5) + 'px'; 
		    menuPreview.style.top = newImagePos;
	    }else{
		    newImagePos = imagePos;
		    strPxVal = newImagePos + 'px'
		    menuPreview.style.top = strPxVal
	    }	
    }	
}

window.onscroll = scrollIframe;
window.onkeyup = scrollIframe;
window.onclick = scrollIframe;

var intCurIndex

function PopupScroll(cocktailIndex) {
    intCurIndex = cocktailIndex
    var strUrl = "/cocktail-recipes/cocktail-recipe-search.aspx?mm=True"
    var PopupWin = window.open(strUrl,"barmenus","scrollbars=yes,toolbar=no,directories=no,status=no,menubar=no,resizable=no,width=1050,height=800");
    PopupWin.focus();
}

//function setDdlFromPopup(drinkId) {
//    var txtCocktail = $("ctl00_cph1_C_" + intCurIndex + "_name")
//    var txtDescription = $("ctl00_cph1_C_" + intCurIndex + "_des")

//	for(var i = 0; i < arrCocktailNames.length; i++) {
//        if (arrCocktailIds[i] == drinkId) {
//            txtCocktail.value  = arrCocktailNames[i]
//            txtDescription.value  = arrCocktailDescriptions[i]
//        }
//	}	    		

//}

function setDdlFromPopup(drinkId) {
    var txtCocktail = $("ctl00_cph1_C_" + intCurIndex + "_name");
    var txtDescription = $("ctl00_cph1_C_" + intCurIndex + "_des");
	var ddlCats = $("ctl00_cph1_C_" + intCurIndex + "_ddlCats");
	var ddlDrinks = $("ctl00_cph1_C_" + intCurIndex + "_ddlDrinks");

    ddlCats[ddlCats.selectedIndex].value = -1;

	for(var i = 0; i < arrCocktailNames.length; i++) {
        if (arrCocktailIds[i] == drinkId) {
            txtCocktail.value  = arrCocktailNames[i];
            txtDescription.value  = arrCocktailDescriptions[i];
            
          	ddlDrinks.options.length = 0; // clear options list
        	var selectOption = new Option(arrCocktailNames[i], arrCocktailIds[i]); //Create new item;
	        ddlDrinks.options[ddlDrinks.options.length] = selectOption; // Add to list
	        
	        document.getElementById("ctl00_cph1_currentCocktailDdl_" + intCurIndex).value = arrCocktailIds[i];
        }
	}	    		

}