
function initialize() {
	var countryCode = getQueryVariable("countryCode");
	var stateCode = getQueryVariable("stateCode");
	
	if ($("schlrshp") !== null) {
		schlrshp=true;
		schoolDataSrc="CONTEST_DATA_SRC";
	} else {
		schlrshp=false;
		schoolDataSrc="STANDARD_DATA_SRC";
	}
	
	if (countryCode == "USA" && stateCode !== "") {
		var stateDD = $("schoolState");
		for (var i=0; i<stateDD.options.length; i++) {
			if (stateDD.options[i].value == stateCode) {
				stateDD.options[i].selected = true;
			}
		}
		populateCities("USA", stateCode);
		enableSchoolName();
	} else if (countryCode == "CAN" && stateCode != "") {
		swapCountry("CAN");
		selectCountryLocation("CAN");
		var stateDD = $("schoolProvince");
		for (var i=0; i<stateDD.options.length; i++) {
			if (stateDD.options[i].value == stateCode) {
				stateDD.options[i].selected = true;
			}
		}
		populateCities("CAN", stateCode);
		enableSchoolName();
	} else if (countryCode != "USA" && countryCode != "") {
		swapCountry("INTL"); 
		selectCountryLocation("INTL");
		var countryDD = $("selCountry");
		for (var i=0; i<countryDD.options.length; i++) {
			if (countryDD.options[i].value == countryCode) {
				countryDD.options[i].selected = true;
			}
		}
		enableSchoolName();
	}	
}

function selectCountryLocation (countryLocation) {
	var countryLocationDD = $("schoolCountry");
	for(var i=0; i< countryLocationDD.options.length; i++) {
		if (countryLocationDD[i].value == countryLocation) {
			countryLocationDD[i].selected = true;
		}
	}

}
function showAvailableDressCodes() {
	for (var i=0; i<schoolNumbers.length; i++) {
		if (URLExists("/pdfs/school/" + schoolNumbers[i] + ".pdf")) {
			$("dress_code_" + schoolNumbers[i]).style.display = "block";
		} else {
			$("please_call_" + schoolNumbers[i]).style.display = "block";
		}
	}
}

function loadCities(data) {
	doDebug("loadCities");
	if (data !== null && typeof data == 'object') {
		var schoolState = $("schoolCity");
		var cityName = unescape(getQueryVariable("cityName"));
		for (var i=0; i<data.length; i++) {
			var el = document.createElement("option");
			schoolState.appendChild(el);
			el.value = data[i];
           	el.text = data[i];
           	if (data[i] == cityName) {
           		el.selected = true;
           	}
		}		
	}
}

function validateDC() {
	var num = $("schoolCode").value;
	$("schoolCode").value = trim($("schoolCode").value);
	var valid = false;
	var numExp = /[0-9]{9}|[0-9]{10}/;

	if(numExp.test(num) == true) {
		valid = true;
		doDebug("valid");
	} else {
	doDebug("invalid");
		var errorMsg = "The Preferred School Number is incorrect or invalid. Please verify it and re-enter or call 1-800-469-2222 for assistance.";
		$("dressCodeErrorTxt").innerHTML = errorMsg;
		$("dressCodeError").style.display = "block";
		$("dcSchoolNumber").className = "errorInput";
	}
	return valid;
}
function getDressCode() {
	var schoolCode = $("schoolCode");
	var dressCodeURL = "/pdfs/school/" + schoolCode.value + ".pdf";
	if (URLExists(dressCodeURL)) {
		new_win = window.open(dressCodeURL, 'pdfWindow', 'resizable=yes,toolbar=yes');
	} else {
        var schoolID = schoolCode.value.substring(0,schoolCode.value.length-1);
        var checkDigit = schoolCode.value.substring(schoolCode.value.length-1);
		JProductDataAccessImpl.findSchoolProgramBySchoolNumber(schoolID, checkDigit, handleSchoolLookupResponse);
	}
}

function handleSchoolLookupResponse (request) {
	var errorMsg="";
    if (request !== null && typeof request == "object") {
    	doDebug("school exists");
		errorMsg = "The Preferred School Number is valid but there is no Dress Code for this school. Please call 1-800-469-2222 for assistance."
	} else {
		doDebug("school doesn't exist");
		errorMsg = "The Preferred School Number is incorrect or invalid. Please verify it and re-enter or call 1-800-469-2222 for assistance.";
    }
    $("dressCodeErrorTxt").innerHTML = errorMsg;
	$("dressCodeError").style.display = "block";
	$("dcSchoolNumber").className = "errorInput";
	return;
}
/* SEARCH PAGE CODE */
var schoolLocation = "USA"; //set the inital location variable to us
function swapCountry(country) { 
	//drop down objects & input divs
	var stateDD = $("stateDD");
	var provinceDD = $("provinceDD");
	var countryDD = $("countryDD");
	var cityDD = $("schoolCity");
	var cityDIV = $("schoolCityDIV");
	if(schoolLocation != country) { //the region clicked is diffrent than before	
		hideErrLbl(); //hide red error labels
		//hide error divs
		$("stateError").style.display = "none";
		$("cityError").style.display = "none";
		$("schoolNameError").style.display = "none";
		$("dressCodeError").style.display = "none";
		clearCities(); //empty city drop down
	}
	if(country == "USA") {
		stateDD.className = "inputDIV";
		provinceDD.className = "inputDIVHidden";
		countryDD.className = "inputDIVHidden";
		cityDIV.className = "inuptDIV";
		schoolLocation = "USA";
		$("schoolState").selectedIndex = 0;
		$("schoolName").value = "";
	}else if(country == "CAN") {
		stateDD.className = "inputDIVHidden";
		provinceDD.className = "inputDIV";
		countryDD.className = "inputDIVHidden";
		cityDIV.className = "inuptDIV";
		schoolLocation = "CAN";
		$("schoolProvince").selectedIndex = 0;
		$("schoolName").value = "";
	} else {
		stateDD.className = "inputDIVHidden";
		provinceDD.className = "inputDIVHidden";
		countryDD.className = "inputDIV";
		cityDIV.className = "inputDIVHidden"; //hide city drop down (still takes up space)
		schoolLocation = "INTL";
		$("selCountry").selectedIndex = 0;
		$("schoolName").value = "";
	}
	$("schoolCity").disabled = true;
	disableSchoolName();
}

var activeTab = "location";
function hideErrLbl() {
	//clear label colors
	$("stateLabel").className = "inputDescription";
	$("provinceLabel").className = "inputDescription";
	$("countryLabel").className = "inputDescription";
	$("cityLabel").className = "inputDescription";
	$("schoolNameLabel").className = "inputDescription";
	$("dcSchoolNumber").className = "inputDescription";
}
function resetVals() {	
	if(getCookie("school_region") == "") {
		document.schoolSearch.schoolCountry[0].checked = true;
		swapCountry('USA');
	}
}
function disableSchoolName() {
	$("schoolName").disabled = true;
	$("schoolName").style.backgroundColor = "#d4d0c8";
	$("schoolName").value = "";
}

function enableSchoolName() {
	$("schoolName").disabled = false;
	$("schoolName").style.backgroundColor = "#FFFFFF";
}

function toggleSchoolName(obj) {
	if(obj.selectedIndex > 1) {
		enableSchoolName();
	}else {
		disableSchoolName();
	}
}

function searchType(kind) {
	if(activeTab != kind) { //tab has been changed
		 hideErrLbl();
	}
	if(kind == "location" && activeTab != kind) {
		$("schoolSearch").style.display = 'block';	
		$("dressCode").style.display = 'none';
		/* swap tabs */
		//region tab
		$("regionText").className = "tabOnText";
		var rL = new String($("regionImgL").src);
		$("regionImgL").src = rL.replace("tabOffLeft.gif", "tabOnLeft.gif");
		var rR = new String($("regionImgR").src);
		$("regionImgR").src = rR.replace("tabOffRight.gif", "tabOnRight.gif");
			
		//dress code tab
		$("dressCodeText").className = "tabOffText";
		var dL = new String($("dressCodeImgL").src);
		$("dressCodeImgL").src = rL.replace("tabOnLeft.gif", "tabOffLeft.gif");
		var dR = new String($("dressCodeImgR").src);
		$("dressCodeImgR").src = rR.replace("tabOnRight.gif", "tabOffRight.gif");

	}else if(kind == "code" && activeTab != kind) {
		$("schoolSearch").style.display = 'none';	
		$("dressCode").style.display = 'block';

		/* swap tabs */
		//region tab
		$("regionText").className = "tabOffText";
		var rL = new String($("regionImgL").src);
		$("regionImgL").src = rL.replace("tabOnLeft.gif", "tabOffLeft.gif");
		var rR = new String($("regionImgR").src);
		$("regionImgR").src = rR.replace("tabOnRight.gif", "tabOffRight.gif");
		
		//dress code tab
		$("dressCodeText").className = "tabOnText";
		var dL = new String($("dressCodeImgL").src); 
		$("dressCodeImgL").src = rL.replace("tabOffLeft.gif", "tabOnLeft.gif");
		var dR = new String($("dressCodeImgR").src);
		$("dressCodeImgR").src = rR.replace("tabOffRight.gif", "tabOnRight.gif");

	}
	$("stateError").style.display = "none";
	$("cityError").style.display = "none";
	$("schoolNameError").style.display = "none";
	$("dressCodeError").style.display = "none";
	activeTab = kind;
}

function clearCities() {
	document.schoolSearch.schoolCity.length = 1;
}

function populateCities(loc, arrayKey) {
	var cityDD = $("schoolCity");
	cityDD.length = 1;
	var popArray;
	var selIndex;

	if (loc == "USA") {
		selIndex = document.schoolSearch.schoolState.selectedIndex;		
	} else if (loc == "CAN") {
		selIndex = document.schoolSearch.schoolProvince.selectedIndex;	
	}
	if (selIndex > 1) { 
		cityDD.options[cityDD.options.length] = new Option("----------------",""); //.add(optn1);		
		doDebug("loadAvailable");
		JSchoolSearchDataAccessImpl.loadAvailableSchoolCities(loc, arrayKey, schoolDataSrc, {
			callback:loadCities,
			timeout:10000,
			errorHandler: function(msg){doDebug("crap:"+msg)}
		});
		doDebug("huh?");
		cityDD.disabled = false;
	} else {
		cityDD.disabled = true;
		disableSchoolName();
	}
}

function validateSS() {		
	var valid = true;
	var errorID;
	var errorMsgId;
	var errorMsg = "";

	if(schoolLocation == "USA") {
		if(document.schoolSearch.schoolState.selectedIndex < 2) {
			errorMsg = "Please select a state.";
			errorID = "stateError";
			valid = false;
			$("stateLabel").className = "errorInput";
			$("cityLabel").className = "inputDescription";
			$("schoolNameLabel").className = "inputDescription";
		}
		if(document.schoolSearch.schoolCity.selectedIndex < 2 && valid == true) {
			errorMsg = "Please select a city.";
			errorID = "cityError";
			valid = false;

			$("stateLabel").className = "inputDescription";
			$("cityLabel").className = "errorInput";
			$("schoolNameLabel").className = "inputDescription";
		}
	} else if (schoolLocation == "CAN") {
		if (document.schoolSearch.schoolProvince.selectedIndex < 2) {
			errorMsg = "Please select a province.";
			errorID = "stateError";
			valid = false;
			$("provinceLabel").className = "errorInput";
			$("cityLabel").className = "inputDescription";
			$("schoolNameLabel").className = "inputDescription";

		}
		if (document.schoolSearch.schoolCity.selectedIndex < 2 && valid == true) {
			errorMsg = "Please select a city";
			errorID = "cityError";
			valid = false;

			$("provinceLabel").className = "inputDescription";
			$("cityLabel").className = "errorInput";
			$("schoolNameLabel").className = "inputDescription";
		}
	} else if (schoolLocation == "INTL") {
		if(document.schoolSearch.selCountry.selectedIndex < 2) {
			errorMsg = "Please select a country.";
			errorID = "stateError";
			valid = false;
			$("countryLabel").className = "errorInput";
			$("schoolNameLabel").className = "inputDescription";
		}
	}
	if (valid == true) {
		var nameExp = /[a-zA-Z0-9\s\.]/;
		var numExp = /[0-9]+/;
		var schoolName = $("schoolName").value.trim();
		if (schoolName.length < 4) {
			errorMsg = "School name must contain at least 4 letters.";
			valid = false;
		} else if (!nameExp.test(schoolName) && schoolName != "") {
			errorMsg = "Invalid characters. Please use only alpha/numeric characters.";
			valid = false;
		} else {
			var numSchoolResults = getNumResults();
			if (numSchoolResults < 1) {
				errorMsg = "No matching schools found.";
			valid = false;
			} else if (numSchoolResults > 4) {
				errorMsg = "Multiple schools found. To refine your search, please be as specific as possible.";
				valid = false;
			}
		}
		if (valid == false) {
			errorID = "schoolNameError";
			$("provinceLabel").className = "inputDescription";
			$("cityLabel").className = "inputDescription";
			$("schoolNameLabel").className = "errorInput";
		}
	}
	if (valid == false) {
		var errorTxtID = errorID + "Txt"
		$("stateError").style.display = "none";
		$("cityError").style.display = "none";
		$("schoolNameError").style.display = "none";
		$(errorID).style.display = "block";
		$(errorTxtID).innerHTML = errorMsg;
	}
	return valid;
}

function getNumResults() {
	var state = "";
	var city = "";
	var schoolName = $("schoolName").value.trim();
	
	if (schoolLocation == "USA") {
		state = document.schoolSearch.schoolState.value;
		city = document.schoolSearch.schoolCity.value;
	} else if (schoolLocation == "CAN") {
		state = document.schoolSearch.schoolProvince.value;
		city = document.schoolSearch.schoolCity.value;
	} else if (schoolLocation == "INTL") {
		schoolLocation=document.schoolSearch.selCountry.value;
	}
	DWREngine.setAsync(false);
	window.numResults = 0;
	JSchoolSearchDataAccessImpl.loadMatchingSchoolPrograms(_handleSchoolSearchResponse, schoolName, schoolLocation, state, city, schoolDataSrc);
	return window.numResults;
}
function _handleSchoolSearchResponse (data) {
	var errorMsg="";
    if (data !== null && typeof data == "object" ) {
    	window.numResults =data.length;
	} 
	return window.numResults;
}

function goFind() {
	var state = "";
	var city = "";
	var schlrshpParm = "";
	var schoolName = $("schoolName").value.trim();
	
	if (schoolLocation == "USA") {
		state = document.schoolSearch.schoolState.value;
		city = document.schoolSearch.schoolCity.value;
	} else if (schoolLocation == "CAN") {
		state = document.schoolSearch.schoolProvince.value;
		city = document.schoolSearch.schoolCity.value;
	} else if (schoolLocation == "INTL") {
		schoolLocation=document.schoolSearch.selCountry.value;
	}
	if (schlrshp) {
		schlrshpParm="&mode=schlrshp";
	}
	window.location = window.location.pathname +"?action=doSearch&countryCode="+schoolLocation+"&stateCode="+state+"&cityName="+city+"&schoolName="+schoolName+schlrshpParm;
	
}

function resetRadios() {
	$("radioLocation").checked =false;
	$("radioNumber").checked =false;
}

function showContinue(num) {
	var id="continue_" + num;
	hideContinues();
	$(id).style.visibility = 'visible';
	$(id).style.display = 'block';
}

function hideContinues() {
	var index = 0;
	
	while($("continue_" + index)) {
		$("continue_" + index).style.visibility = 'hidden';
		$("continue_" + index).style.display = 'none';
		index++;
	}
}

function clear_radio_buttons() {
	if(document.schoolResults.schoolSelect) {
		if(document.schoolResults.schoolSelect.length > 1) {
			hideContinues();
		}
		for (var i = 0; i < document.schoolResults.schoolSelect.length; i++) {
		  document.schoolResults.schoolSelect[i].checked = false;
		}
	}
}

function schoolSelect(num) {	
	setCookie("schoolNumber", num);
	try {
		if (opener && opener.firstBuyGridKey) {
			opener.buyGrids[opener.firstBuyGridKey].applySchoolCode(num);
		}
	} catch (e) {
	}
	window.location = "/pp/SchoolSearch.html?action=doApply";

}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
