// $Id: search.js 1662 2008-04-18 18:37:22Z alexd $


function advanceSearch(testprepServer) {
    advanceF = testprepServer + "CourseSearch/AdvancedSearch.aspx";
    //alert(advanceF);
    window.location = advanceF;
}

function topSearch(testprepServer){
    var search = testprepServer;//document.getElementById("SearchFor").value;
    if (search == "schools")
    {
		keyword = document.getElementById("txtFindColleges_keyword").value;
		window.location = "http://www.princetonreview.com/Search.aspx?search=" +   search + "&keyword=" + keyword;		
    }
    else if (search == "site")
    {
		keyword = document.getElementById("txtFindSite_keyword").value;
		window.location = "http://www.princetonreview.com/SiteSearch.aspx?search=" + search + "&keyword=" + keyword;        
    }
    else 
    {
		ctrl = document.getElementById("cboFindCourse_options");
		idx = ctrl.selectedIndex
		itemCode = ctrl.options[idx].value;
		if (itemCode == "MORE")
		{
		        advanceSearch('http://testprep.princetonreview.com/');
		        return;
		}
		zipcode = document.getElementById("txtFindCourse_keyword").value;
		sendCourseFinder(itemCode, zipcode, 'http://testprep.princetonreview.com');
    }
}

function sendCourseFinder(itemCode, zipcode,testprepServer)
{    
    courseF = testprepServer + "/CourseSearch/Search.aspx?productType=C"+
        "&rid=1&Zip="+zipcode+"&itemCode="+itemCode;
    if(validateBasic(itemCode, zipcode))
    {
        window.location=courseF;
    }
}

function collegeSearch() {
	var search = htmlEncode(document.getElementById('txtCollegeSearch_keyword').value);
	window.location = "/Search.aspx?search=schools" + "&keyword=" + search;
}

function setSearchType(type) {
    document.getElementById("SearchFor").value = type;
}
/* utility functions--should be moved to separate file */
function htmlEncode(s) {
    var str = new String(s);
    str = str.replace(/&/g, "");
    str = str.replace(/</g, "");
    str = str.replace(/>/g, "");
    str = str.replace(/"/g, "");
    return str;
}


function zipValid(aZip) 
{
	if (aZip == "Zip/Postal Code") {
		alert("Please enter full zip code (should be at least 5 characters long)");
		return false;
	}
	
	if (aZip.length < 5) {
		alert("Please enter full zip code (should be at least 5 characters long)");
		return false;
	}
	if (aZip.length > 7) {
		alert("Zip/Postal code can not be greater than 7 characters long");
		return false;
	}
	if (!isAlphaNumeric(aZip)) {
		alert("Please enter full zip code (should contain only alpha-numeric characters)");
		return false;
	}
	needsValidation = false;
	return true;			
}

function optionValid(aOption)
{
	if (aOption == "")
	{
		alert("Please select a test type");
		return false;
	}
	return true;
}

function validateBasic(itemCode, zipcode) 
{
	if (!optionValid(itemCode)) return false;
	if (!zipValid(zipcode)) return false;
	return true;
}

function clearDefault()
{
	if (document.getElementById("txtFindCourse_keyword").value == "Enter Zip/Postal Code")
	 document.getElementById("txtFindCourse_keyword").value = "";
}

function isAlphaNumeric(aString) 
{
	var s = aString.toUpperCase();
	var ch;
	for (var i = 0; i < s.length; i++) 
	{
		ch = s.charAt(i);
		if (!((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch == ' '))) 
		{
			return false;
		}
	}
	return true;		
}

function setInitialSearch() {
	var courseTab = document.getElementById("tabCourseSearch");
	var schoolTab = document.getElementById("tabSchoolSearch");
	var siteTab   = document.getElementById("tabSiteSearch");
	var urlPieces = parseUri(window.location);
//	alert(urlPieces["directoryPath"]);
//	alert(urlPieces["fileName"]);
	if ( urlPieces["fileName"] == "Search.aspx" || urlPieces["directoryPath"].match("/schools/")) {
		changeClass(schoolTab,"tab active");
		changeClass(courseTab,"first tab");
		changeClass(siteTab,"tab");
		document.getElementById("SearchFor").value = "schools";
	} else if ( urlPieces["fileName"] == "testprep.aspx" || urlPieces["fileName"] == "Home.aspx" ) {
		changeClass(schoolTab,"tab");
		changeClass(courseTab,"first tab active");
		changeClass(siteTab,"tab");
		document.getElementById("SearchFor").value = "course";
	} else {
		changeClass(schoolTab,"tab");
		changeClass(courseTab,"first tab");
		changeClass(siteTab,"tab active");
		document.getElementById("SearchFor").value = "site";
	}
}