﻿var _saved_scroll_tops = new Object();


function beginRequestHandler(sender, args) {
   
}    

function endRequestHandler(sender, args) {

    init_oll_help();
    
    return true;
    
}    

function toggleDisplay(elid,link,hlid){
    
    var el = $get(elid);
    var el2 = $get(elid+'2');
    
    var filter = getTransition(el);
    
    if(filter){filter.Apply();}
    
    if(el.style.display=='none'){
        
        el.style.display='';
        if(el2)el2.style.display='';
        link.src = link.src.replace('plus','minus');
        link.alt = 'Hide This Section';
        if($get(hlid)) $get(hlid).value = 'true';
        
        //_oll_on_section_visible(hlid);
        
    }else{
        
        el.style.display='none';
        if(el2)el2.style.display='none';
        link.src = link.src.replace('minus','plus');
        link.alt = 'Expand This Section';
        if($get(hlid)) $get(hlid).value = 'false';
        
        //_oll_show_pagehelp();
    
    }
    _oll_show_next_help(hlid);
    if(filter){filter.Play();}
    
    return false;
}

function getTransition(el){

    var ch = el.children ? el.children : el.childNodes;
    for(var i=0;i<ch.length;i++){
        if(ch[i].filters && ch[i].filters.length){
                return ch[i].filters[0];
        }
    }
    return null;

}   

/**
 *
 *
 *
 */   
var ollhelp;
var _postback_element_id = null;

function OLLHelp(containerid,sectionhelpid,itemhelpid,sectionhelplabelid,itemhelplabelid,helptext){

    this.container = $get(containerid);
    this.sectionhelp = $get(sectionhelpid);
    this.sectionhelplabel = $get(sectionhelplabelid);
    this.itemhelp = $get(itemhelpid);
    this.itemhelplabel = $get(itemhelplabelid);
    this.helptext = helptext.oll_helptext;
    
    var fels = _oll_get_elements(['input','select','textarea','a']);
    for(var i=0; i<fels.length; i++){
        _oll_add_event(fels[i],'focus',_oll_show_help);
    }
    
    this.getPosition = _oll_get_position;
    
    setTimeout('_oll_show_next_help(_postback_element_id);',100);

}

OLLHelp.prototype.showHelp = function(e){

    if(!$get('helppage')) return;
    
    var el = e!=null ? _oll_get_event_element(e) : null;
    
    if(el!=null){
        if(el.type=='image' && !el.id.endsWith('_Validate')){
            return;       alert(1);//uc test
        }        
        var nameparts =  el.id.split('_');    
    }

    var helppage = $get('helppage').getAttribute('value');
    var helpsection = this.helptext[helppage];
    
    var focuselement = el;
    var focussection = _oll_find_helpsection_el(el);

    var inputname = null;    
    var sectionname = null;
    

    window.status = '';
    
    if(focuselement==null){
    
        helpsection = this.helptext[helppage];
        focussection = focuselement = $get($get('helppage').getAttribute('firstsection'));        
        focussection.headerheight = 0;
        focussection.helpwidth = 218;
        focussection.left = 20;
        
    }else{
        
        window.status += '...' + el.id.substring(50) + '|' + el.type + '|';
     
        if(focussection==null){
            focussection = focuselement = $get($get('helppage').getAttribute('firstsection'));
            focussection.headerheight = 0;
        }else{
            sectionname = focussection.getAttribute('helpsection');
            helpsection = this.helptext[helppage][sectionname];
            focussection.headerheight = 30;
        }
        
        inputname = focuselement.type=='radio' 
                    ? nameparts[nameparts.length-2] 
                    : nameparts[nameparts.length-1];

        focussection.left = focuselement.id.indexOf('AppSection')>-1 ? 60 : 20;
        
        _postback_element_id = focuselement.id;
    }

    this.element = focussection;
    this.getPosition();    
    
    window.status += helpsection + ':' + sectionname + ':' + inputname + ':' + this.top; 
    
    if(helpsection!=null){    
        
        this.sectionhelplabel.innerHTML = helpsection.label;
        this.sectionhelp.innerHTML = helpsection['instructions'].value;
        
        if(helpsection[inputname]){
    
            this.itemhelp.innerHTML = helpsection[inputname].value;
            this.itemhelplabel.innerHTML = helpsection[inputname].label;
    
        }else{
            
            this.itemhelp.innerHTML = '';
            this.itemhelplabel.innerHTML = '';
            
        }

        //this.container.style.top = (this.top - focussection.headerheight) + 'px';
        this.container.style.left = (focussection.left) + 'px';
        this.container.style.display='block';
        fun(this.top - focussection.headerheight);
    
    }else{
    
        this.container.style.display='none';
    
    }

}
var fun_timeouts = new Array();

function fun(nt){
    
    for(var i = 0; i < fun_timeouts.length; i++){
        clearTimeout(fun_timeouts[i]); //alert(timeout);
    }
    
    var ct = parseFloat(ollhelp.container.style.top);

    if(isNaN(ct))
        ct = ollhelp.container.offsetTop;
   
    if(nt==ct) return;
    
    var intervals = parseInt((nt-ct)/20);  
    
    if(intervals==0){
        
        ollhelp.container.style.top=nt+'px';
        return;

    }else{

        var first = intervals<0 ? intervals : 1;
        var last = intervals<0 ? -1 : intervals;
        
        fun_timeouts = new Array();
        var ft=0;
        for(var i=first ; i<last ; i++){
            fun_timeouts[ft] = setTimeout("ollhelp.container.style.top='"+(ct+(i*20))+"px';",Math.abs(i*10));   
            ft++;
        }
        
        fun_timeouts[ft] = setTimeout("ollhelp.container.style.top='"+nt+"px';",Math.abs(intervals*10));  

    }
}

function _oll_show_pagehelp(e){
    ollhelp.showHelp(null);
}

function _oll_show_help(e){
    ollhelp.showHelp(e);
}

function _oll_find_helpsection_el(el){

    var walk = el;
    
    while (walk!=null && walk.getAttribute){

        if(walk.getAttribute('helpsection')!=null){
            return walk;
        }     
        walk = walk.parentNode;
    }    
    return null;
}

function _oll_element_hidden(el){

    var walk = el;
    
    while (walk!=null){

        if(walk.style){
            
            if(walk.style.display=='none' 
                || walk.style.visiblity=='hidden')
            {
                return true;
            }   
        }
        
        if(walk.type=='hidden'){
            return true;
        } 
                    
        walk = walk.parentNode;
    }    
    return false;
}

function _oll_show_next_help(current_element_id){
    
    //if the current_element_id is not hidden, then it IS the next focus
    var current_element = $get(current_element_id);
    if(current_element 
        && !_oll_element_hidden(current_element)){

        current_element.focus();
        setTimeout("$get('"+current_element_id+"').focus();",100);
        return;   
    }

    //otherwise loop to find an array of possible elements to focus on next
    var possibles = new Array();
    var next_element_possible = false;
    var found_current_element = false;
    var fels = _oll_get_elements(['input','select','textarea','a']);
        
    for(var i=0; i<fels.length; i++){
        
        if(!_oll_find_helpsection_el(fels[i])
            && !fels[i].id.endsWith('SectionVisible') ) //firefox dom support
        {
            continue;
        }
        
        if(next_element_possible 
            && !_oll_element_hidden(fels[i])){
            
            possibles[possibles.length] = fels[i];  
            next_element_possible = false; 
            
            if(found_current_element)
                break;         
        }
        
        if(current_element 
            && current_element.id == fels[i].id){
            
            next_element_possible = true;
            found_current_element = true;
            
        }
        
        if(fels[i].type=='hidden' 
            && fels[i].name.endsWith('SectionVisible')
            && fels[i].value=='true'){

            next_element_possible = true;
        }
    }    
    
    if(possibles.length>0){
        
        var next_element = found_current_element 
                            ? possibles[possibles.length-1] 
                            : possibles[0];
        next_element.focus();
        setTimeout("$get('"+next_element.id+"').focus();",100);               
        
    }else{
        
        setTimeout('_oll_show_pagehelp();',100);
    }
}

function _oll_add_event(o, ev, f, c){
    
    if(c==undefined)c=false;
    if(o.attachEvent){
        o.attachEvent('on' + ev, f);
    }else if(o.addEventListener){
        o.addEventListener(ev, f, c);
    }
}

function _oll_get_event_element(e){
    
	if (!e) var e = window.event;
	
	if (e.target) t = e.target;
	else if (e.srcElement) t = e.srcElement;
	
	if (t.nodeType == 3) t = t.parentNode;
	
    return t;    
}

function _oll_get_elements(names) {

	var elements = new Array();
	for (var i=0;i<names.length;i++) {
		var tags = document.getElementsByTagName(names[i]);
		for (var j=0;j<tags.length;j++) {
			elements.push(tags[j]);
		}
	}
	var testNode = elements[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		elements.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		elements.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return elements;
}

function _oll_get_position(){

    this.top = 0;
    this.left = 0;
    
    var el = this.element;
    
    if(el.style && el.style.position=='absolute'){
        this.top = el.style.top;
        this.left = el.style.left;
    }else{    
        while (el.offsetParent != null) {
            this.top += el.offsetTop;
            this.left += el.offsetLeft;
            el = el.offsetParent;
        }   
    }
    
    el = this.element;
    this.bottom = this.top + el.offsetHeight;
    this.right = this.left + el.offsetWidth;
    
}

function _oll_getkey(e){
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	return code;
}

function openCenterWindow(URL,width,height,style) {

    var w=660, h=590, bw, bh, bl, bt, topPos, leftPos;

    if (width > 0 ){w=width;}

    if (height > 0){h=height;}

    if (document.all){
        bw = screen.width;
        bh = screen.height;
        bw = document.body.clientWidth;
        bh = document.body.clientHeight;
        bl = window.screenLeft;
        bt = window.screenTop;
    }else if (document.layers){
        bw = window.outerWidth;
        bh = window.outerHeight;
        bl = window.screenX;
        bt = window.screenY;
    }

    leftPos = Math.floor((bw-w)/2) + bl;
    topPos = Math.floor((bh-h)/2) + bt;
    window.open(URL, '', style + ',width=' + w + ',height=' + h + ',top=' + topPos + ',left=' + leftPos);
}

/**
 * function _debug()
 * 
 * WRITTEN BY:	Dave Krause			August 28, 2006
 * 
 * DESCRIPTION:
 * HTML Console display for variables in JavaScript.  To enable or disable the function,
 * set the _dbg boolean variable to true or false, or set it to read some other criteria.
 * Set the variable _dbgw to window to use the current window, or null to write the 
 * console in a popup.  
 * 
 * PARAMETERS:
 * 		argument[0] = a descriptive comment (eg js file and line number)
 * 		argument[1] = a comma delimited string of variable names
 * 		argument[2-n] = the variables to write to the console
 * 
 * EXAMPLE:  
 * 		_debug('MarlinCoreA3a.963','paramName,a9,a10,a0.id',paramName,a9,a10,a0.id);   
 */
var _dbgw = null;
var _dbg = location.href.indexOf('_dbg=1')!=-1;
var _debug = _dbg ? function(){
    
    if(_dbgw==null){
        _dbgw = window.open('about:blank');
        _dbgw.document.write('<body></body>');
    }
    
    var _dl = _dbgw.document.getElementById('_dbgel');
    if(_dl==null){
        var dbgrhtml = '<table id="_dbgel" cellspacing="0" cellpadding="0" border="0" style="padding: 0px 0px 2px 4px;"' +
                       'style="border:1pt solid #cc0000; ' +
                       'color:#cc0000; ' +
                       'font-size:10pt; ' +                       
                       'background-color:#ffffff; ' +
                       'zposition:absolute; ' +
                       'width:100%' +                    
                       'ztop:100px; ' +
                       'zleft:300px;"><tr bgcolor="#cc0000">'+
                       '<td><b style="color:white;">JAVASCRIPT CONSOLE</b><td>' + 
                       '<td align="right" style="color:white;"><i>&copy;David Krause 2005,2006</i></td>'+
                       '</tr></table>';
        _dbgw.document.body.insertAdjacentHTML('beforeEnd',dbgrhtml);      
        _dl = _dbgw.document.getElementById('_dbgel');
    }
    
    if(arguments[0]!=null){
	    var rh = _dl.insertRow();
	    var ch = rh.insertCell();
	    ch.colSpan=3;
	    ch.innerHTML = '<b>'+arguments[0]+':</b>';
	    ch.style.borderTop = '1pt solid #cc0000';
	}
    var names = arguments[1].split(',');    
    
    for(var i=2;i<arguments.length;i++){
        
        var val = arguments[i];
       	var name = names[i-2];
       	
        if(typeof(val)=='object'){
        	if(arguments[0]==null){
        		val = val.toString().replace(/\&/g,' &');
        	}else{
	        	_debug(null,name,'[object]');
	        	var obj = arguments[i];
	        	for(var s in obj){
	        		try{
	        			if(obj[s]!=null){
		        			_debug(null,'&nbsp;&nbsp;&nbsp;&nbsp;.'+s,obj[s]);
		        		}
	        		}catch(e){}
	        	}
	        	name = null;
	        }
        }else if(typeof(val)=='function'){
            val = '<pre>'+arguments[i]+'</pre>';
        }else if(typeof(val)=='string' && name.indexOf('HTML')!=-1){
            val = val.replace(/</g,'&lt;').replace(/>/g,'&gt;');
        }
                
        if(name!=null){
	        var r = _dl.insertRow();
	        var c1 = r.insertCell();
	        var c2 = r.insertCell();
	        var c3 = r.insertCell();
        
	        c1.innerHTML = '<nobr>'+name+'</nobr>';        
	        c2.innerHTML = '<i>'+(val=='[object]'?val:typeof(arguments[i]))+'</i>';
	        c3.innerHTML = val+'&nbsp;';
			
			c2.style.color = '#000077';        
			c3.style.color = '#000000';        
			
	        c1.style.width = c2.style.width = '20px';
	        c1.style.verticalAlign = c2.style.verticalAlign = c3.style.verticalAlign = 'top';
	        c1.style.borderTop = c2.style.borderTop = c3.style.borderTop = '1pt dashed #cc0000';
	        c1.style.borderRight = c2.style.borderRight = '1pt dashed #cc0000';
	    }
    }    
    
    
}: function(){};

function isNum(argvalue) 
{
    argvalue = argvalue.toString();
    if (argvalue.length == 0)
        return false;

    for (var n = 0; n < argvalue.length; n++)
        if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
            return false;

    return true;
}


function LuhnCheck(cardNum, cardType) 
{
    if (! isNum(cardNum)) 
    {
        alert('Please enter your credit card number with digits only.');
        return false;
    }

    var no_digit = cardNum.length;
    var oddoeven = no_digit & 1;
    var sum = 0;

    for (var count = 0; count < no_digit; count++) 
    {
        var digit = parseInt(cardNum.charAt(count));
        if (!((count & 1) ^ oddoeven)) 
        {
            digit *= 2;
            if (digit > 9) digit -= 9;
        }
        sum += digit;
    }

    if (sum % 10 == 0)
    {
        if ((cardType == 'V' && cardNum.charAt(0) != '4') 
            || (cardType == 'M' && cardNum.charAt(0) != '5')
            || (cardType == 'D' && cardNum.charAt(0) != '6'))
        {
            alert('Please select the correct credit card type.');
            return false;         
        }
        return true;        
    }
    else
    {
        alert('Please enter a valid credit card number.');
        return false;
    }
}

               
function QuoteExpirationCheck(quoteExpiryDate)
{
    var rightNow = new Date();
    
    if (quoteExpiryDate < rightNow)
    {
        alert('We are sorry but your quote has already been expired. The quote expiration time was ' + expiryDate + '.');
        return false;
    }
    
    return true;                                
}

function LeaseSubmitFinalCheck(yyyy, mm, dd, h, m, s)
{
    if (!QuoteExpirationCheck(new Date(yyyy, mm, dd, h, m, s)))
        return false;
    
    var cardNumber;
    var cardType;
    var formFields = document.forms[0].elements;
    for (var i=0; i < formFields.length; i++)
    {
        if (formFields[i].name.indexOf('$cardnumber') > 0)
            cardNumber = formFields[i].value;
        else if (formFields[i].name.indexOf('$cardtype') > 0)
            cardType = formFields[i].options[formFields[i].selectedIndex].value
    }
        
    return LuhnCheck(cardNumber, cardType);
}
