/************************************************************
*    _____  _    _ __  __  _____
*   |  __ \| |  | |  \/  |/ ____|
*   | |  | | |__| | \  / | |
*   | |  | |  __  | |\/| | |
*   | |__| | |  | | |  | | |____
*   |_____/|_|  |_|_|  |_|\_____|
* 
*   Author: Ayaz A. Asif // DHMC Information Systems
*   Date: 5/12/2008
*   Language: Javascript
*   Purpose: Use HttpRequest to call CFM page which 
*			 will build out the code to insert a 
*			 video or preview frame image
*
*	Revision History:
*			 5/13/08 (aaa/dlm) - Bug fixes
*			 2/17/08 (aaa) - Added referrer data to pass
*
************************************************************/

function urlencode( str ) {
                             
    var histogram = {}, 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["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function ajaxVid(vidID,respType) {
	
	//instantiate XMLHTTP object
	var xmlHttp;
	
	// Firefox, Opera 8.0+, Safari
	try { xmlHttp=new XMLHttpRequest(); }
	catch(e) {
		// Internet Explorer
		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    	catch(e) {
			try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	var vidURL = "/apps/vidPlayback/vidPlaybackAjax.cfm?ID=" + vidID + "&Type=" + respType + "&locHref=" + urlencode(window.location.href);	
	
	//alert (vidURL);
	xmlHttp.open("GET",vidURL,true); //Call the script that does the work -- true sets it to be ansynchronous (no wait for process)
	xmlHttp.setRequestHeader("Cache-Control","no-cache");
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var returnCode = xmlHttp.responseText;
			var elementID = respType + '_' + vidID;
			//alert(returnCode);
			//alert(elementID);
			document.getElementById(elementID).innerHTML=returnCode;
			//document.writeln(returnCode);
		}
	}
	
	xmlHttp.send(null);
}
