// JAVASCRIPT utility_functions.js 
// Files and directory structures
var baseURL = "http://bookstore.ucsd.edu";
var secureURL = "https://bookstore.ucsd.edu";

//NEEDS HTTP PATH ON SERVER
var cssDir = "http://bookstore.ucsd.edu/utilities/styles/";
var stylePrefix = "";

var winNS6CSS 		= "win_mozilla.css";
var winNS4CSS 		= "win_ns4.css";
var winIECSS  		= "win_ie.css";	
var winIEPRE7CSS 	= "win_iePRE7.css";	
var winOPCSS  		= "win_op.css";
var macIECSS  		= "mac_ie.css";
var macNS6CSS 		= "mac_ns6.css";
var macNS4CSS 		= "mac_ns4.css";
var macOPCSS  		= "mac_op.css";
var unixCSS   		= "unix.css";

//Adding the PHP file to get Server Time
var serverDate = "/utilities/php/serverdate.php";
document.write("<script type\"=text/javascript\" src=\"" + baseURL + serverDate + "\"></script>");

// *************************************************************
//  CLIENT_SIDE SNIFFER CODE
// *************************************************************
// convert all characters to lowercase to simplify testing 
var agt=navigator.userAgent.toLowerCase(); 

// *** BROWSER VERSION *** 
// Note: On IE5, these return 4, so use is_ie5up to detect IE5. 
var is_major = parseInt(navigator.appVersion); 
var is_minor = parseFloat(navigator.appVersion); 

// *** BROWSER TYPE *** 
	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
    var is_nav2 = (is_nav && (is_major == 2));
    var is_nav3 = (is_nav && (is_major == 3));
    var is_nav4 = (is_nav && (is_major == 4));
    var is_nav4up = (is_nav && (is_major >= 4));
    var is_navonly      = (is_nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    var is_nav6 = (is_nav && (is_major == 5));
    var is_nav6up = (is_nav && (is_major >= 5));
    var is_gecko = (agt.indexOf('gecko') != -1);

    var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var is_ie3    = (is_ie && (is_major < 4));
    var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5")==-1) );
    var is_ie4up  = (is_ie && (is_major >= 4));
    var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
	var is_ie7    = (is_ie && (is_major == 4) && (agt.indexOf("msie 7.0")!=-1) );
	var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
    var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
	//var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);
	//var is_ie7up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5 && !is_ie_6);
	


   // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
   // or if this is the first browser window opened.  Thus the
   // variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
    var is_aol   = (agt.indexOf("aol") != -1);
    var is_aol3  = (is_aol && is_ie3);
    var is_aol4  = (is_aol && is_ie4);
    var is_aol5  = (agt.indexOf("aol 5") != -1);
    var is_aol6  = (agt.indexOf("aol 6") != -1);

    var is_opera = (agt.indexOf("opera") != -1);
    var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
    var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
    var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
    var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
    var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);

    var is_webtv = (agt.indexOf("webtv") != -1); 

    var is_TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1)); 
    var is_AOLTV = is_TVNavigator;

    var is_hotjava = (agt.indexOf("hotjava") != -1);
    var is_hotjava3 = (is_hotjava && (is_major == 3));
    var is_hotjava3up = (is_hotjava && (is_major >= 3));

   // *** PLATFORM ***
    var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
   // NOTE: On Opera 3.0, the userAgent string includes "Windows 95/NT4" on all
   //        Win32, so you can't distinguish between Win95 and WinNT.
    var is_win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));

   // is this a 16 bit compiled version?
    var is_win16 = ((agt.indexOf("win16")!=-1) || 
               (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1) || 
               (agt.indexOf("windows 16-bit")!=-1) );  

    var is_win31 = ((agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) ||
                    (agt.indexOf("windows 16-bit")!=-1));

    var is_winme = ((agt.indexOf("win 9x 4.90")!=-1));
    var is_win2k = ((agt.indexOf("windows nt 5.0")!=-1));

   // NOTE: Reliable detection of Win98 may not be possible. It appears that:
   //       - On Nav 4.x and before you'll get plain "Windows" in userAgent.
   //       - On Mercury client, the 32-bit version will return "Win98", but
   //         the 16-bit version running on Win98 will still return "Win95".
    var is_win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
    var is_winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
    var is_win32 = (is_win95 || is_winnt || is_win98 || 
                    ((is_major >= 4) && (navigator.platform == "Win32")) ||
                    (agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));

    var is_os2   = ((agt.indexOf("os/2")!=-1) || 
                    (navigator.appVersion.indexOf("OS/2")!=-1) ||   
                    (agt.indexOf("ibm-webexplorer")!=-1));

    var is_mac    = (agt.indexOf("mac")!=-1);
   // hack ie5 js version for mac
    if (is_mac && is_ie5up) is_js = 1.4;
    var is_mac68k = (is_mac && ((agt.indexOf("68k")!=-1) || 
                               (agt.indexOf("68000")!=-1)));
    var is_macppc = (is_mac && ((agt.indexOf("ppc")!=-1) || 
                                (agt.indexOf("powerpc")!=-1)));

    var is_sun   = (agt.indexOf("sunos")!=-1);
    var is_sun4  = (agt.indexOf("sunos 4")!=-1);
    var is_sun5  = (agt.indexOf("sunos 5")!=-1);
    var is_suni86= (is_sun && (agt.indexOf("i86")!=-1));
    var is_irix  = (agt.indexOf("irix") !=-1);   // SGI
    var is_irix5 = (agt.indexOf("irix 5") !=-1);
    var is_irix6 = ((agt.indexOf("irix 6") !=-1) || (agt.indexOf("irix6") !=-1));
    var is_hpux  = (agt.indexOf("hp-ux")!=-1);
    var is_hpux9 = (is_hpux && (agt.indexOf("09.")!=-1));
    var is_hpux10= (is_hpux && (agt.indexOf("10.")!=-1));
    var is_aix   = (agt.indexOf("aix") !=-1);     // IBM
    var is_aix1  = (agt.indexOf("aix 1") !=-1);    
    var is_aix2  = (agt.indexOf("aix 2") !=-1);    
    var is_aix3  = (agt.indexOf("aix 3") !=-1);    
    var is_aix4  = (agt.indexOf("aix 4") !=-1);    
    var is_linux = (agt.indexOf("inux")!=-1);
    var is_sco   = (agt.indexOf("sco")!=-1) || (agt.indexOf("unix_sv")!=-1);
    var is_unixware = (agt.indexOf("unix_system_v")!=-1); 
    var is_mpras    = (agt.indexOf("ncr")!=-1); 
    var is_reliant  = (agt.indexOf("reliantunix")!=-1);
    var is_dec   = ((agt.indexOf("dec")!=-1) || (agt.indexOf("osf1")!=-1) || 
           (agt.indexOf("dec_alpha")!=-1) || (agt.indexOf("alphaserver")!=-1) || 
           (agt.indexOf("ultrix")!=-1) || (agt.indexOf("alphastation")!=-1)); 
    var is_sinix = (agt.indexOf("sinix")!=-1);
    var is_freebsd = (agt.indexOf("freebsd")!=-1);
    var is_bsd = (agt.indexOf("bsd")!=-1);
    var is_unix  = ((agt.indexOf("x11")!=-1) || is_sun || is_irix || is_hpux || 
                 is_sco ||is_unixware || is_mpras || is_reliant || 
                 is_dec || is_sinix || is_aix || is_linux || is_bsd || is_freebsd);

    var is_vms   = ((agt.indexOf("vax")!=-1) || (agt.indexOf("openvms")!=-1));

			 
//  Select the appropriate stylesheet
if ((is_nav4up || is_ie4up) || (!is_nav && !is_ie)) {
  ChooseStyleSheet();
}

// Handle Navigator 4 window resizing
if (is_nav4up) {
   var initWidth, initHeight;
   initWidth = window.innerWidth;
   initHeight = window.innerHeight;
   window.captureEvents(Event.RESIZE);
   window.onresize = handleResize;
}

// *********************************************************************
//  UTILITY FUNCTIONS
// *********************************************************************
// Function to handle window resizing on Navigator 4
function handleResize() {
   if (((initWidth != window.innerWidth) || (initHeight != window.innerHeight)) && (typeof disableReload == "undefined")) {
     location.reload();
   }
   return false;
}

// Function to choose the style sheet for use based on the platform
// and browser version
function ChooseStyleSheet() {
	var fileHead = cssDir + stylePrefix;
	var styles;
  if (is_win) {
	if (is_nav6up) {
      styles = fileHead + winNS6CSS;} 
	else if (is_nav4) {
      alert("The UCSD Bookstore no longer supports this browser.  We encourage you to upgrade your browser in order to fully enjoy our site.");
	  styles = fileHead + winNS4CSS;} 
	else if (is_opera) {
      styles = fileHead + winOPCSS;} 
	else if (is_ie7) {
      styles = fileHead + winIECSS;}
	else {  
	  styles = fileHead + winIEPRE7CSS;	}
  } 
  else if (is_mac) 
      if (is_nav6up) {
	    styles = fileHead + macNS6CSS;
	} else if (is_nav4) {
       alert("The UCSD Bookstore no longer supports this browser.  We encourage you to upgrade your browser in order to fully enjoy our site.");
	  styles = fileHead + macNS4CSS;
	} else if (is_opera) {
	  styles = fileHead + macIECSS;
	
  } else if (is_unix) {
    styles = fileHead + unixCSS;
  } else {
	styles = fileHead + macIECSS;
  }
  document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + styles + "\">");
  return true;
}

function formHandler(form){
var URL = document.form.quickLink.options[document.form.quickLink.selectedIndex].value;
window.location.href = URL;
}


function comment(tempname)
{
	url = baseURL + "/" + (tempname) + ".htm";
var windowSettings = 'width=600,height=415,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function my_win(tempname)
{
	url = baseURL + "/launch/" + (tempname) + ".htm";
var windowSettings = 'left=0,top=0,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,';
newwin=window.open("","",windowSettings);
	if (document.all){
		newwin.moveTo(0,0)
		newwin.resizeTo(screen.width,screen.height)
	}
newwin.location=url
}

function book_win(tempname)
{
	url = baseURL + "/books/" + (tempname) + ".htm";
var windowSettings = 'width=600,height=375,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"bookView",windowSettings);
}

function ext_win(tempname)
{
	url =  baseURL + "/launch/" + (tempname) + ".htm";
var windowSettings = 'width=600,height=415,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function medship_win(tempname)
{
	url = baseURL + "/books/medical/" + (tempname) + ".htm";
var windowSettings = 'width=610,height=415,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function ship_win(tempname)
{
	url = baseURL + "/books/general/" + (tempname) + ".htm";
var windowSettings = 'width=600,height=520,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function textbookship_win(tempname)
{
	url = baseURL + "books/textbooks/" + (tempname) + ".htm";
var windowSettings = 'width=600,height=275,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function bestseller_win(tempname)
{
	url = baseURL + "/books/general/" + (tempname) + ".htm";
var windowSettings = 'width=700,height=575,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function loan_win(tempname)
{
	url = baseURL + "/computers/" + (tempname) + ".htm";
var windowSettings = 'width=585,height=250,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function date_win(tempname)
{
	url = baseURL + "/books/general/" + (tempname) + ".htm";
var windowSettings = 'width=600,height=180,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

//This Funtion opens the Book Order Form in a new window
function openBook(tempname)
{
url = secureURL + "/cgi-bin/" + (tempname) + ".cgi";
var windowSettings = 'left=0,top=0,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,';
window.open(url,"orderForm",windowSettings);
}

function return_win(tempname)
{
	url = baseURL + (tempname) + ".htm";
var windowSettings = 'width=600,height=575,left=0,top=0,toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,';
	window.open(url,"",windowSettings);
}

function product_win(tempname)
{
	url = escape(tempname);
	url = baseURL + "/clothing/products/" + url + ".htm";
var windowSettings = 'width=450,height=580,left=10,top=5,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no'	
	window.open(url,"",windowSettings);
	}
	
function product_win_med(tempname)
{
	url = escape(tempname);
	url = baseURL + "/books/medical/products/" + url + ".htm";
var windowSettings = 'width=450,height=580,left=10,top=5,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no'	
	window.open(url,"",windowSettings);
	}	
		
function product_win_sup(tempname)
{
	url = escape(tempname);
	url = baseURL + "/supplies/products/" + url + ".htm";
var windowSettings = 'width=450,height=580,left=10,top=5,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=no'	
	window.open(url,"",windowSettings);
	}		
	
// ANY TIME VALIDATION IS NECESSARY FOR A PRODUCT (SELECTION TO BE MADE FOR THE PRODUCT) USE THIS METHOD, WHICH CALLS ADDTOCART AT THE END
function submitIt(carForm) {
	if (carForm.elements[1].selectedIndex == null || carForm.elements[1].selectedIndex == "undefined") {
		}
	else {	
		colorChoice = carForm.elements[1].selectedIndex	
		if (carForm.elements[1].options[colorChoice].value == "1") {
			alert("Please select a size.  Thank You")
			return false;
			}
		else if (carForm.elements[1].options[colorChoice].value == "2") {
			alert("Please select a color.  Thank You")
			return false;
			}
		else if (carForm.elements[1].options[colorChoice].value == "3") {
			alert("Please select your color and size.  Thank You")
			return false;
			}
		else if (carForm.elements[1].options[colorChoice].value == "4") {
			alert("Please choose your item.  Thank You")
			return false;
			}
		else if (carForm.elements[1].options[colorChoice].value == "5") {
			alert("Please choose your amount.  Thank You")
			return false;
			}	
		else if (carForm.elements[1].options[colorChoice].value == "0") {
			alert("Please choose your choice.  Thank You")
			return false;
			}
		}		
		return AddToCart(carForm);
	}
// FOR MOUSEOVER IMAGE CHANGE ON THE PRODUCT POP UP PAGES	
function changeimage(towhat,url){
	if (document.images){
		document.images.targetimage.src=towhat.src
		gotolink=url
	}
}
	function warp(){
		window.location=gotolink
}

	var myimages=new Array()
	var gotolink="#"
function preloadimages(){
	for (i=0;i<preloadimages.arguments.length;i++){
		myimages[i]=new Image()
		myimages[i].src=preloadimages.arguments[i]
	}
}

function openNewWindow (url) {
var form = document.BOOKLIST;
if (form.searchfor.value == null || form.searchfor.value == "") {
	alert("Please enter search criteria.");
	form.searchfor.focus();
	return false;
	}
else {
if (navigator.appName == "Microsoft Internet Explorer") {
        window.open(url, "TextList", "width=640,height=480,scrollbars=yes,resizable=yes,menubar=no,status=yes,location=no,toolbar=no");
    }
else {
        win = window.open(url, "TextList", "width=640,height=480,scrollbars=yes,resizable=yes,menubar=no,status=yes,location=no,toolbar=no");
        win.focus();
    }
}
return true;
}

function openOrdWindow (url) {
if (navigator.appName == "Microsoft Internet Explorer") {
        window.open(url, "OrderWindow", "width=675,height=480,scrollbars=yes,resizable=yes,menubar=no,status=yes,location=no,toolbar=no");
    }
else {
        win = window.open(url, "OrderWindow", "width=675,height=480,scrollbars=yes,resizable=yes,menubar=no,status=yes,location=no,toolbar=no");
        win.focus();
    }
}

function getCookieData(cID) {
    var cIDLen = cID.length;
    var cLen = document.cookie.length;
    var i = 0;
    var cEnd;
    while (i < cLen) {
        var j = i + cIDLen;
        if (document.cookie.substring(i,j) == cID) {
            cEnd = document.cookie.indexOf(";",j);
            if (cEnd == -1) {
                cEnd = document.cookie.length;
            }
            return unescape(document.cookie.substring(j,cEnd));
        }
        i++;
    }
    return "";
}

function popUp(url) {
sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
// self.name = "mainWin";
}
	
function placeFocus() {
	if (document.forms.length > 0) {
	var field = document.forms[0];
	for (i = 0; i < field.length; i++) {
	if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
	document.forms[0].elements[i].focus();
	break;
         }
      }
   }
}