

function flipDiv(divToFlip,visible){
	
	 
	if(visible != "undefined"){
		if(visible === true){
			divToFlip.style.display = 'block';
		}
		else if(visible === false){
			divToFlip.style.display = 'none';
		}
	}
	else{
		if(divToFlip.style.display == 'none')
		{
			divToFlip.style.display = 'block';
		}
		else
		{
			divToFlip.style.display = 'none';
		}
	}
};


function openlinks_newWindow(targeturl)
{

	if(targeturl.indexOf('http:') > -1){
		
		if(targeturl.indexOf(window.location.host) > -1  || targeturl.indexOf('calcxml') > -1){
			trackExternalLinks(targeturl);
			window.open(targeturl);
		}
		else{
			var agree=confirm("You are now leaving the Farm Bureau Financial Services Web site. To proceed, please click \"OK\" below. Or, click \"Cancel\" to return to the Farm Bureau Financial Services Web site.");
			if (agree)
			{
				trackExternalLinks(targeturl);
				window.open(targeturl);
			}else
			{
				//window.close();
			}
		}
	}
	else{
		trackExternalLinks(targeturl);
		window.open(targeturl);
	}
}

function openlinks_location(targeturl)
{
		
	if(targeturl.indexOf('http:')>-1){
		
		if(targeturl.indexOf(window.location.host) > -1  || targeturl.indexOf('calcxml') > -1){
			trackExternalLinks(targeturl);
			window.location.href=targeturl;
		}
		else{
			var agree=confirm("You are now leaving the Farm Bureau Financial Services Web site. To proceed, please click \"OK\" below. Or, click \"Cancel\" to return to the Farm Bureau Financial Services Web site.");
			if (agree)
			{
				trackExternalLinks(targeturl);
				window.location.href=targeturl;
			}
		}
	}
	else{
		trackExternalLinks(targeturl);
		window.location.href=targeturl;
	}
}

function trackExternalLinks(name)
{
	pageTracker._trackEvent("Link", "Click", name);
}

function openRelatedLinks(targeturl, n_wind)
{
	if(n_wind=="0"){
		openlinks_location(targeturl);
	}
	else{
		openlinks_newWindow(targeturl);
	}
}


String.prototype.trim = function()
	{
     return this.replace(/^\s+|\s+$/g,"");
	}
/*
Encapsulate web service (.NET) call using Mootools ajax library mootools-1.2.1-core.js
    
Usage example:
        
var ws = new WebService();
ws.setArguments(new Hash({ 'agentName': 'daniel','location':'somewhere'}));
ws.setMethod("GetOfficesByLocationAndAgentName");
ws.setUrl("http://localhost:53031/AgentLocatorService.asmx");
ws.setNamespace("http://fbfs.ebusiness.agentlocator.com/");
ws.setCallback(function(result) {
if (result.success) {
alert(result.data.First + " " + result.data.Last);
}
else {
alert("error");
}
});

ws.Invoke();
*/
function WebServiceCall() {

    var _url;
    var _ns;
    var _method;
    var _data;
    var _headers;
    var _callback = null;
    var _argsDic;
    var _result = new Object();
    var _isDebugMode = true;
    var _resultName = "data";

    //Get or set web service url
    this.setUrl = function(newUrl) { _url = newUrl; };
    this.getUrl = function() { return _url; };

    //Get or set web service namespace
    this.setNamespace = function(namesspace) { _ns = namesspace; };
    this.getNamespace = function() { return _ns; };

    //Get or set method name to call
    this.setMethodName = function(newMethod) { _method = newMethod; };
    this.getMethodName = function() { return _method; };

    //Set callback method
    this.setCallback = function(clbck) { _callback = clbck; };

    //Get or set calling method arguments
    this.setArguments = function(argsHash) { _argsDic = argsHash; };
    this.getArguments = function() { return _argsDic; };

    //Get or set debug mode
    this.setDebugMode = function(debug) { _isDebugMode = debug; };
    this.getDebugMode = function() { return _isDebugMode; };
    
    //Get of set result name
    this.setResultName = function(resultName) { _resultName = resultName };
    this.getResultName = function() { return _resultName; };
    
    //Invoke web service
    this.Invoke = function() {
        if (_isDebugMode) {
            debug("INVOKE:\n\nurl: " + _url + "\nmethod: " + _method + "\nnamespace: " + _ns);
        }

		var syncXhr = new XHR({
							  async: true, 
							  method: 'post',
							  onSuccess :function(responseXml){
								        if (_isDebugMode) {
                    debug("RESPONSE:\n\n" + responseXml);
                }
                if (!responseXml || responseXml === '@ERROR@') {
                    if (_isDebugMode) {
                        debug("Invalid response from server!");
                    }
                    handleCallback(false, "Invalid response from server!");
                    return;
                }

                var xmlObj = null;
                if (-1 != window.navigator.appName.indexOf("Internet Explorer")) {
                    xmlObj = new ActiveXObject("Microsoft.XMLDOM");
                    xmlObj.async = "false";
                    xmlObj.loadXML(responseXml.toString());
                } else {
                    xmlObj = new XMLHttpRequest();
                    xmlObj.async = "false";
                    var parser = new DOMParser();
                    xmlObj = parser.parseFromString(responseXml, "text/xml");
                }

                if (xmlObj.documentElement != null) {
					
                    var xmlResult = xmlObj.documentElement.getElementsByTagName(_method + 'Result');
                    
					var nut="";
					for(var i=0;i<xmlResult[0].childNodes.length;i++){
					 nut+= xmlResult[0].childNodes[i].nodeValue;
					}
					nut = nut.replace(/[\n\r\t]/g, '');
					
					try{
                    	var json = eval('(' + nut + ')');
						handleCallback(true, json);
					}catch(e){
						handleCallback(false,"@NO_RESULT@");	
					}	
                }

							  },
							  onFailure : function(instance){
								 handleCallback(false, instance);
							  },
							 
							  urlEncoded:false,
							  headers:{
								  		'Content-Type': 'text/xml; charset="utf-8"'
							  		  }
							  });
	 
	 
		 
	  
	 
		
		syncXhr.send(_url, GetSoapEnvelope(_method, _ns, _argsDic));

	 
   
		
    };

    //Invoke callback function
    var handleCallback = function(success, data) {
        if (_callback) {
            _result.success = success;
            _result[_resultName] = data;
            _callback(_result);
        };
    };

    //Redirect all debug messages here to be handled
    var debug = function(msg) {
            alert(msg);
    };

    //Create SOAP envelope
    var GetSoapEnvelope = function(WebMethod, Namespace, paramHash) {
        var envelope = '';
        envelope += '<?xml version="1.0" encoding="utf-8"?>\n';
        envelope += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\n';
        envelope += ' <soap:Body>\n';
        if (paramHash.keys().length > 0) {
            envelope += ' <' + WebMethod + ' xmlns="' + Namespace + '">\n';
            paramHash.each(function(value, key) {
                envelope += '   <' + key + '>' + value + '</' + key + '>\n';
            });
            envelope += ' </' + WebMethod + '>\n';
        } else {
            envelope += ' <' + WebMethod + ' xmlns="' + Namespace + '" />\n';
        }
        envelope += ' </soap:Body>\n';
        envelope += '</soap:Envelope>';

        if (_isDebugMode) {
            debug("SOAP ENVELOPE: \n\n" + envelope);
        }

        return envelope;
    };

};