//GetComponentAction URL
var GETCOMPONENTACTIONURL="http://www.ti.com/wcmcomponents/docs/getcomponentaction.tsp";

//Proxy URL
var hostname = location.hostname;

if(hostname=="webcontent.itg.ti.com")
var PROXYURL="https://webcontent.itg.ti.com/iw-cc/ccpro/JSPProxyScript.jsp?url=";
else
var PROXYURL="http://www.ti.com/lsds/iw/admin/JSPProxyScript.jsp?url=";

//Modify flash component path
var FLASHHREFPATH="http://focus.ti.com";




//Function creates XMLHttpRequest
function createXMLHttpRequest()
{
    try
    {
        requestXMLHttp = new XMLHttpRequest();
    }
    catch (trymicrosoft)
    {
        try
        {
            requestXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft)
        {
            try
            {
                requestXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed)
            {
                requestXMLHttp = false;
            }
        }
    }
    if (!requestXMLHttp)
        alert("Error initializing XMLHttpRequest!");
    return requestXMLHttp;
}



//Function builds the url from proxyUrl and componentActionUrl depending on the arguments passed to the function
function getContentUrl(argumentString)
{
    
    var url = GETCOMPONENTACTIONURL;
    if(argumentString!=null){
    argumentString=argumentString.replace(/^\s+|\s+$/g,"");
    url+="?"+argumentString;
    }
    //return PROXYURL+encodeURIComponent(url);
return PROXYURL+url;
}

function trim(inputString) {
	return inputString.replace(/^\s+|\s+$/g,"");
}

//JSON Request
function addJSONScript(url) 
{
	 var script = document.createElement('script');
	script.src = url;
	document.body.appendChild(script);
}

//JSON Request Callback
function callback (data) 
{
    var respstatus = data.response.responseStatus;
    //If successful
    if(respstatus=="success"){
        //alert("Success");
        //PageAttributeJsonComponent
        if(data.response.pageAttributesValue!=null){
//alert("Success1");
            var keywords=data.response.pageAttributesValue.keywordMeta;
            var hitBoxTitle=data.response.pageAttributesValue.hitBoxTitle;
            var description=data.response.pageAttributesValue.descriptionMeta;
            var browserTitle=data.response.pageAttributesValue.browserTitle;
            //Set browser title
            document.title=browserTitle;
            window.status=document.title;	
//alert(browserTitle);
//alert(keywords);
//alert(description);
            //Create script element
            var scriptElement=document.createElement("script");
            scriptElement.setAttribute("language", "JavaScript");
            scriptElement.setAttribute("type", "text/javascript");
           

            var scriptLine1 = "var tiPageName;\n tiPageName = \"" + hitBoxTitle + "\";\n"
            scriptElement.text = scriptLine1;
            document.getElementsByTagName('head')[0].appendChild(scriptElement);
            
            
            //Keyword
            var metaTags=document.getElementsByTagName("meta");
            var foundKeyword=false;
            var foundDescription=false;
            for(i=0; i<metaTags.length; i++){
                if(metaTags[i].name=="keywords"){
                    metaTags[i].content=keywords;
                    foundKeyword=true;
                }
                if(metaTags[i].name=="description"){
                    metaTags[i].content=description;
                    foundDescription=true;
                }
            }
            if(!foundKeyword){
                var metaKElement=document.createElement("META");
                metaKElement.setAttribute("NAME", "keywords");
                metaKElement.setAttribute("CONTENT", keywords);
                document.getElementsByTagName('head')[0].appendChild(metaKElement);
            }
            //Description
            if(!foundDescription){
                var metaDElement=document.createElement("META");
                metaDElement.setAttribute("NAME", "description");
                metaDElement.setAttribute("CONTENT", description);
                document.getElementsByTagName('head')[0].appendChild(metaDElement);
            }
//alert("SuccessFinal");
        } else if(data.response.someValue!=null){
    //Some other Json component
    }
    }		    
}

//TabbedContent Tab Functionality
function showTabOnLoad(){
        var foundObj = true;
        var isFirstTab = true;
        var iCount = 0;
        while (foundObj) {
            iCount++;
            try {
                var tabObj=document.getElementById('tab' + iCount);
                if (tabObj != null) {
                    foundObj = true;
                    var tabContentObj = document.getElementById('tab' + iCount + "content");
                    if(isFirstTab){
                        tabObj.parentNode.setAttribute("id", "active");
                        tabContentObj.style.display = "inline";
                        isFirstTab = false;
                    } else {
                        tabContentObj.style.display = "none";
                        tabObj.parentNode.removeAttribute("id");
                    }
                }else{
                    foundObj = false;
                }
            } catch (Error) {
                foundObj = false;
            }
        }
    }

    function showTabContent(tabid) {
        document.getElementById(tabid+"content").style.display = "inline";
        hideOtherContent(tabid);
    }

    function hideOtherContent(tabid) {
        var foundObj = true;
        var iCount = 0;
        while (foundObj) {
            iCount++;
            try {
                var tabObj=document.getElementById('tab' + iCount);
                if (tabObj != null) {
                    foundObj = true;
                    var tabContentObj = document.getElementById('tab' + iCount + "content");
                    var currentTabId= tabObj.id;
                    if(currentTabId != tabid){
                        tabContentObj.style.display = "none";
                        tabObj.parentNode.removeAttribute("id");
                    } else{
                        tabObj.parentNode.setAttribute("id", "active");
                    }
                }else{
                    foundObj = false;
                }
            } catch (Error) {
                foundObj = false;
            }
        }
    }


//Run Flash Component Script
function runScript(str){
    var startPattern=/<[\s]*script[^>]*>/i;
    var endPattern=/<\/[^(s|>)]*script[^>]*>/i;
    var xIndex,xLength,yIndex,yLength;
    var startMatch = startPattern.exec(str);
    if (startMatch != null) {
        xIndex=startMatch.index;
        strMatchWord="";
        for (i = 0; i < startMatch.length; i++) {
            strMatchWord=strMatchWord+startMatch[i];
        }
        xLength=strMatchWord.length;
        var endMatch = endPattern.exec(str);
        if (endMatch != null) {
            yIndex=endMatch.index;
            strMatchWord="";
            for (i = 0; i < endMatch.length; i++) {
                strMatchWord=strMatchWord+endMatch[i];
            }
            yLength=strMatchWord.length;
        }
        eval(str.substr(xIndex+xLength,yIndex-xIndex-xLength));
    }
}


//Process a FlashObject
function processFlashObject(str){
    var FlashPattern1 = /FlashObject\(\"\/(^\")*/g;
    str=str.replace(FlashPattern1, "FlashObject(\""+FLASHHREFPATH+"/$1");
    return str;
}

//Correct asset paths
function correctAssetPath(str){
    var pathPattern;
    pathPattern=/iw-resources\/graphics/g;
    str=str.replace(pathPattern,"media/images");
    pathPattern=/iw-resources\graphics/g;
    str=str.replace(pathPattern,"media\\images");
    return str;
}


function setPageAttributes(hitBoxTitle) 
       {
            // var script = document.createElement('script');
            // document.body.appendChild(script);

            var scriptElement=document.createElement("script");
            scriptElement.setAttribute("language", "JavaScript");
            scriptElement.setAttribute("type", "text/javascript");
           

            var scriptLine1 = "var tiPageName;\n tiPageName = \"" + hitBoxTitle + "\";\n"
            scriptElement.text = scriptLine1;
            document.getElementsByTagName('head')[0].appendChild(scriptElement);
}

