
/**
*This function uses the functions of scriptaculous to define draggable(widgets) and droppable objects(columns)
**/
function startDrag()
{
	Sortable.create(THIRDCOLUMN,{dropOnEmpty:true,tag:'div',containment:[THIRDCOLUMN,SECONDCOLUMN,FIRSTCOLUMN],constraint:false})
	Sortable.create(SECONDCOLUMN,{dropOnEmpty:true,tag:'div',containment:[THIRDCOLUMN,SECONDCOLUMN,FIRSTCOLUMN],constraint:false})
	Sortable.create(FIRSTCOLUMN,{dropOnEmpty:true,tag:'div',containment:[THIRDCOLUMN,SECONDCOLUMN,FIRSTCOLUMN],constraint:false})	
}

/**
 *The function is ajax call when a page is loaded.
 **/
function getContent(isRevert, addTool, caller){
	var http_request = getHttpRequest();
	if(!http_request)
		return false;
	
	http_request.onreadystatechange = function(){loadPage(http_request, addTool, caller);};

	if(isRevert){
	    http_request.open('GET', source+'xml/'+callpath+'_publish.xml?ms='+new Date().getTime(), true);
	}
	else{
	    http_request.open('GET', source+'xml/'+callpath+'.xml?ms='+new Date().getTime(), true);
	}
	http_request.send(null);
	
}

/**
 *The function is called when a page is loaded which populates all the widgets
 **/
function loadPage(http_request, addTool, caller){
    var xmlPage;
    total_cnt++;
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
	    xmlPage = http_request.responseXML.documentElement;
        }else {
            if(ISEDITABLE){	            
                if(confirm('This page does not exist yet. Do you want to create it now?')){
		    getElement('brandingSelect').value= defaultBranding;
		    getElement('layoutSelect').value= defaultLayout;
		    dragCall(true, caller);
		}
            }else {
                alert("The page requested does not exist or has not yet been published.");
	    }
	}
    }

    if(xmlPage){
 
        if (!addTool) {
            versionNumber = xmlPage.getElementsByTagName('version')[0].childNodes[0].nodeValue;
            if(xmlPage.getElementsByTagName('firstversion').length > 0){
                firstVersion = xmlPage.getElementsByTagName('firstversion')[0].childNodes[0].nodeValue;
            }
            if(xmlPage.getElementsByTagName('lastversion').length > 0){
                lastVersion = xmlPage.getElementsByTagName('lastversion')[0].childNodes[0].nodeValue;
            }
        }

        if(ISEDITABLE) {
            if (parseInt(firstVersion) < parseInt(versionNumber)) {
                undoenabled = true;
                //Enable Undo button, by default it should be darked out.
                getElement("btnUndo").className = "enabledundo";
            }else {
                undoenabled = false;
                getElement("btnUndo").className = "disabledundo";
            }
            if (parseInt(versionNumber) < parseInt(lastVersion)) {
                redoenabled = true;
                //Enable Redo button, by default it should be darked out.
                getElement("btnRedo").className = "enabledundo";
            }else {
                redoenabled = false;
                getElement("btnRedo").className = "disabledundo";
            }
        }    

        var propElem = xmlPage.getElementsByTagName('property')[0];
        var tempElem = propElem.childNodes[0];
	getElement('titleSelect').value = getText(tempElem);
	document.title = getElement('titleSelect').value;
	getElement('titlepv').innerHTML = getElement('titleSelect').value ;

        tempElem = propElem.childNodes[1];
	getElement('titleSize').value = getText(tempElem);
        tempElem = propElem.childNodes[2];
	getElement('brandingSelect').value = getText(tempElem);
        tempElem = propElem.childNodes[3];
	getElement('layoutSelect').value = getText(tempElem);

        var creatorElem = xmlPage.getElementsByTagName('creator')[0];
        tempElem = creatorElem.childNodes[0];
	creatorPennKey = getText(tempElem);
        tempElem = creatorElem.childNodes[1];
	creatorFirstName = getText(tempElem);
        tempElem = creatorElem.childNodes[2];
	creatorLastName = getText(tempElem);
        tempElem = creatorElem.childNodes[3];
	dateCreated = getText(tempElem);

        var editorElem = xmlPage.getElementsByTagName('lasteditor')[0];
        tempElem = editorElem.childNodes[0];
	lasteditorPennKey = getText(tempElem);
        tempElem = editorElem.childNodes[1];
	lasteditorFirstName = getText(tempElem);
        tempElem = editorElem.childNodes[2];
	lasteditorLastName = getText(tempElem);
        tempElem = editorElem.childNodes[3];
	dateLastModified = getText(tempElem);

        if(ISEDITABLE){
            getElement('lastupdate').innerHTML = dateLastModified;
            getElement('lastupdtpennkey').innerHTML = lasteditorPennKey;
	    getElement('createdpennkey').innerHTML = (creatorPennKey=="0123456789")?lasteditorPennKey:creatorPennKey;
	}
        writeLastModifiedDate();
                   
	var noOfElem  = xmlPage.getElementsByTagName('elem');
	var elemLength = noOfElem.length;

	for(iWidCount = 0  ; iWidCount< elemLength ; iWidCount++){

			if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == RSSFEEDSTEXT)
			{
			    var node = noOfElem[iWidCount];                          
                            if(xmlPage.getElementsByTagName('itemTitle').length > 0) {
                                showFeed( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , getString(node, 8), getYesNo(node, 9), getYesNo(node,10), getYesNo(node,11), getString(node, 12), getYesNo(node, 13), true);	
                            }else{
                                showFeed( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , getString(node, 8), getYesNo(node, 9), getYesNo(node,10), getYesNo(node,11), getString(node, 12), "Yes", true);	
                            } 
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == PENNTAGSTEXT){
			    var node = noOfElem[iWidCount];
                            if(xmlPage.getElementsByTagName('itemTitle').length > 0) {
                                showFeed( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , getString(node, 8), getYesNo(node, 9), getYesNo(node,10), getYesNo(node, 11), getString(node, 12), getYesNo(node, 13), true);
                            }else {
                                showFeed( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , getString(node, 8), getYesNo(node, 9), getYesNo(node,10), getYesNo(node, 11), getString(node, 12), "Yes", true);
                            }
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == WEBNOTETEXT){
			    try{
				showNote(getCol(noOfElem[iWidCount].childNodes[getIndex(3)].firstChild.nodeValue), noOfElem[iWidCount].childNodes[getIndex(2)].firstChild.nodeValue, noOfElem[iWidCount].childNodes[getIndex(5)].firstChild.nodeValue, noOfElem[iWidCount].childNodes[getIndex(6)].firstChild.nodeValue, noOfElem[iWidCount].childNodes[getIndex(7)].firstChild.nodeValue, true);
			    }catch(err){
				showNote(getCol(noOfElem[iWidCount].childNodes[getIndex(3)].firstChild.nodeValue), noOfElem[iWidCount].childNodes[getIndex(2)].firstChild.nodeValue, "No", noOfElem[iWidCount].childNodes[getIndex(6)].firstChild.nodeValue, "", true);
			    }
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == STANDALONEHEADERTEXT){

                            if(noOfElem[iWidCount].getElementsByTagName('style').length > 0){
                                showStandAloneHeader(getCol(noOfElem[iWidCount].childNodes[getIndex(3)].firstChild.nodeValue), noOfElem[iWidCount].childNodes[getIndex(2)].firstChild.nodeValue, "No", noOfElem[iWidCount].childNodes[getIndex(6)].firstChild.nodeValue, noOfElem[iWidCount].childNodes[getIndex(7)].firstChild.nodeValue, true);
                            }else{
                                showStandAloneHeader(getCol(noOfElem[iWidCount].childNodes[getIndex(3)].firstChild.nodeValue), noOfElem[iWidCount].childNodes[getIndex(2)].firstChild.nodeValue, "No", noOfElem[iWidCount].childNodes[getIndex(6)].firstChild.nodeValue, "header", true);
                            }
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == IMGTOOLTEXT){
			    var node = noOfElem[iWidCount];
                            showTool( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) ,getString(node, 8), true);
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == "libAcct"){
			    showAccount(getCol(noOfElem[iWidCount].childNodes[getIndex(3)].firstChild.nodeValue), noOfElem[iWidCount].childNodes[getIndex(2)].firstChild.nodeValue, noOfElem[iWidCount].childNodes[getIndex(5)].firstChild.nodeValue, true);
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == LIBDETAILSTEXT)
			{
   			    var node = noOfElem[iWidCount];
                            showLibDetails( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , true);
                        }

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == SEARCHTEXT)
			{
			    var node = noOfElem[iWidCount];
                            showSearch( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , true);	
                        }

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == ERESOURCETEXT)
			{
			    var node = noOfElem[iWidCount];
			    showEResource(getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5),  getString(node, 6),  getString(node, 8) , getString(node, 7),  getYesNo(node, 9), true);	
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue == PCERESTEXT)
			{
			    var node = noOfElem[iWidCount];
			    showPceRes( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , true);	
			}

			else if(noOfElem[iWidCount].childNodes[getIndex(1)].firstChild.nodeValue==NAVTOOLTEXT)
			{
			    var node = noOfElem[iWidCount];
			    showNavTool( getCol(getString(node,3)), getNum(node, 2), getYesNo(node, 5), getString(node, 6), getString(node, 7) , true);	
			}
			total_cnt++;
		}
		if(ISEDITABLE){
                    startDrag();
                    // No need to increment version in this call.
                    dragCall(true, caller);
		}	
	}
}

/**
 *This method just sets the variable callpath to the pagename asked for.
 *Which is used to identify it with the specific xml on the server.
 **/
function setPage(){               
    var splitpathname = window.location.pathname.split("/");
    projectPrefix = splitpathname[1] + '/' + splitpathname[2] + '/';
    source = source + projectPrefix;
    splitsource = source + 'html/';
    var strHref = window.location.href;
    var paths = strHref.split(splitsource);
    var path = paths[paths.length-1];
    var pathsunprocessed = path.split("?");		
    var pathprocessed = pathsunprocessed[0];

    if(window.location.search == "")
        ISEDITABLE = false;
    else
        startDrag();
    var pathnames = pathprocessed.split(".");
    var pathname = pathnames[0];
    if(ISEDITABLE){
        callpath = pathname;
    }
    else{
        callpath = pathname+'_publish';
    }
    paths = strHref.split(source);
    path = paths[paths.length-1];
    pathsunprocessed = path.split("?");		
    pathprocessed = pathsunprocessed[0];	
    pathnames = pathprocessed.split("/");
	
    for(var i = 0 ; i < pathnames.length - 1 ; i++)
    {
        dirStruct += pathnames[i] + '/'; 
    }
	
    populateLibArray();
    //populateEResource();
    populateSearchArray();
    createEditLink();
    // Now instead of using hard coded jsp source, we are forming it out of source...
    servletsource = source + servletsource;
}


/**
*This function puts a link on the page through which an user can shift from end user view to editable page.
**/
function createEditLink() {
	if (!ISEDITABLE) {
	    var elem = getElement("editMode");
	    var location = window.location;
	    location = location + "?action=edit";
	    elem.setAttribute("href", location);
	}
}


/**
*This is a synchronous call to Ajax function to get the list of librarys.
**/
function populateLibArray(){
	var objXmlHttp1=null;
	
	objXmlHttp1 = getHttpRequest();
	if(!objXmlHttp1)
		return false;

	objXmlHttp1.open('GET', '/cocoon/librarytools/xml/librarylist.xml?ms='+new Date().getTime(), false);
	objXmlHttp1.send(null);
	if(objXmlHttp1.responseXML.documentElement){
	    var xmlPage = objXmlHttp1.responseXML.documentElement;;
	    var libraries = xmlPage.getElementsByTagName('row');
                    for(iLib=0; iLib< libraries.length; iLib++){
                    	libShortNme[iLib] = libraries[iLib].childNodes[getIndex(0)].firstChild.nodeValue;
                    	libName[iLib] = libraries[iLib].childNodes[getIndex(1)].firstChild.nodeValue;
                    }
                    libArrayPopu = true;
	}
	else 
	    return false;
}

/**
*This is a synchronous call to Ajax function to get the list of search options.
**/
function populateSearchArray(){
    var objXmlHttpSearch=null;
	
    objXmlHttpSearch = getHttpRequest();
    if(!objXmlHttpSearch)
        return false;

    //objXmlHttpSearch.open('GET', '/cocoon/librarytools/xml/searchtoollist.xml?ms='+new Date().getTime(), false);
    objXmlHttpSearch.open('GET', '/cocoon/librarytools/searchtoollist.xml?ms='+new Date().getTime(), false);
    objXmlHttpSearch.send(null);
    if(objXmlHttpSearch.responseXML.documentElement){
        var xmlPage = objXmlHttpSearch.responseXML.documentElement;
	var searchTools = xmlPage.getElementsByTagName('item');
        for(iSearch=0; iSearch< searchTools.length; iSearch++){
            searchID[iSearch] = searchTools[iSearch].childNodes[getIndex(0)].firstChild.nodeValue;
            searchName[iSearch] = searchTools[iSearch].childNodes[getIndex(1)].firstChild.nodeValue;
        }
    }else 
        return false;
}

/**
*This function populates the value for lastmodifieddate when it is the end user view.
**/
function writeLastModifiedDate() {
	if (!ISEDITABLE) {
	    getElement("lastupdate").innerHTML = dateLastModified;
	    getElement('lastupdtpennkey').innerHTML = lasteditorPennKey;
   	    getElement('createdpennkey').innerHTML = creatorPennKey;
	}
}

/**
*This function calls the Ajax function for getting the list of options for the EResource tool.
**/
function populateEResource(){
	var objXmlHttp1=null;
	
	objXmlHttp1 = getHttpRequest();
	if(!objXmlHttp1)
		return false;

	objXmlHttp1.onreadystatechange = function(){processEResource(objXmlHttp1);};
	objXmlHttp1.open('GET', '/cocoon/librarytools/eredforminfo.xml?ms='+new Date().getTime(), false);
	objXmlHttp1.send(null);
}

/**
*Call back function for the Ajax Call to get the list for EResource tool. This processes the response from the  sesrver and parses the xml.
**/
function processEResource(objXmlHttp1){
	var xmlPage;
	if (objXmlHttp1.readyState == 4) {
		if (objXmlHttp1.status == 200) {
			xmlPage = objXmlHttp1.responseXML.documentElement;

		} else {
			alert('This page doesnot exist yet. We are going to create it now.');
		}
	}

	if(xmlPage){
		var comm = xmlPage.childNodes[getIndex(0)].childNodes[getIndex(0)];
		var commData = comm.getElementsByTagName('row');
		for(iCommData=0; iCommData< commData.length; iCommData++){
			commid[iCommData] = commData[iCommData].firstChild.firstChild.nodeValue;
			name[iCommData] = commData[iCommData].lastChild.firstChild.nodeValue;
		}
		var types = xmlPage.childNodes[getIndex(0)].childNodes[getIndex(1)];
		var typesData = types.getElementsByTagName('row');
		for(iTypesData=0; iTypesData< typesData.length; iTypesData++){
			pubrtid[iTypesData] = typesData[iTypesData].firstChild.firstChild.nodeValue;
			publicname[iTypesData] = typesData[iTypesData].firstChild.nextSibling.firstChild.nodeValue;
			sortOrder[iTypesData] = typesData[iTypesData].lastChild.firstChild.nodeValue;
		}
	}
}

