/************************************
 *
 *  Global config for CW js
 *
 ************************************/

//Global debug param.  Place jsDebug=true in query string to enable.
if (window.location.href.indexOf("jsDebug=true") != -1){
	var debug = true;
}

/**
 * Site initialization function
 */
window.onload = function()
{
	// run onload events
	config.runOnLoadEvents();
}

//alert("Config.js included");
    // Used to be we had hostRoot/prodRoot because there were two different servers, no we have one for current protocol/host and one for the secure protocol/host
    // They may often turn out to the be the same
    var hostRoot = window.location.protocol + '//' + window.location.host + '/';
    var prodRoot = 'https://' + window.location.host + '/';
// Image Sources
    var imageDir = hostRoot + 'images/';

// Submit counter for limiting submits
    var submitCount = 0;
// Cookie Munging, produces array: cookie with all the cookie fields
// Cookie fields:
//  $service_address   # index: 0
//  $service_city      # index: 1
//  $service_state     # index: 2
//  $service_zip       # index: 3
//  $service_type      # index: 4
//  $service_floor     # index: 5
//  $success = false;  # index: 6
//  $service_firstname # index: 7
//  $customer_id       # index: 8
    var c_array = document.cookie.split(/;/);
    var pqcookie = -1;
    var cookies = new Array;
    //alert("Cookie: " + document.cookie);
    for(var c_ind = 0; c_ind < c_array.length; c_ind++) {
        var split_array = c_array[c_ind].split(/=/);
        split_array[0] = split_array[0].replace(' ','');
        cookies[split_array[0]] = split_array[1];
        //alert("cookies[" + split_array[0] + "] = " + cookies[split_array[0]] + " from original: " + split_array[1]);
    }
    if(cookies["PrequalAddress"]) {
        //alert("PrequalAddress cookie found! " + cookies["PrequalAddress"]);
        cookies["PrequalAddress"] = decodeURI(cookies["PrequalAddress"]);
        cookies["PrequalAddress"] = cookies["PrequalAddress"].replace(/%3F/g,':');
        cookies["PrequalAddress"] = cookies["PrequalAddress"].replace(/%3D/g,'=');
        cookies["PrequalAddress"] = cookies["PrequalAddress"].replace(/\+/g,' ');
        pq_array = cookies["PrequalAddress"].split(/:/);
        //alert("pq_array size=" + pq_array.length);
        if(pq_array.length > 0) {pqcookie = new Array;}
        for(var pq_ind = 0; pq_ind < pq_array.length; pq_ind++) {
            //alert("pq_array[" + pq_ind + "] = " + pq_array[pq_ind]);
            var keyval = pq_array[pq_ind].split(/=/);
            //alert("assigning pqcookie[" + keyval[0] + "] = " + keyval[1]);
            pqcookie[keyval[0]] = keyval[1];
            //alert("pqcookie[" + keyval[0] + "] = " + pqcookie[keyval[0]]);
        }
        /*for(var z in pqcookie) {
            document.write("pqcookie[" + z + "] = " + pqcookie[z] + "<br>");
        }*/
    }


// could be made into an all purpose submit function if oop'd
function one_click() {
    submitCount++;
    if(submitCount != 1) {
        alert("Please click the submit button only once.");
        return false;
    } else {
        if( submitCount > 5 ) {
            //alert("Seriously, stop clicking me or I'm going to get mad...");
            return false;
        }
        return true;
    }
}

function popUp(strURL,strType,strHeight,strWidth) {
    var strOptions="";
    if (strType=="console") strOptions="scrollbars,resizable,height="+strHeight+",width="+strWidth;
    if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
    if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
    window.open(strURL, 'newWin', strOptions);
}

function getElem(id) {
    var d = document;
    var element = null;
    if (!element && d.getElementById) {
        element = d.getElementById(id);
        if(element) {
            return element;
        }
    }
    if (!element && d.getElementsByTagName) {
        var tr_elms = d.getElementsByTagName("TR");
        if (tr_elms) {
            for (j=0;j<tr_elms.length;j++) {
                if (tr_elms[j].name == id) {
                    element = tr_elms[j];
                    if(element) {
                        return element;
                    }
                }
            }
        }
    }
    if (!element && d.getElementsByTagName) {
        var inp_elms = d.getElementsByTagName("INPUT");
        if (inp_elms) {
            for (j=0;j<inp_elms.length;j++) {
                if (inp_elms[j].name == id) {
                    element = inp_elms[j];
                    if(element) {
                        return element;
                    }
                }
            }
        }
    }
    return element;
}
function hide(element_name) {
    elem = getElem(element_name);
    if(elem) {
        elem.style.display = 'none';
    }
}
function show(element_name) {
    elem = getElem(element_name);
    if(elem) {
        elem.style.display = '';
    }
}

function addOption( ) {
    var selectBox   = arguments[0];
    var optionValue = arguments[1];
    var optionText  = arguments[--arguments.length]; // This makes text an optional param, will default to optionValue

    var newOption   = document.createElement('option');
    newOption.text  = optionText;
    newOption.value = optionValue;
    try {
        selectBox.add(newOption, null); // standards compliant; doesn't work in IE
    } catch(ex) {
        selectBox.add(newOption); // IE only
    }
}

// returns the selected index of the radioFamily object passed (i.e. var selectedIndex = selectedRadio( document.myForm.radioFamily );)
function selectedRadio( radioFamily ) {
	if( typeof( radioFamily.length ) == "undefined" ) {
		return -1;
	}
	for( var radioCount = 0; radioCount < radioFamily.length; radioCount++ ) {
		if( radioFamily[radioCount].checked == true ) {
			return radioCount;
		}
	}
	return -1;
}
function getSelectedRadioObj(pRadioFamily) {
	if(typeof(pRadioFamily.length) == 'undefined') {
		return pRadioFamily;
	}
	var l = pRadioFamily.length;
	for(var i=0;i<l;i++) {
		if(pRadioFamily[i].checked == true) {
			return pRadioFamily[i];
		}
	}
}




/****************************************
 *  GlobalConfigClass (config) for handling onLoad events (allows stacking)
 *
 *  @author Jesse Brown
 *  @version 1.5
 *
 *  example:
 *  	<script language="javascript">
 *      	config.addOnLoadEvent("document.zipform.address.select();document.zipform.address.focus();");
 *      </script>
 ****************************************/
function GlobalConfigClass() {
	var t = this;
	var onLoadEvents = new Array;

	/**
	 * Event handler so that more than one onload event can be assigned per page.
	 * @param pEvent a string of javascript code that you would normally put in the onload event.
	 */
	t.addOnLoadEvent = function(pEvent) {
		onLoadEvents.push(pEvent);
	}

	/**
	 * method to run all the queued up onload events.
	 * this will be fired by the window.onload event and should only happen once.
	 */
	t.runOnLoadEvents = function() {
		for(var pEvent in onLoadEvents) {
			try{
				eval(onLoadEvents[pEvent]);
			} catch(exc) {
				if(debug) alert(exc + "\nevent input: " + onLoadEvents[pEvent]);
			}
		}
	}

	/**
	 * Method used to get the value of any parameter from the query string.
	 * @param pGetParam (string) name of the param you would like the value for.
	 */
	t.get = function(pGetParam) {
		var h = window.document.location.href;
		var pairs = h.substr(h.indexOf('?') + 1).split('&');
		var l = pairs.length;
		for(var i=0;i<l;i++) {
			var pair = pairs[i].split('=');
			if(pair[0] == pGetParam){
				return pair[1];
			}
		}
		return null;
	}
}
var config = new GlobalConfigClass();


/*var validation = new FormValidationClass();*/