// JavaScript Document

function validateMediaSelection(mediaForm, formatList)	// Validates product media type selection prior to adding to cart
// Pass in a reference to the list of media formats that the product comes in, along with the selection the user has made
// e.g., <form action="http://www.kingschools.com/Cart/Cart_Detail.asp" method="post" name="myForm" onsubmit="return submitIt(this.mediaType,'DVD,CD-ROM')">
{
	var formatOption = -1;
	var formats = formatList.split(",");
	var formatText = "";
	var conjunction;
	var mediaSelection = mediaForm.mediaType;
	var answer;
	var bIsURL = false;
	var selectionValue = "";

    /* If there is only one radio button, then mediaSelection
       will not have not have an array and the single value
       property will be populated, so copy the value and set the formatOption.
    */
    if(mediaSelection.value!=null)  //Only one option, so check if selected
    {
        formatOption = 0;
		selectionValue = mediaSelection.value; //If bIsURL=true, then this will be a URL
        if(mediaSelection.value.indexOf("kingschoolsonline")>-1) //check if option is for an online course and requires a redirect
        {
            bIsURL = true;
        }
	}
	else // Two or more options, so cycle through array and find if one was selected
	{
	    for(i=0;i<mediaSelection.length;i++)
	    {
		    if(mediaSelection[i].checked)
		    {
			    formatOption = i;
			    //mediaForm.ITN.value = mediaSelection[i].value;
			    selectionValue = mediaSelection[i].value; //If bIsURL=true, then this will be a URL
                if(mediaSelection[i].value.indexOf("kingschoolsonline")>-1) //check if option is for an online course and requires a redirect
                {
                    bIsURL = true;
                }
		    }
	    }
	 }
	
	 if(bIsURL) //User chose online course, so ask if they want to be sent there
	 {
	    var sOnlinePrompt;
	    sOnlinePrompt = "Your online learning is just a few clicks away!\n\n";
	    sOnlinePrompt += "We will now open a new window to www.kingschoolsONLINE.com to complete the purchase of this online course.\n\n";
	    sOnlinePrompt += "KING Online Courses are purchased seperately from other items.  If you have added ";
	    sOnlinePrompt += "other items to your cart, you may wish to click 'Cancel' here and complete the purchase of ";
	    sOnlinePrompt += "those items.  Otherwise, click 'OK' to continue on to the convenience of KING Online Learning!\n\n";
	    sOnlinePrompt += " - Click 'OK' to complete the purchase of your online course at www.KingSchoolsONLINE.com, or...\n\n";
	    sOnlinePrompt += " - Click 'Cancel' to continue shopping on www.KingSchools.com";
	    
	    var answer = confirm(sOnlinePrompt);
	    if(answer)
	    {
	        //User wants to continue to online course registration, so open new window to take them there
	        KSOnlineWin = window.open('http://' + selectionValue,'KSOnline','left=20,top=20,scrollbars=1,width=900,height=600,toolbar=1,resizable=1');
	        KSOnlineWin.focus;

	        return false;
	    }
	    else
	    {
	        return false;
	    }
	}
	
	if(formatOption==-1)
	{
		for(i=0;i<formats.length;i++)
		{
			if(i==formats.length-2)
			{
				conjunction = " and ";
			}
			else if(i<formats.length-1)
			{
				conjunction = ", ";
			}
			else
			{
				conjunction = " ";
			}
			formatText += formats[i];
			formatText += conjunction;	
		}
		alert("This course is offered in " + formatText + "formats.  Please indicate which media type you would like to receive.");
		return false;
	}
	// This is a ks.com product, so set ITN field to product SKU and submit form
	mediaForm.ITN.value = selectionValue;
	return true;
}

function jumpLoc(newLoc)
// Use for EITHER on page jumping or redirect to another page, but do not mix in a given select list
// This is due to the resetting of the selected index
// Required for on-page jumping:  elements with unique id's to jump to(e.g. <div id="checkride-private">)
{
	var newPage;
	newPage = newLoc.options[newLoc.selectedIndex].value;	
	if(newPage!= "")
	{
	    if(newPage.indexOf('.asp')==-1) //assume this is an anchor on the page
	    {
		    window.location.href = "#" + newPage;
		    newLoc.options.selectedIndex = null;
		}
		else    //this is to a new page
		{
		    window.location.href = newPage;
		}
	}
}