﻿var lastSearch = '';
var lastKeyHitTime;
var ctlUID = '';

var newAffiliateID = 0;
var currentAffiliateID = 0;
var haveCheckedAffiliateID = false;

function CheckAndCallDoPostBack() {
    if (document.getElementById(ctlUID).value.length > 2) {
        if (document.getElementById(ctlUID).value != lastSearch) {
            __doPostBack(ctlUID, '');
        }
    }
}

// do a jquery ajax call that gets the current affiliate id from the server...
function checkForSchoolChange(affId)
{
	newAffiliateID = affId
	var scriptFrame = getEl("SchoolCheckFrame");
	
	// the date serves two purposes: to help prevent caching, and for the script to check the age of the call.
	var dateString = new Date().toUTCString();

	var scriptUrl = location.protocol + "//" + location.host + "/GetCurrentAffiliateID.aspx?date=" + dateString;

	scriptFrame.src = scriptUrl;
}

// check the affiliate id from the server against the one that the customer is selecting.
function returnFromGettingAffiliateID()
{
	if (currentAffiliateID != 0 && currentAffiliateID != newAffiliateID)
	{
		showSchoolChangeDialog();
	}
	else
	{
		currentAffiliateID = newAffiliateID;
		selectSchool();
	}
}

function showSchoolChangeDialog()
{
	$("#dlgSchoolChange").dialog({ modal: true, autoOpen: false, width: 375, resizable: false, title: "Change Schools?", position: 'center' });
	$("#dlgSchoolChange").dialog("open");
}

function selectSchool()
{
	$("#dlgSchoolChange").dialog("close");
	
	var merchUrl = "YBMerchandise.aspx?sid=" + newAffiliateID;
	window.location = merchUrl;
}

function cancelSchoolChange()
{
	$("#dlgSchoolChange").dialog("close");
	window.location = "ssCart.aspx";
}