﻿/// <summary>getSoapEnvelope returns a formatted soap xml envelope</summary>
///
/// <param name="wsFunction">the function name to be executed</param>
/// <param name="wsNamespace">the namespace of the function to be executed</param>
/// <param name="paramHash">a hash of the parameters to be sent tot he function</param>
///
/// <remarks></remarks>
function getSoapEnvelope(wsFunction, wsNamespace, paramHash)
{
	var envelope = '';
	envelope += '<?xml version="1.0" encoding="utf-8"?>'
	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/">'
	envelope += ' <soap:Body>'
	envelope += ' <' + wsFunction + ' xmlns="' + wsNamespace + '">'
	if (paramHash.getLength() > 0){
	var keys = paramHash.getKeys();
	var values = paramHash.getValues();
	for (var i = 0; i < paramHash.getLength(); i++)
	{
		envelope += " <" + keys[i] + ">";
		envelope += values[i];
		envelope += "</" + keys[i] + ">";
	}
	}
	envelope += ' </' + wsFunction + '>'
	envelope += ' </soap:Body>'
	envelope += '</soap:Envelope>'
	return envelope;
}		

/// <summary>FormatPhoneNumber formats a string as a phone number</summary>
///
/// <param name="number">string to be formatted</param>
///
/// <remarks></remarks>
function FormatPhoneNumber(number)
{
	
	//
	//	strip out all formatting
	//
	
	number = number.replace(/\(/gi, '');
	number = number.replace(/\)/gi, '');
	number = number.replace(/ /gi, '');
	number = number.replace(/-/gi, '');
	
	//
	//	put in brackets and dash
	//
	
	if (number.length == 10)
	{	
		number = '(' + number;
		number = number.substr(0, 4) + ') ' + number.substr(4);
		number = number.substr(0, 9) + '-' + number.substr(9);
	}
	return number;
}

/// <summary>addLoadEvent is a function that adds events to the page load</summary>
///
/// <param name="func">the function to be added</param>
///
/// <remarks></remarks>
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function EnterKeyPress(ev)
{
    var evt = ev || window.event;
    if(evt.keyCode == '13')
    {
		return true;
    }
    return false;
}
