$(document).ready(function() {
	$("a[@rel=popup]").click(function(){
           popupWindow = window.open(this.href,this.title,'menu=no,toolbar=no,width=640px,height=600,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48');
           popupWindow.focus();
           return false;
    })

    $("a[@rel=external]").click(function(){
           popupWindow = window.open(this.href,this.title,'width=800,height=600,toolbar=yes,status=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes');
           popupWindow.focus();
           return false;
    })	

    $("a[@rel=exit_link]").click(function(){
        $.ajax({
                type: "GET",
                url: "?affiliate_id=1k"
               });
        location = $(this).attr('href');
        return false;
    })	
    
    $("a[@rel=taf_link]").click(function(){
        $.ajax({
                type: "GET",
                url: "?affiliate_id=2e"
               });
        location = $(this).attr('href');
        return false;
    })

    $("#school_name").click(function(){$("#school_zip").val('')});
    $("#school_zip").click(function(){$("#school_name").val('');$("#school_state").val('')});
    $("#schools_dropdown").click(function() {$("#school").val($(this).find(':selected').text());});
});


window.onunload = metrics_end;

function record_taf() {
    $.ajax({
            type: "GET",
            url: "?affiliate_id=2e"
           });
    return false;
}

// Metrics tracking for pathing, dwell time and drop off
//failing in IE, we need to pause execution before end
function metrics_end() {
	if(document.images['metrics']) {
		var endMetrics = new Image();
		endMetrics.src = document.images['metrics'].src.replace(/(page_id=)([^&]*)/, '$1$2&action=end');  ;
	}
	metrics_end_delay(endMetrics);
}
function metrics_end_delay(endMetrics) {
	return endMetrics.src;
}
// Rules Pop-Up
function rules() {
	window.open("index.tbapp?page=rules","rules","menu=no,toolbar=no,width=450,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=88,left=124");
}

// FAQ Pop-Up
function faq() {
  window.open("index.tbapp?page=contact_form","faq","menu=no,toolbar=no,width=450,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48");
}

// FAQ Pop-Up
function faq_form() {
  window.open("index.tbapp?page=contact_form#contact_us","faq","menu=no,toolbar=no,width=450,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=48,left=48");
}


//Forgot password
function forgot(session_id) {
  window.opener.location="index.tbapp?page=forgot_form&session_id="+session_id;
  window.close(); 
}	

// Image Security Information Pop-Up
function img_security() {
	window.open("index.tbapp?page=img_security_info","imgsec","menu=no,toolbar=no,width=450,height=340,scrollbars=0,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=132,left=196");
}

// List Pop-Up
function list() {
	window.open("index.tbapp?page=list","imgsec","menu=no,toolbar=no,width=450,height=340,scrollbars=0,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=132,left=196");
}

// Generic Pop-Up
// ex: <a href="javascript:openThis('http://www.eprize.com');">Click here</a>
function openThis(URL) {
  window.open(URL);
}

// New Window
function openWindow(link) {
  window.open(link, '_blank', 'width=800,height=600,toolbar=yes,status=yes,location=yes,menubar=yes,directories=yes,resizable=yes,scrollbars=yes');
}

// Learn More
function learnmore() {
	window.open("index.tbapp?page=learn_more","learnmore","menu=no,toolbar=no,width=450,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=88,left=124");
}


// No Cookies Popups
function cookies_faq() {
	window.open("index.tbapp?page=cookies_faq","cookies_faq","menu=no,toolbar=no,width=450,height=400,scrollbars=1,resizable=0,directories=no,location=no,screenX=0,screenY=0,top=88,left=124");
}

function find_schools_by_zip(zip) {
    if(zip) {
        $.ajax({
                type: "GET",
                url: "index.tbapp?method=find_schools_by_zip&zip="+zip,
                success: function(data) {
                            populate_school_list(data);
                         }
               });
    }
}

function find_schools_by_name(name, state) {
    if(name && state) {
        $.ajax({
                type: "GET",
                url: "index.tbapp?method=find_schools_by_name&name="+name+"&state="+state,
                success: function(data) {
                            populate_school_list(data);
                         }
               });
    }
}

function populate_school_list(data) {
    var school_list = eval('('+data+')');
    school_list = school_list.reverse();
    for (i in school_list) {
        var opt  = "<option value='"+school_list[i].id+"'>"+school_list[i].name+"  ("+school_list[i].city+")</option>";
        $("#schools_dropdown").prepend(opt);
    }
    
    if(school_list.length > 0) {
        if( -1 != navigator.userAgent.indexOf ("MSIE") ) {
            $("#schools_dropdown option:first").attr('selected', 'true');
        }
        $("#school").val($("#schools_dropdown option:first").text());
        $("#school_label").html("We\'ve found "+school_list.length+" schools. Please select one and click Choose this school and continue.")
        $("#search_results").fadeIn('slow');
        $("#submit_school_btn").fadeIn('slow');
    } else {
        $("#school_label").html("The school cannot be found. Please try again.")
        if(!$("$search_results").is(":hidden")) {
            $("#search_results").fadeOut('slow');
            $("#submit_school_btn").fadeOut('slow');
        }
    }
    $("#school_label").fadeIn();
    
    if(school_list.length > 0) {
        $("#schools_dropdown").attr('disabled','');
    }
}

function find_schools() {
    var zip = $("#school_zip").val();
    var school_name = $("#school_name").val();
    var state = $("#school_state").val();
    
    if(!zip && !(school_name && state)) {
        if(school_name) {
            $("#school_state_label").addClass('error');
        } else if(state) {
            $("#school_name_label").addClass('error');
        } else {
            $("#school_state_label").addClass('error');
            $("#school_name_label").addClass('error');
            $("#school_zip_label").addClass('error');
        }
        $("#reg_error").html("Please enter school's ZIP Code and click search."); 
    } else if(!validateZIP(zip)) {
        $("#school_zip_label").addClass('error');
        $("#reg_error").html("The ZIP Code you entered is invalid."); 
    } else {
        $(".search_button").fadeOut();
        $("#reg_error").html(""); 
        $("#school_state_label").removeClass('error');
        $("#school_name_label").removeClass('error');
        $("#school_zip_label").removeClass('error');
        $("#schools_dropdown > option").remove();
        $("#schools_dropdown").attr('disabled', 'true');
        $("#school_label").html('<img src="images/loader.gif"/> Searching...');
        $("#school").val('');
        
        if(zip) {
            find_schools_by_zip(zip);
        } else if(school_name && state) {
            find_schools_by_name(school_name, state);
        }
        $(".search_button").fadeIn();
    }
}

function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;

    if (field.length!=5 && field.length!=10) {
        return false;
    }
    for (var i=0; i < field.length; i++) {
        temp = "" + field.substring(i, i+1);
        if (temp == "-") hyphencount++;
        if (valid.indexOf(temp) == "-1") {
        return false;
    }
    if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
        return false;
       }
    }
    return true;
}

