function isIE() {
	return (navigator.appVersion.indexOf("MSIE") != -1);
}

function getElementsByClass(searchClass, node, tag)
{
	var classElements = [];
	if (node === null)
		node = document;
	if (tag === null)
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++)
	{
		if (pattern.test(els[i].className))
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};

function rand(n)
{
	return (Math.floor(Math.random()*n+1));
}

function Randum(X)
{
  return (X*(Math.random()%1))|0;
}

function Asc(strString)
{
	return strString.charCodeAt(0);
}

function isNumeric(sText)
{
	var valid = false;
	var regNum = /^\d+$/;
	if(sText.match(regNum))
	{
		valid = true;
	}
	else
	{
		valid = false;
	}
	return valid;	
}


function GetHashNo(url) 
{
	var pos = 1;
	var seed = 0;
	for (var i = 0; i<url.length; i++)
	{
		seed+=parseInt(Asc(url.substr(i,1)), 0);
	}
	return seed;
}


// ***********************************************************************
/* parseUri JS v0.1.1, by Steven Levithan <http://stevenlevithan.com>
Splits any well-formed URI into the following parts (all are optional):
----------------------
- source (since the exec method returns the entire match as key 0, we might as well use it)
- protocol (i.e., scheme)
- authority (includes both the domain and port)
  - domain (i.e., host; can be an IP address)
  - port
- path (includes both the directory path and filename)
  - directoryPath (supports directories with periods, and without a trailing backslash)
  - fileName
- query (does not include the leading question mark)
- anchor (i.e., fragment) */
function parseUri(sourceUri)
{
	var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
		uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
		uri = {};
	
	for(var i = 0; i < 10; i++){
		uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
	}
	/* Always end directoryPath with a trailing backslash if a path was present in the source URI
	Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
	if(uri.directoryPath.length > 0){
//		uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
	}
	return uri;
}
// ***********************************************************************

function getVideoStreamingURL(strURL)
{ 
	var seed = 0;
	var nSerialNumber = 0;
	var hrefUri = parseUri(strURL);
	var strVideoURL = hrefUri.authority + hrefUri.directoryPath + hrefUri.fileName;
	var strFileHash = GetHashNo(parseUri(strVideoURL).fileName);
	seed = GetHashNo(strVideoURL);
	while(seed>1) {
		seed = (seed/10);
	}
	while(nSerialNumber <= 0) {
		nSerialNumber = Math.round(2000*seed);	   
	}
	var fileExtension = hrefUri.fileName.match(/\.(\w+)$/)[1];
	if (fileExtension=='wmv') {
		if (parseUri(strVideoURL).domain=='www.resmed.com') {
//			strVideoURL = 'mms://a'+nSerialNumber+'.v32810c.c32810.g.vm.akamaistream.net/7/'+nSerialNumber+'/32810/v'+strFileHash+'/' + strVideoURL;
			strVideoURL = 'mms://resmedwms.cdnetworks.net/resmedwms' + hrefUri.directoryPath + hrefUri.fileName;
		} else {
//			strVideoURL = 'mms://a'+nSerialNumber+'.v32810c.c32810.g.vm.akamaistream.net/7/'+nSerialNumber+'/32810/v'+strFileHash+'/www.resmed.com/'+ hrefUri.directoryPath + hrefUri.fileName;
			strVideoURL = 'mms://resmedwms.cdnetworks.net/resmedwms' + hrefUri.directoryPath + hrefUri.fileName;
			 
//			strVideoURL = strURL;
		}
	} else {
		if (parseUri(strVideoURL).domain=='www.resmed.com') {
			if (fileExtension=='flv') {
				strVideoURL = 'http://resmed.cdnetworks.net' + hrefUri.directoryPath + hrefUri.fileName;
	//			strVideoURL = 'flashstream/resmed.cdnetworks.net' + hrefUri.directoryPath + hrefUri.fileName;
	//			strVideoURL = strURL;
			} else if (fileExtension=='mp4') {
				strVideoURL = 'http://resmed.cdnetworks.net' + hrefUri.directoryPath + hrefUri.fileName;
	//			strVideoURL = 'flashstream/resmed.cdnetworks.net' + hrefUri.directoryPath + hrefUri.fileName;
	//			strVideoURL = strURL;
			}
		} else {
			strVideoURL = strURL;
		}
	}
	return strVideoURL;
}


function urlencode( str ) {
    // URL-encodes string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urlencode/
    // +       version: 809.1713
    // +   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 urldecode( str ) {
    // Decodes URL-encoded string
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_urldecode/
    // +       version: 809.1713
    // +   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: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('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');
    // *     returns 3: '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'
    
    var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
    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 urlencode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}// }}}


function openWindow(strURL, strWindowName, nWidth, nHeight)
{
	window.open(strURL, strWindowName, 'top=200, left=300, width='+nWidth+',height='+nHeight+', channelmode=no, directories=no, location=no, menubar=no, resizable=0, status=no, scrollbars=no, toolbar=no, dialog=1, minimizable=1",false');
}

function openWindowX(strURL, strWindowName, strOptions)
{
	window.open(strURL, strWindowName, strOptions);
}

