var isOpen = false;

function positionOverlay() {
	//if box is not set to open then don't do anything
	if( isOpen == false ) return;
	// set the properties of the overlay box, the left and top positions
	$('.overlay-container').css({
		left:( $(window).width() - $('.overlay-container').width() )/2,
		//top:( $(window).height() - $('.overlay-container').height() )/2 -20,
		position:'absolute'
	});
	// set the window background for the overlay. i.e the body becomes darker
	$('.overlay-background_screen').css({
		display:'block',
		width: $(window).width(),
		height:($(window).height() * 2)
	});
}
var isOpen;

function openOverlay() {
	//set status to open
	isOpen = true;
	positionOverlay();
	$('.overlay-container').slideDown();
	$('.overlay-background_screen').css({opacity:0}).animate( {opacity:0.8} );
	
	//execute download.asp to insert records in tblDownloadInitial: track abandoned download
	$.get("/shop/download.asp?from=product");
	return false;
}
function closeOverlay() {
	//set status to closed
	isOpen = false;
	$('.overlay-container').slideUp();

	// now animate the background to fade out to opacity 0
	// and then hide it after the animation is complete.
	$('.overlay-background_screen').css({
		display:'none',
		width: $(window).width(),
		height:$(window).height()
	});
	return false;
}

function downloadItems() {
	$('#download_form input:checked').each(function(){
	    if ($(this).val() != 'yes' && $(this).val() != 'no')
	    {
		    //window.open($(this).val());
		    var title = $(this).attr("title");
		    //$(this).next(): hidden field with part number 
		    window.open('/shop/SigninDownload2.asp?from='+$(this).val()+ '&product=' + $('#product').val() + '&item=' + $(this).next().val() + 
		        '&fname=' + $('#Dfname').val() + '&lname=' + $('#Dlname').val() + '&Dtitle=' + $('#Dtitle').val() + '&email=' + $('#Demail').val() + 
		        '&zip=' + $('#Dzip').val() + '&MDR=' + $('#MDR_PID').val() + '&YesEmail=' + $('send_information').val() + '&dtype='+ title);
		    $(this).attr('checked', '');
		}
	});
	window.focus();
	closeOverlay();
	return false;
}

$(function() {
	// if window is resized then reposition the overlay box
	$(window).bind('resize',positionOverlay);
	// activate when the link with class launch link is clicked
	$('a.overlay-launch_link').click(openOverlay);
	// close it when close link is clicked
	$('a.overlay-close_link').click(closeOverlay);
	$('#download_form').submit(downloadItems);
	
	//for discontinue product
    var oldproduct;
    oldproduct = $.query.get('oldproduct');
	if (oldproduct != "")
	    $(window).load(openReplacelay);
    //$('a.overlay-close_link').click(closeReplacelay);
});

function openReplacelay() {
	//set status to open
	isOpen = true;
	$('.overlay-background_screen').css({
		display:'block',
		width: $(window).width(),
		height:($(window).height() * 2)
	});
	$('.replacelay-container').slideDown();
	$('.overlay-background_screen').css({opacity:0}).animate( {opacity:0.8} );
	
	return false;
}
//function closeReplacelay() {
//	//set status to closed
//	isOpen = false;
//	$('.replacelay-container').slideUp();

//	// now animate the background to fade out to opacity 0
//	// and then hide it after the animation is complete.
//	$('.overlay-background_screen').css({
//		display:'none',
//		width: $(window).width(),
//		height:$(window).height()
//	});
//	return false;
//}

function showSample()
{
    //make all show first: for TAKSReady
    $('#download_form input:radio').each(function()
    {
        if ($(this).val() != 'yes' && $(this).val() != 'no')
		      $(this).parent().css({display:'block'});
    });
    document.getElementById("head2").style.display = "block";
    document.getElementById("head4").style.display = "block";

    var grade = document.download_form.ddlGrade.options[document.download_form.ddlGrade.selectedIndex].text;
    if (document.download_form.ddlGrade.value.length !=0)
    {
        document.getElementById("download_real").style.display = "block";
        if (grade == "Grade 6" || grade == "Grade 7" || grade == "Grade 8")
        {
            document.getElementById("head2").style.display = "none";
            document.getElementById("head4").style.display = "none";
        }
        $('#download_form input:radio').each(function()
        {
	        if ($(this).val() != 'yes' && $(this).val() != 'no')
	        {
		        var sampleid = $(this).attr("id");
		        //alert(title);
		        if (sampleid.indexOf(grade) == -1)
		        {
		            //$(this).css({display:'none'});
		            //$(this).prev().css({display:'none'});
		            $(this).parent().css({display:'none'});
		        }
		    }
	    });
    }
    else
    {
        document.getElementById("download_real").style.display = "none";
    }    
}

function showSampleCACS()
{
    //make all show first
    $('#download_form input:radio').each(function()
    {
        if ($(this).val() != 'yes' && $(this).val() != 'no')
		      $(this).parent().css({display:'block'});
    });
    document.getElementById("head1").style.display = "block";
    document.getElementById("head2").style.display = "block";
    document.getElementById("head3").style.display = "block";
    document.getElementById("head4").style.display = "block";
    document.getElementById("head5").style.display = "block";
    document.getElementById("head6").style.display = "block";
    var grade = document.download_form.ddlGrade.options[document.download_form.ddlGrade.selectedIndex].value;
    if (document.download_form.ddlGrade.value.length !=0)
    {
        document.getElementById("download_real").style.display = "block";
        if (grade == "1")
        {
            document.getElementById("head1").style.display = "none"; // CAHSEEpm-ela
            document.getElementById("head2").style.display = "none"; // CAHSEEpm-math
            document.getElementById("head5").style.display = "none"; // CACSscience
            document.getElementById("head6").style.display = "none"; // CELD
        }
        if (grade == "2" || grade == "3" || grade == "4")
        {
            document.getElementById("head1").style.display = "none"; // CAHSEEpm-ela
            document.getElementById("head2").style.display = "none"; // CAHSEEpm-math
            document.getElementById("head5").style.display = "none"; // CACSscience
        }
        if (grade == "5")
        {
            document.getElementById("head1").style.display = "none"; // CAHSEEpm-ela
            document.getElementById("head2").style.display = "none"; // CAHSEEpm-math
        }
        if (grade == "6" || grade == "7")
        {
            document.getElementById("head1").style.display = "none"; // CAHSEEpm-ela
            document.getElementById("head2").style.display = "none"; // CAHSEEpm-math
            document.getElementById("head5").style.display = "none"; // CACSscience
        }
        if (grade == "9" || grade == "10" || grade == "11" || grade == "12")
        {
            document.getElementById("head3").style.display = "none"; // CACSela
            document.getElementById("head4").style.display = "none"; // CACSmath
            document.getElementById("head5").style.display = "none"; // CACSscience
        }
        
        $('#download_form input:radio').each(function()
        {
            var igrade = parseInt(grade);
            var sgrade = "Book " + grade;
                
	        if ($(this).val() != 'yes' && $(this).val() != 'no')
	        {
		        var sampleid = $(this).attr("id");
		        sampleid = sampleid.replace("10-Pack", "");
		        
		        if (sampleid.indexOf("CAHSEE") != -1)    //CAHSEE
		        {
		            if (igrade < 8){$(this).parent().css({display:'none'});}
		        }
		        else if (sampleid.indexOf("Algebra 1") != -1)    //Algebra 1
		        {
		            if (igrade != 8){$(this).parent().css({display:'none'});}
		        }
		        else
		        {
		            if (sampleid.indexOf("3-5") != -1 )
		            {
		                if (igrade <3 ||igrade >5){$(this).parent().css({display:'none'});}
		            }
		            else if (sampleid.indexOf("6-8") != -1)
		            {
		                if (igrade <6 ||igrade >8){$(this).parent().css({display:'none'});}
		            }
		            else if(sampleid.indexOf("9-12") != -1)
		            {
		                if (igrade <9){$(this).parent().css({display:'none'});}
		            }
		            else
		            {
		                if (sampleid.indexOf(sgrade) == -1)
		                {
		                    $(this).parent().css({display:'none'});
		                }
		            }
		        }
		    }
	    });
    }
    else
    {
        document.getElementById("download_real").style.display = "none";
    }  
}

function validateDownloadUser(form)
{
    var strMsg = "";
    var fname = form.Dfname;
    var lname = form.Dlname;
    var email = form.Demail;
    var title = form.Dtitle;
    var zip = form.Dzip;
    if ((ltrim(fname.value)).length ==0)
    {
        strMsg = "Please enter your first name!";
        fname.focus();
        alert(strMsg);
        return false ;
    }
    if ((ltrim(lname.value)).length ==0)
    {
        strMsg = "Please enter your last name!";
        lname.focus();
        alert(strMsg);
        return false ;
    }
    if ((ltrim(title.value)).length ==0)
    {
        strMsg = "Please enter your title!";
        title.focus();
        alert(strMsg);
        return false ;

    }
    if ((ltrim(email.value)).length ==0)
    {
        strMsg = "Please enter your email!";
        email.focus();
        alert(strMsg);
        return false ;

    }
    else 
    {
        if (echeck(email.value)==false){
            email.focus();
            return false ;

        }
    } 
    if ((ltrim(zip.value)).length == 0)
	{
		strMsg = "Please enter your zip code.";
		zip.focus();
		alert(strMsg);
	    return false ;
	}
	return true;	   
}