var xmlHttp
var currentQuote
var loaded=0
var counter = 0
var t
var quoteText = ""
var allQuotes = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59','60','61','62','63','64','65','66','67','68','69','70','71','72','73','74','75','76','77','78','79','80','81','82','83','84','85','86','87','88','89','90','91','92','93','94','95','96','97','98','99','100','101','102','103','104','105','106',107,'108','109','110','111','112','113','114','115','116','117','118','119','120','121',122,'123','124','125','126','127','128','129',130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261]

var studentQuotes = [1,3,'8','9','10','11','12','16','17','18','21','22','23','26','29','31','32','33','35','36','37','38',42,'46','50','52','54','55','56','57','58','59','60','61','62','63','64','65','66','67','70','80','84','93','97','98','100','101',103,'104','105',110,112,'114','116','117',118,119,120,122,'125','126','128',129,138,141,158,173,179,189,207,223,229,234,245,249]

var bestQuotes = ['3','2','8','9','10','11','12','14','16','17','18','24','25','26','29','31','32','33','35','40','44','45','46','47','50','57','59','60','61','62','63','66','71','73','75','80','93','98','100','101',103,'104',107,110,114,116,'117',118,122,126,'128',138,158,173,179,189,207,229,230,242,244,249,250,251,257]

var funnyQuotes = [156,5,6,'7',8,11,17,27,28,29,31,35,44,49,50,'52','53',55,'60','62','68','69',71,73,75,76,'88','89','90',93,'94','108','109',110,'111','112','115',120,'124','126','127',130,131,134,135,154,155,162,165,168,169,170,171,172,174,179,181,189,190,192,193,197,198,200,201,202,204,205,208,210,216,218,219,220,221,222,224,226,227,228,229,232,233,234,236,238,245,246,247,252,253,254,255,256]

var hiddenQuotes = ['95','97','98',122,257,258,259,260,261]

var languageQuotes = [88,130,132,135,170,171,172,176,178,180,181,190,191,198,199,200,201,202,204,217,218,219,224,225,227,228,232,233,235,246,248]

var simplicityQuotes = [16,24,25,55,59,60,64,101,102,114,117,128,138,143,145,147,148,149,150,151,152,215,231]

var optimizationQuotes = [61,63,78,105,110,115,116,131,145,177,259]

var requirementsQuotes = [14,25,48,52,65,85,91,95,100,102,103,137,186,221,224,258]

var designQuotes = [16,21,24,37,43,45,60,92,103,109,133,134,136,140,153,157,158,159,160,208,221,234,249,251]

var testingQuotes = [5,13,18,19,20,36,57,81,82,83,84,94,97,126,169,193,203,231,239,240,241,242,243,245]

var planningQuotes = [2,3,6,8,9,10,11,12,21,23,70,71,79,120,179,189]

var qualityQuotes = [17,22,23,31,32,33,35,36,38,58,67,69,70,76,112,113,117,118,119,121,133,141,168,185]

var educationQuotes = [62,75,181,122,250]

var maintenanceQuotes = [13,66,99,123,223,229]

var reuseQuotes = [15,46,74,121,186]

var documentationQuotes = [29,33,58,90,111,125,205]

var processQuotes = [56,86,96,98,139,166,167,237]

var professionQuotes = [1,7,26,28,49,53,54,73,77,80,88,89,117,122,174,175,187,188,192,194,206,211,216,234,238]

var usersQuotes = [7,68,72,78,124,156,174,220]

var peopleQuotes = [3,30,34,39,40,41,42,44,45,47,49,75,87,173,188,211,230,247,250,257,260,261]


var numberOfQuotes = allQuotes.length
var retryCount = 0
var currentQuote = 999
var debugStack = ''
var category="All"
var info = ""


function showAllQuotes(){
    var type = getUrlParam('type')
    msg('type is '+type)
    if (type=='studentQuotes') {
			currentQuotes = studentQuotes ; 
			category = 'For students' 
			info="These quotes convey some software engineering insight that (I think) is particularly valuable to software engineering students, especially to students following a SE project course. Some have been taken from seminal books that such students are highly encouraged to read <p>"
		}else if (type=='funnyQuotes') { currentQuotes = funnyQuotes ; category = 'Amusing' }
        else if (type=='hiddenQuotes') { 
			currentQuotes = hiddenQuotes ; 
			category = 'Hidden gems' 
			info = "Valuable software engineering insights I found hidden in the books/articles I've read. <p>"
		}else if (type=='languageQuotes') { 
			currentQuotes = languageQuotes ; 
			category = 'Quotes about languages' 
			info="These are quotes about programming languages, operating systems, technologies etc. <b>I don't necessarily agree with <i>all</i> of them </b>, but I still list them here because they represent interesting points of view <p>"
		}else if (type=='allQuotes') { currentQuotes = allQuotes ; category = 'All quotes' }
        else if (type=='simplicityQuotes') { currentQuotes = simplicityQuotes ; category = 'Quotes about simplicity' }
        else if (type=='optimizationQuotes') { currentQuotes = optimizationQuotes ; category = 'Quotes about optimization' }
        else if (type=='requirementsQuotes') { currentQuotes = requirementsQuotes ; category = 'Quotes about requirements'; info='These are quotes related to requirement gathering/analysis and product features'}
        else if (type=='designQuotes') { currentQuotes = designQuotes ; category = 'Quotes about design/architecture'; info='These are quotes related to software design/designers and architrecture/architects'}
        else if (type=='testingQuotes') { currentQuotes = testingQuotes ; category = 'Quotes about testing/bugs'; info='These are quotes related to software testing and bugs'}
        else if (type=='planningQuotes') { currentQuotes = planningQuotes ; category = 'Quotes about planning/scheduling/estimating'; info='These are quotes related to project planning, scheduling, and estimating'}
        else if (type=='qualityQuotes') { currentQuotes = qualityQuotes ; category = 'Quotes about code quality'; info='These are quotes related to code quality (also includes some quotes about overall product quality'}
        else if (type=='educationQuotes') { currentQuotes = educationQuotes ; category = 'Quotes about SE education'; info='These are quotes related to software engineering education'}
        else if (type=='maintenanceQuotes') { currentQuotes = maintenanceQuotes ; category = 'Quotes about software maintenance'; info='These are quotes related to software maintenance'}
        else if (type=='reuseQuotes') { currentQuotes = reuseQuotes ; category = 'Quotes about software reuse'}
        
        else if (type=='documentationQuotes') { currentQuotes = documentationQuotes ; category = 'Quotes about documentation'}
        else if (type=='processQuotes') { currentQuotes = processQuotes ; category = 'Quotes about software processes/standards'; info='These are quotes related to software processes, standards, and guidelines'}
        else if (type=='professionQuotes') { currentQuotes = professionQuotes ; category = 'Quotes about software eigineering profession'; info='These are quotes related to our profession: i.e., related to our industry, our craft, our practitioners'}
        else if (type=='usersQuotes') { currentQuotes = usersQuotes ; category = 'Quotes about software users/usability'; info='These are quotes related to users and usability'}
        else if (type=='peopleQuotes') { currentQuotes = peopleQuotes ; category = 'People-related quotes'; info='These are quotes related to staffing, managing, and teamwork'}
        
        
        else { type=='bestQuotes'; currentQuotes = bestQuotes; category = 'Top rated' }
    
    numberOfQuotes = currentQuotes.length
    displayTextInSpan("info",info)
    
    displayTextInSpan("category",category + ' [ Total = '+numberOfQuotes+ ' ]')
    
    if(numberOfQuotes!=0){
		msg(currentQuotes+'_'+numberOfQuotes)
		loaded = 0
		counter = 1
		msg("starting loadquotes loop")
		currentQuote = currentQuotes[counter-1]
		showCurrentQuote(currentQuote)
		msg('going to load rest of the quotes')
		loadQuotes()
	} else {
	    displayTextInSpan("quotes","<p>Sorry, currently there are no quotes in this category...")
	}
}


function GetXmlHttpObject(handler){ 
    msg("creating xmlHttpObject")
	var objXmlHttp=null
/**
	if (navigator.userAgent.indexOf("Opera")>=0){
		document.getElementById("quotes").innerHTML="Sorry, this doesn't work in opera, please use IE or Firefox ..." 
		return 
	}else if (navigator.userAgent.indexOf("MSIE")>=0){
		  msg('MSIE browser') 
		  var strName="Msxml2.XMLHTTP"
		  if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP"
		  } 
		  try { 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		  } catch(e){ 
			//alert("Error. Scripting for ActiveX might be disabled") 
			//return 
		  } 
	} else if (navigator.userAgent.indexOf("Mozilla")>=0){
	  msg('Mozilla browser')
	  objXmlHttp=new XMLHttpRequest()
	  objXmlHttp.onload=handler
	  objXmlHttp.onerror=handler 
	  return objXmlHttp*/
	  
	if (typeof XMLHttpRequest != "undefined") {
        objXmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            objXmlHttp = new ActiveXObject(aVersions[i]);
        } catch (oError) {
            //Do nothing
        }
      }
    }
    if (objXmlHttp == null) throw new Error("XMLHttp object could not be created.");
	
	objXmlHttp.onreadystatechange=handler
	return objXmlHttp
} 

function displayTextInSpan(spanID,text){
    document.getElementById(spanID).innerHTML=text
}


function loadQuotes(){
    msg("loaded="+loaded+" counter="+counter);
	if(loaded==numberOfQuotes){
		msg("finished loop")
		clearTimeout(t)
		return
	}else if(loaded==counter){
	    msg("going to load another quote")
		counter++
		currentQuote = currentQuotes[counter-1]
		showCurrentQuote(currentQuote)
	}
	msg("end of iter:loaded="+loaded+" counter="+counter);
	t=setTimeout("loadQuotes()",100)
}

function showCurrentQuote(q){
    var url="../quotes/quote"+q+".htm" 
    msg(url)
	xmlHttp=GetXmlHttpObject(stateChanged)
	xmlHttp.open("GET", url , true)
	setTimeout("xmlHttp.send(null)",100)
	msg('showCurrentQuote complete') 
}

function stateChanged(){ 
    msg('state changed')
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	    msg("loading complete, ready state: "+xmlHttp.readyState)
	    createAndDisplayQuoteText()
	} else {
	    msg("loading not complete, ready state: "+xmlHttp.readyState)
	}
} 

function createAndDisplayQuoteText(){
	quoteText += "<table border=\"0\" width=\"100%\" id=\"table1\"><tr><td width=\"23\">[<a href=\"http://www.comp.nus.edu.sg/~damithch/quotes/quote"+currentQuote+".htm\">"+counter+"</a>]</td>"
	quoteText += "<td rowspan=\"2\"><table width=\"100%\" BORDER=1 FRAME=BOX RULES=NONE bordercolor=\"#C0C0C0\" cellpadding=\"10\" ><tr><td>" 
	quoteText += xmlHttp.responseText +"</td></tr></table></td></tr><tr><td width=\"23\">&nbsp;</td></tr></table><p/>"
	if(loaded==(numberOfQuotes-1)){displayText=quoteText}
	  else{displayText=quoteText+"<p align='center'><img src='http://www.comp.nus.edu.sg/~damithch/images/indicator.white.gif'></p>"}
	displayTextInSpan("quotes",displayText)
	//document.getElementById("quotes").innerHTML=displayText
	
	loaded++
	msg("dispaly complete")
}

function msg(msg){
    //debugStack = debugStack + '<br>'+msg
	//document.getElementById("debug").innerHTML=debugStack
}

function cmsg(msg){ //critical message
    debugStack = debugStack + '<br>'+msg
	document.getElementById("debug").innerHTML=debugStack
}

function getUrlParam( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


//----for quote box ---


function showQuote(){ 
	currentQuote = Math.floor(Math.random()*numberOfQuotes+1)
	getQuote(currentQuote)
} 

function showNextQuote(){ 
	if(currentQuote == numberOfQuotes){currentQuote = 1}else{currentQuote++}
	getQuote(currentQuote)
} 

function showPreviousQuote(){ 
	if(currentQuote == 1){currentQuote = numberOfQuotes}else{currentQuote--}
	getQuote(currentQuote)
} 

function getQuote(q){
	displayTextInSpan("txtHint","<p align='center'><img src='http://www.comp.nus.edu.sg/~damithch/images/indicator.white.gif'></p>")
	var url="http://www.comp.nus.edu.sg/~damithch/quotes/quote"+q+".htm" 
	xmlHttp=GetXmlHttpObject(stateChangedForQuoteBox)
	xmlHttp.open("GET", url , true)
	setTimeout("xmlHttp.send(null)",100)
}

function stateChangedForQuoteBox(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		displayTextInSpan("txtHint",xmlHttp.responseText)
	} 
} 
