//var formValidationScriptReady = false;

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 isIE() {
	return (navigator.appVersion.indexOf("MSIE") != -1);
}

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 Hash(url) 
{
	var pos = 1;
	var seed = 0;
	for (var i = 0; i<url.length; i++)
	{
		seed+=parseInt(Asc(url.substr(i,1)));
	}
	return seed;
}

function getElementsByClass(searchClass, node, tag)
{
	var classElements = new Array();
	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;
}

function openWindow(strURL,strFeatures)
{
  window.open(strURL,'',strFeatures);
}

// ***********************************************************************
/* 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;
}
// ***********************************************************************
// add event handlers
// ***********************************************************************

function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function() {
		oldonload();
		func();
		}
	}
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

//function doLoadEvents()
//{
//	setVideoLinks();
//}
//window.onload = doLoadEvents; 

//**********************************************************************
// Below code to be removed when video_links.js is included in the templates
//**********************************************************************

function setVideoAnchors(strClassName, docContext)
{ 
	if (!document.getElementsByTagName) return; 
	var strMovieSize = '';
	var hrefUri;
	var strNewURL = '';
	
	var anchors = getElementsByClass(strClassName, docContext, 'a');
	
	for (var i=0; i<anchors.length; i++)
	{ 
		var anchor = anchors[i]; 
		anchor.target = '';
		if (isIE())
		{
			hrefUri = parseUri(anchor.getAttribute('href'));
			if (strClassName=='media-wmv')
			{
				strNewURL = hrefUri.authority + hrefUri.directoryPath + hrefUri.fileName;
			}
			else if (strClassName=='media-flash')
			{
				strNewURL = hrefUri.directoryPath + hrefUri.fileName;
			}
			strMovieSize = checkVideoSize(hrefUri.anchor);
		}
		else
		{
			hrefUri = parseUri(anchor.baseURI);
			if (strClassName=='media-wmv')
			{
				strNewURL = hrefUri.authority + anchor.pathname;
			}
			else if (strClassName=='media-flash')
			{
				strNewURL = anchor.pathname;
			}
			strMovieSize = checkVideoSize(parseUri(anchor.getAttribute('href')).anchor);
		}
		strNewURL = strNewURL.concat('#' + strMovieSize);
		anchor.setAttribute('target', '');
		anchor.removeAttributeNode(anchor.getAttributeNode('href'));
		var oHref = document.createAttribute('href');
		if (strClassName=='media-wmv')
		{
			oHref.value = 'javascript:openWMVWindow(\''+strNewURL+'\');';
		}
		else if (strClassName=='media-flash')
		{
			oHref.value = 'javascript:openFlashWindow(\''+strNewURL+'\');';
		}
		anchor.setAttributeNode(oHref);
	} 
	
} 

function setVideoLinks()
{
	setVideoAnchors('media-wmv', document.content);
	setVideoAnchors('media-flash', document.content);
}

function checkVideoSize(strHash)
{
		if ((strHash=='high')||(strHash=='low')||(strHash=='high43')||(strHash=='low43'))
		{
			return strHash;	
		}
		else
		{
			return 'low';	
		}
}



function generateAkamaiSerial(url)
{
	var serialno = 0;
	var seed = Hash(url);
	while(seed>1)
	{
		seed = (seed/10)	
	}
//	alert(seed);
	while(serialno <= 0)
	{
		serialno = Math.round(2000*seed);	   
//		alert(serialno);
	}
	return serialno
}

function openWMVWindow(strVideoURL)
{
	var strAnchor = parseUri(strVideoURL).anchor;
	var strWindowName = Math.floor(Math.random()*11);
	var nWidth = getMovieWidth(strAnchor, 'wmv');
	var nHeight = getMovieHeight(strAnchor, 'wmv');
	var strFileHash = Hash(parseUri(strVideoURL).fileName);
	var nSerialNumber = generateAkamaiSerial(strVideoURL);
	if (parseUri(strVideoURL).domain=='www.resmed.com')
	{
		strVideoURL = 'mms://a'+nSerialNumber+'.v32810c.c32810.g.vm.akamaistream.net/7/'+nSerialNumber+'/32810/v'+strFileHash+'/' + strVideoURL;
	}
	else
	{
		strVideoURL = 'http://' + strVideoURL;
	}
//	window.open('/lib/video_assets/wmv_template.html?'+strVideoURL, 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 openFlashWindow(strVideoURL)
{
	var strAnchor = parseUri(strVideoURL).anchor;
	var strWindowName = Math.floor(Math.random()*11);
	var nWidth = getMovieWidth(strAnchor, 'flash');
	var nHeight = getMovieHeight(strAnchor, 'flash');
//	window.open('/lib/video_assets/flash_template.html?'+strVideoURL, 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 getVideoURL()
{
	return parseUri(getVideoURLQuery()).source;
}

function getVideoURLQuery()
{
	return parseUri(window.location.href).query;
}

function getVideoURLAnchor()
{
	return parseUri(window.location.href).anchor;
}


function getMovieWidth(strMovieSize, strMime)
{
	if ((strMovieSize=='high')||(strMovieSize=='high43'))
	{
		return (640).toString();
	}
	else if((strMovieSize=='low')||(strMovieSize=='low43'))
	{
		return (320).toString();
	}
}

function getMovieHeight(strMovieSize, strMime)
{
	var nOffset = 0;
	if (strMime=='flash')
	{
		if (isIE())
		{
			nOffset = 0;
		}
		else
		{
			nOffset = 0;
		}
	}
	else if (strMime=='wmv')
	{
		if (isIE())
		{
			nOffset = 69;
		}
		else
		{
			nOffset = 45;
		}
	}
	if (strMovieSize=='high')
	{
		return (360+nOffset).toString();
	}
	else if(strMovieSize=='high43')
	{
		return (480+nOffset).toString();
	}
	else if(strMovieSize=='low')
	{
		return (180+nOffset).toString();
	}
	else if(strMovieSize=='low43')
	{
		return (240+nOffset).toString();
	}
}
addLoadEvent(setVideoLinks);

//**********************************************************************
// Above code to be removed when video_links.js is included in the templates
//**********************************************************************


//**********************************************************************
// Below code to be removed when faq.js is included in the templates
//**********************************************************************
function toggleFaqItem(oHandle)
{
	if (oHandle.parentNode.parentNode)
	{
		if (isIE())
		{
//			var oTarget = oHandle.parentNode.nextSibling;
			var oTarget = getElementsByClass('faq-a', oHandle.parentNode.parentNode, 'div')[0];
		}
		else
		{
			var oTarget = getElementsByClass('faq-a', oHandle.parentNode.parentNode, 'div')[0];
//			var oTarget = oHandle.parentNode.nextSibling.nextSibling;
		}
		oHandle.className = (oTarget.style.display=='inline')?'mi-plus':'mi-minus';
		oTarget.style.display = (oTarget.style.display=='inline')?'none':'inline';
		oHandle.blur();
		window.focus();
	}
}

function toggleExpandableItem(oHandle)
{
	if (oHandle.parentNode.parentNode)
	{
		var oTarget = getElementsByClass('expandable-section', oHandle.parentNode.parentNode, 'div')[0];
		oHandle.className = (oTarget.style.display=='inline')?'mi-plus':'mi-minus'
		oTarget.style.display = (oTarget.style.display=='inline')?'none':'inline';
		oHandle.blur();
		window.focus();
	}
}
//**********************************************************************
// Above code to be removed when faq.js is included in the templates
//**********************************************************************

//**********************************************************************
// Below code to be removed when image_gallery.js and image_gallery_preload.js is included in the templates
//**********************************************************************
function setGalleryImageFile(imageId)
{
	if (document.getElementById('largesample'))
	{
		document.getElementById('largesample').src='images/'+imageId+'-l.jpg';
	}
	window.focus();
}

function setGalleryLargeImage(imageId, srcEl)
{
	var srcUri = '';
	var srcPath = '';
	if (srcEl.firstChild.getAttribute('src'))
	{
		srcPath = parseUri(srcEl.firstChild.getAttribute('src'));
		if (isIE())
		{
			srcUri = srcPath.directoryPath;
		}
		else
		{
			srcUri = srcPath.authority+srcPath.directoryPath;
		}
		if (document.getElementById('largesample'))
		{
			document.getElementById('largesample').src=srcUri+imageId+'-l.jpg';
		if (srcEl.firstChild.getAttribute('alt'))
		{
			if (document.getElementById('gallerylargeimagecaption'))
			{
				var strCaption = srcEl.firstChild.getAttribute('alt');
				document.getElementById('gallerylargeimagecaption').innerHTML='<h4>'+strCaption+'</h4>';
				if (isIE())
					document.getElementById('largesample').setAttribute('alt', strCaption);
				document.getElementById('largesample').setAttribute('title', strCaption);
			}
		}
		}
	}
	window.focus();
}
//**********************************************************************
// Above code to be removed when image_gallery.js and image_gallery_preload.js is included in the templates
//**********************************************************************

//**********************************************************************
// Below code to be removed when googlemini_search.js is included in the templates
//**********************************************************************
function doSearchKeyPressCheck()
{
	if (window.event.keyCode==13)
	{
		window.event.keyCode =0;			
		doSearch();
	}
}

function doSearch()
{
	var searchForm = document.getElementById('searchsite');
	var searchField = document.getElementById('searchstring');
	var strSearchFieldvalue;
	window.focus();
	if (searchField)
	{
		strSearchFieldvalue = searchField.value;
		strSearchFieldvalue = strSearchFieldvalue.trim();
	}
	if (strSearchFieldvalue.length>0)
	{
		searchForm.submit();
	}
	else
	{
		document.getElementById('searchstring').value = '';
		document.getElementById('searchstring').focus();
	}
}
//**********************************************************************
// Above code to be removed when googlemini_search.js is included in the templates
//**********************************************************************

