﻿

var abortExecutor;
function On_InvokingRequest(executor, eventArgs){var currentRequest = eventArgs.get_webRequest(); abortExecutor = currentRequest.get_executor();} 
function endRequest(sender, args) {loadCount=-1;return true;if( args.get_error() ){args.set_errorHandled( true );}}



var newHTML;
var newHTMLDiv;
var tMinutes = 180; // 3 hours

function sessionTimer(){      
    tMinutes--;
    if (tMinutes < 5){        
        // Fade in the session alert prompt
        if (document.getElementById('divFade').style.visibility!='visible'){
            fadeBG(false);
        }
        var divSessionAlert = document.getElementById("divSessionAlert");
        divSessionAlert.style.visibility = "visible";        
        divSessionAlert.style.left = (document.body.clientWidth/2)-(document.getElementById('divSessionAlert').offsetWidth/2)+'px';           
        divSessionAlert.style.top = (document.body.clientHeight/2) + 'px';        
    }
    
    if (tMinutes < 2){
        // user took too long to respond, log user out
        window.location.href = "/login.aspx?logout=true";
    }
} 

function extendSession(){
    // Fade out the session alert prompt
    document.getElementById("divSessionAlert").style.visibility = "hidden";
    document.getElementById('divFade').setAttribute('hide','1');
    fadeBG(true);
        
    var wRequest = new Sys.Net.WebRequest();
    wRequest.invoke();
    tMinutes = 180;  
}



function getPageForDiv(page,tdiv,record){
    currPage = page;
    tMinutes = 180;
    //Record history   
    try{
        if(record==null){}
        else{
            if (record == 1){
                if(tdiv==null){tdiv=document.getElementById('mainContent')}
                if(isString(tdiv)){
                    tdiv = document.getElementById (tdiv);
                }
                addHistoryEvent(encrypt(page + "|" + tdiv.id));
            }
        }
    }catch(t){
        return false;
    }
    
    // This function performs a GET Web request 
    // to retrieve  information from the Url specified in 
    // the query string. 
    try{
        if(tdiv==null){tdiv=document.getElementById('mainContent')}
        if(isString(tdiv)){
            tdiv = document.getElementById (tdiv);
        }
        
        var wRequest =  new Sys.Net.WebRequest();
        wRequest.set_url(page);  
        wRequest.set_httpVerb("GET");
        wRequest.set_userContext(tdiv);
        wRequest.add_completed(returnPageForDiv);
        Sys.Net.WebRequestManager.add_invokingRequest(On_InvokingRequest);
        wRequest.invoke();  
        loadCount=0;
        displayLoading();
        return false;
    }catch(t){
        return false;
    }
}



function getPageForDivFade(page,tdiv,record){
    if(AnimationsOn==false){
        return getPageForDiv(page,tdiv,record);
    }
    currPage = page;
    tMinutes = 180;
    //Record history   
    try{
        if(record==null){}
        else{
            if (record == 1){
                if(tdiv==null){tdiv=document.getElementById('mainContent')}
                if(isString(tdiv)){
                    tdiv = document.getElementById (tdiv);
                }            
                var o = document.getElementById ('pnlPartSearch')
                if(tdiv == document.getElementById('mainContent') && typeof o !='undefined' && o!=null){
                   //if leaving from a page that had the part search, CLEAR the listeners
                   clearListeners();
                }
                addHistoryEvent(encrypt(page + "|" + tdiv.id));
            }
        }
    }catch(t){
        return false;
    }
    
    // This function performs a GET Web request 
    // to retrieve  information from the Url specified in 
    // the query string. 
//    try{
        if(tdiv==null){tdiv=document.getElementById('mainContent')}
        if(isString(tdiv)){
            tdiv = document.getElementById (tdiv);
        }
	fadeDiv(tdiv,true,"")
        var wRequest =  new Sys.Net.WebRequest();
        wRequest.set_url(page);  
        wRequest.set_httpVerb("GET");
        wRequest.set_userContext(tdiv);
        wRequest.add_completed(returnPageForDivF);
        Sys.Net.WebRequestManager.add_invokingRequest(On_InvokingRequest);
        wRequest.invoke();  
        loadCount=0;
        displayLoading();
        return false;
//    }catch(t){
//        return false;
//    }
       
}


function isString() {
    if (typeof arguments[0] == 'string') return true;
    if (typeof arguments[0] == 'object') {  
        try{
            var criterion =  arguments[0].constructor.toString().match(/string/i); 
            return (criterion != null);  
        }catch(t){return false;}
    }
}



function returnPageForDiv(executor, eventArgs) {
    // This the handler for the Web request completed event
    // that is used to display return data.
    loadCount=-200;
    Sys.Net.WebRequestManager.remove_invokingRequest(On_InvokingRequest);
    if(executor.get_responseAvailable()) {
        //newHTMLDiv=executor.get_userContext();
        //newHTML=executor.get_responseData();
        sethtml(executor.get_webRequest().get_userContext(),executor.get_responseData());
        try{if(ie6FixPNG){ie6FixPNG();}}catch(t){}
	    window.setTimeout ('resizeHeights()',500)
    }

}

function returnPageForDivF(executor, eventArgs) {
    // This the handler for the Web request completed event
    // that is used to display return data.
    loadCount=-200;
    Sys.Net.WebRequestManager.remove_invokingRequest(On_InvokingRequest);
    if(executor.get_responseAvailable()) {
        newHTMLDiv=executor.get_webRequest().get_userContext();
        newHTML=executor.get_responseData();
        //sethtml(executor.get_userContext().id,executor.get_responseData());
        fadeInRtrnDiv();
    	window.setTimeout ('resizeHeights()',500)
    }
	
}

function fadeInRtrnDiv(){
            if (fading==true){window.setTimeout(fadeInRtrnDiv,50);return false;}
            fadeDiv(newHTMLDiv, false,"")
            sethtml(newHTMLDiv.id,newHTML)
            newHTMLDiv.style.display='';
            newHTMLDiv.style.visibility="visible";
            try{if(ie6FixPNG){ie6FixPNG();}}catch(t){}
}


function getPageFromASMXF(page, menuitem){
    //document.body.style.overflowX='hidden';
    //SetDefault(menuitem);
    fadeDiv(document.getElementById ('mainContent'),true,"")
    Navigation.Functions.getHTMLFromPage(page, returnPageASMX)
    return false;
    
}
    

function returnPageASMX(rstring){
    newHTML=rstring;
    fadeInDivASMX();
}

function fadeInDivASMX(){
        if (fading==true){window.setTimeout(fadeInDivASMX,50);return}
        fadeDiv(newHTMLDiv,false,"")
        sethtml(newHTMLDiv,newHTML)
        try{if(ie6FixPNG){ie6FixPNG();}}catch(t){}
}
var loadCount=0;
function displayLoading(){
            tdiv=document.getElementById ('divLoading');
            
            if (loadCount<0){
                tdiv.style.display='none';
                return;
            }
            loadCount+=1;
            if(loadCount>=2){
                var IpopTop = 200;
                var IpopLeft = (document.body.clientWidth - 200) / 2;
                tdiv.style.left=IpopLeft + document.body.scrollLeft+'px';
                tdiv.style.top=IpopTop + document.body.scrollTop+'px';
                tdiv.style.position = "absolute"; 
                tdiv.style.display = "block";    
            
            }
            window.setTimeout (displayLoading,1000);
            
            
    }




/////////////
//Function to set a div with a string of html and run javascript
/////////////        
        
        
        
        function sethtml(tDiv,content) { 
        //No comments, I didn't make this
         var search = content; 
         var script; 
          
         var is_singleq = 0; var singleq = "'"; 
         var is_doubleq = 0; var doubleq = '"'; 
         var is_escaped = 0; var escap = "\\"; 
         var layer = 0; 
    
         while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/)) { 

            search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length); 
            if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break; 
            block = search.substr(0, search.indexOf(RegExp.$1)); 
            search = search.substring(block.length + RegExp.$1.length); 
            while(func = block.match(/(function(.+?)\((.*?)\)\s*\{)/)) { 
                //Make sure we want to run this
                if (block.indexOf ("Sys.Application.initialize();")==-1 
                    && block.indexOf ("var abortExecutor;")==-1
                    && block.indexOf ("Sys.WebForms.PageRequestManager._initialize")==-1
                    && block.indexOf ("function endRequest")==-1
                    ){
                        eval(block.substr(0,block.indexOf(RegExp.$1))); 
                    }
               // for evaluating non functions 
                
               block = block.substr(block.indexOf(RegExp.$1) + RegExp.$1.length); 
               name = RegExp.$2; 
               param = RegExp.$3; 
                
               is_singleq = 0; 
               is_doubleq = 0; 
               is_escaped = 0; 
               layer = 0; 
                
                
               for(i=0;i<block.length;i++) { 
                  c = block.substr(i,1); 
                   
                  if ((is_singleq || is_doubleq) && is_escaped) { 
                     is_escaped = 0; 
                  } else if (!is_doubleq && (c==singleq)) { 
                     is_singleq = !is_singleq; 
                  } else if (!is_singleq && (c==doubleq)) { 
                     is_doubleq = !is_doubleq; 
                  } else if ((is_singleq || is_doubleq) && (c==escap)) { 
                     is_escaped = 1; 
                  } else if ( c=="{") { 
                     layer++; 
                  } else if ( c=="}") { 
                     if ( layer==0 ) { 
                        break; 
                     } 
                     layer--; 
                  } 
               } 
                
               code = block.substr(0,i-1); 
               block = block.substr(i +1); 
                
               code = code.replace(/\n/g, '\\n'); 
               code = code.replace(/\r/g, '\\r'); 
               code = code.replace(/'/g,"\\'"); 
               if (name.replace(/^\s*/, "").replace(/\s*$/, "").length>0) {
                   //Make sure we want to run this
                   if ( eval('typeof '+name) ==  'undefined') {
                    
                       if(
                        code.indexOf("var currentRequest = eventArgs.get_webRequest();")==-1
                        && name!="endRequest"
                        
                        ){ 
                        eval(name + " = new Function('"+param+"','"+code+"');"); 
                        }else{alert(name);}
                    }
                }
            } 
            //Make sure we want to run this
            if (block.indexOf ("Sys.Application.initialize();")==-1 
                && block.indexOf ("function endRequest")==-1
                && block.indexOf ("var abortExecutor;")==-1
                && block.indexOf ("Sys.WebForms.PageRequestManager._initialize")==-1){
            
            
                eval(block); // for evaluating non functions 
            
            }
         } 
         if(isString(tDiv)){
            tDiv = document.getElementById (tDiv);
        }
        if (content.indexOf('<form')>=0 ){content=clearContent(content);}
        if(content.indexOf('id="'+tDiv.id+'"')>=0 || content.indexOf("id='"+tDiv.id+"'")>=0){
            //If we are filling a div with something that has the same ID inside, we assume
            //that we need to trim the content to only get the content inside the same ID
            var tempDiv = document.createElement('div');
            tempDiv.id='tsdiv';
            document.body.appendChild(tempDiv);
            tempDiv=document.getElementById('tsdiv');
            tempDiv.style.display='none';
            var loc = content.indexOf('id="'+tDiv.id+'"');
            if (loc<0){loc=content.indexOf("id='"+tDiv.id+"'");}
            content=content.substring(content.lastIndexOf("<div", loc));
            content=content.replace('id="'+tDiv.id+'"','id="NEW_'+tDiv.id+'"')
            content=content.replace("id='"+tDiv.id+"'","id='NEW_"+tDiv.id+"'")

            tempDiv.innerHTML=content;

            tDiv.innerHTML=document.getElementById('NEW_'+tDiv.id).innerHTML;
            try{
                if(document.all){tempDiv.outerHTML="  ";}else{tempDiv.innerHTML="";tempDiv.parentNode.removeChild(tempDiv);}
            }catch(Er){
                tempDiv.parentNode.removeChild(tempDiv);
            }
            
            


        }else{
	    $(tDiv).html(content);
            //tDiv.innerHTML=content; 
        }
         loadCount=-200;
      } 

    function clearContent(content){
        //if the page is returned with a form tag, the page will not load
        //as extra precaution, we clear the viewstate 
        start=content.indexOf('<form');
        finish=content.indexOf(">",start)
        content=content.substring(0,start)+content.substring(finish+1)
        start=content.lastIndexOf('<',content.indexOf('__VIEWSTATE'));
        finish=content.indexOf(">",start)
        content=content.substring(0,start)+content.substring(finish+1)
        content=content.replace("</form>","")
        return content;        
    }



if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();




//// Real Simple History Methods...

//var currPage = "default.aspx";
//function historyChange(newLocation, historyData) {
//    var historyMsg = (typeof historyData == "object" && historyData != null
//	    ? historyStorage.toJSON(historyData)
//	    : historyData
//    );	   
//    
//    if (newLocation != ""){    
//        var ar = new Array();
//        var str = decrypt(newLocation);
//        ar = str.split("|")
//        if (ar[1] == "") ar[1] = null;        
//        getPageForDivFade(ar[0],ar[1],0); 
//    }
//    else {        
//        getPageForDivFade(window.location.href.replace('#',''),"mainContent",0);
//    }
//};

//window.dhtmlHistory.create({        toJSON: function(o) {                return JSON.stringify(o);        }        , fromJSON: function(s) {                return JSON.parse(s);        }});

//window.onload = function() {
//    //initialize our DHTML history
//    dhtmlHistory.initialize();    
//    //subscribe to DHTML history change events
//    dhtmlHistory.addListener(historyChange);
//    
//    addHistoryEvent(encrypt('/default.aspx|mainContent'));
//    
//};

//addHistoryEvent = function(pg) { 
//    dhtmlHistory.add(pg,'Arbitech');	    
//}

//function encrypt(str)
//{
//    //return str;
//	var encrypted = str.split("");
//	encrypted = encrypted.reverse();	
//	for (var i = 0; i < encrypted.length; i++)
//	{
//		if (encrypted[i] == "a")				
//			encrypted[i] = "!";
//		if (encrypted[i] == "e")				
//			encrypted[i] = "@";
//		if (encrypted[i] == "o")				
//			encrypted[i] = "$";
//		if (encrypted[i] == "u")				
//			encrypted[i] = "^";
//	}
//    return encrypted.join("");
//}

//function decrypt(str)
//{
//   // return str;
//	var decrypted = str.split("");
//	decrypted = decrypted.reverse(); 	
//	
//	for (var i = 0; i < decrypted.length; i++)
//	{
//		if (decrypted[i] == "!")				
//			decrypted[i] = "a";
//		if (decrypted[i] == "@")				
//			decrypted[i] = "e";
//		if (decrypted[i] == "$")				
//			decrypted[i] = "o";
//		if (decrypted[i] == "^")				
//			decrypted[i] = "u";
//	}	
//    return decrypted.join("");
//}
