/*
(C) Copyright MarketLive. 2007. All rights reserved.
MarketLive is a trademark of MarketLive, Inc.
Warning:This computer program is protected by copyright law and international treaties.
Unauthorized reproduction or distribution of this program, or any portion of it, may result
in severe civil and criminal penalties, and will be prosecuted to the maximum extent
possible under the law.
*/
jQuery(document).ready(function() {
   var stateValue = jQuery('#state').attr('value');
   if (stateValue != ""){
     	makeCityList(stateValue);
   }
   jQuery('#state').change(function(){
	  stateValue = jQuery('#state').attr('value');
	  if (stateValue != ""){
     	makeCityList(stateValue);
     }
   });

});

function makeCityList(stateValue) {
   var dataString = "state="+stateValue+"&r="+ Math.random();
 //  alert("dataString="+dataString);
	jQuery.ajax({
	    type: "POST",
	    url: '/getcity.do',
	    data: dataString,
	    success: function(response) {
			//alert("response="+response);
		    if(response != null){
				jQuery('#city').children().remove().end().append('<option value="0">Select City</option>') ;
				jQuery('#city').append(response);
		    }
	   }
	});

}

function setActiveDressCode(thisDressCodeID){
    //highlight tab
	  jQuery("[id^='dresstab_']").each(function(){
         var tabID = jQuery(this).attr("id");
         if(tabID.split("_")[1] == thisDressCodeID){
             jQuery("#"+tabID).removeClass('infoTabOff').addClass('infoTabOn');
         } else {
             jQuery("#"+tabID).removeClass('infoTabOn').addClass('infoTabOff');
         }
    });

	jQuery.post("/ancillary/schools/specificdresscode.do?method=getDressCode&dressCodeID="+thisDressCodeID+ "&r="+ Math.random(),
	function(data){
		var sCurrentSpecificHTML = (jQuery("#dresscodeContent").text() + "").replace(/\s/g, "");
		var sNewSpecificHTML = (jQuery(data).text() + "").replace(/\s/g, "");

		if ((sCurrentSpecificHTML != sNewSpecificHTML)){
		//	alert(jQuery("#dresscodeContent").html());
			jQuery("#dresscodeContent").html(data);
		}
	});

    showPopup(300, 75);

}

function showPopup(w,h){
	var blockScreen = document.getElementById("blockScreen");
	blockScreen.style.visibility = "visible";
	blockScreen.style.width= window.document.body.scrollWidth;
	blockScreen.style.height= window.document.body.scrollHeight;

	var popUp = document.getElementById("popupcontent");
	popUp.style.visibility = "visible";

	popUp.style.width = w + "px";
	popUp.style.height = h + "px";

	var text1="<table border=\"0\">";
	text1= text1+ "<tr><td style=\"font-size:16px\">Please wait a moment while the dress code is loading <blink>...</blink></td></tr>";
// 	text1= text1+ "<tr><td align=middle><img src=\"/images/us/local/localbuttons/animation.gif\" onclick=\"hidePopup();\"></td></tr>";
	text1= text1+ "</table>";
	popUp.innerHTML =text1;

}

function getSize(productID, optionTypeID, dressCodeID, sizeRange, colorID){
    var dataString = "productID="+productID+ "&sizeRange=" + sizeRange + "&dressCodeID=" + dressCodeID + "&colorID=" + colorID + "&r="+ Math.random();
    var thisID = "#options_" + productID + "_" + optionTypeID + "_" + colorID;
  //  alert("dataString="+dataString);
	jQuery.ajax({
	    type: "POST",
	    url: '/getsize.do',
	    data: dataString,
	    success: function(response) {
			//alert("response="+response);
		    if(response != null){
				jQuery(thisID).children().remove().end().append('<option value="0">Then, Select a Size</option>') ;
				jQuery(thisID).append(response);
		    }
	   }
	});
}


function setOptions(pid, colorTypeID, colorID, sizeTypeID){

	jQuery("select[id^='options_']").each(function(){
		var optionValue= jQuery(this).find("option:selected").val();
	//	alert("optionValue="+optionValue);
		var optionID = jQuery(this).attr("id");

		productID = optionID.split("_")[1];
	    thisColor = optionID.split("_")[3];
	//	alert("optionID="+ optionID + "    productID="+productID + "  pid=" + pid + "  thisColor=" + thisColor + "  colorID=" + colorID);
		if ((productID == pid) && (thisColor == colorID)) {
			var optionFieldVal = "#optionTypeValues_" + pid + "_" + colorID;
			var optionVal = colorTypeID + "=" + colorID + ":" + sizeTypeID + "=" + optionValue;
			jQuery(optionFieldVal).val(optionVal);
		}
    });
}

//method to set sourceCode on payment page:
function setSourceCode(sourceCode){
	var src = window.opener.document.getElementsByName("schoolSourceCode");
	src[0].value = sourceCode;
	self.close();
	return false;
}

function GoodbyeAlert(w, h){
    var optValue = '';
    var optTypeValue = '';
    var optionTypes = new Array();
    var options = new Array();
    var productWithNoOptions = 0;
    var productWithOptions = 0;
    var validOptions = 0;
    var j = 0;
    var badOptions = 0;

	var form = document.schoolSearchForm;

    for(var i = 0; i < form.length; i++) {
       var field = form.elements[i];
	//   alert( ' field name: ' + field.name + ', field value ' + field.value + ' field type ' + field.type);
	   if (field.type == "hidden" ) {
            if(field.name == "option") {
              optValue = field.value;
            } else if (field.name == "optionTypes") {
              optTypeValue = field.value;
            }
        }

        // if all fields values are collected, store them into the arrays
        if (optValue != "" && optTypeValue != "") {
          options[j] = optValue;
          optionTypes[j] = optTypeValue;
          j++;
          optTypeValue = ""; optValue = "";
        }
    }

    // go through the arrays and validate
    for (var m = 0; m < j; m++) {
       optTypeValue = optionTypes[m];
       optValue = options[m];
       if (optTypeValue > 0) {
           // product has options so check if any selected
           if(optValue != "none") {
               // something was selected
               // now check that number of options corresponds to selection
               arr = optValue.split(":");
               if (arr.length == optTypeValue) {
                   // got the same number of options as the number of drop-down menus
                   // now check if a zero value is selected for each one of them
                   for(var n = 0; n < arr.length; n++) {
                        subArr = arr[n].split("=");
                        if ((subArr[1] != 0)) {
                            validOptions++;
                        }
                   }
                   if (validOptions == optTypeValue) {
                       productWithOptions++;
                   } else {
                       badOptions++;
                   }
                   // reset var
                   validOptions = 0;
               } else {
                   badOptions++;
               }
           } else {
               badOptions++;
           }
       } else {
           // product has no options, increment counter.
           productWithNoOptions++;
       }

    } // end for

//  alert("badOptions="+badOptions + "   productWithOptions="+productWithOptions + "   productWithNoOptions=" +  productWithNoOptions);
	if ( (productWithOptions > 0) || (productWithNoOptions > 0) ) {
        var x = window.confirm("Note: Items you selected have not been added to your shopping cart, click on the 'Add to Cart' \nbutton before proceeding to another category.  \n\nClick OK to proceed or Cancel to stay on this page.");

        if (x) {
            return true;
		} else {
            return false;
		}
	} else {
		//ok, no alert
	}
    return true;

}
