function GetXMLHttpRequest()
{
	// Set up the lightweigtht AJAX
	// http://www.jibbering.com/2002/4/httprequest.html
	var xmlhttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmlhttp = false;
	}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) 
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) 
	{
		try 
		{
			xmlhttp = window.createRequest();
		} 
		catch (e) 
		{
			xmlhttp = false;
		}
	}
	
	return xmlhttp;
}

function readCookie(NameOfCookie)
	{
		if (document.cookie.length > 0)
			{
				begin = document.cookie.indexOf(NameOfCookie+"=");
				if (begin != -1)
					{
						begin += NameOfCookie.length+1;
						end = document.cookie.indexOf(";", begin);
						if (end == -1) end = document.cookie.length;
						return unescape(document.cookie.substring(begin, end));
					}
			}
		return null;
	}

function InitializeUpload()
	{
		document.getElementById("uploadprogress").innerHTML = '<b>Starting the upload process...</b><br>';
		return true;
	}

function updateimage (customerid, widgetid, formatpix, assetid, thumbnailid, selectedkey, token)
	{		
		// updated ================================
		var customerid = readCookie('c'); 
		var user = readCookie('u'); 
		var newimage = '<img src="'+'http://cdn.liveclicker.net/thumbnails/'+customerid+'/'+assetid+'_1_Flv_'+formatpix+'_thumb_'+thumbnailid+'.jpg'+'" height="48" width="64">';
		var uploadstatusURL = 'ajax/updatethumbnail.php?customerid='+customerid+'&token='+token+'&w='+widgetid+'&t='+thumbnailid;
		var xmlhttp = GetXMLHttpRequest();
			
		xmlhttp.open("GET", uploadstatusURL, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState==4)
			{
				if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
				{
					document.getElementById('selected'+selectedkey).innerHTML=newimage;
					document.getElementById('title'+selectedkey).innerHTML=document.getElementById('title'+selectedkey).innerHTML+'<br><br><b>UPDATED</b>';
				}
			}
		return true;
	}

function updatetitle (widgetid, selectedkey, token)
	{		
		// updated ================================
		var customerid = readCookie('c'); 
		var title = document.getElementById('myform'+selectedkey).myname.value;		
		
		var uploadstatusURL = 'ajax/updatetitle.php?c='+customerid+'&w='+widgetid+'&t='+title+'&token='+token;
		// alert(uploadstatusURL); 
		var xmlhttp = GetXMLHttpRequest();
			
		xmlhttp.open("GET", uploadstatusURL, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState==4)
			{
				if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
				{
					if (xmlhttp.responseText.indexOf('error') < 0)
						{
							document.getElementById('title'+selectedkey).innerHTML=title+' <b>Updated</b>';
						}
				}
			}
		return true;
	}

function updatemeta (widgetid, selectedkey, token)
	{		
		// updated ================================
		var customerid = readCookie('c'); 
		var meta = document.getElementById('myform'+selectedkey).myname.value;		
		
		var uploadstatusURL = 'ajax/updatemeta.php?c='+customerid+'&w='+widgetid+'&m='+meta+'&token='+token;
		// alert(uploadstatusURL); 
		var xmlhttp = GetXMLHttpRequest();
			
		xmlhttp.open("GET", uploadstatusURL, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState==4)
			{
				if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
				{
					if (xmlhttp.responseText.indexOf('error') < 0)
						{
							var currenttitle = document.getElementById('title'+selectedkey).innerHTML; 
							document.getElementById('title'+selectedkey).innerHTML=currenttitle+'<br><b>Updated</b>';
						}
				}
			}
		return true;
	}
	
function updatetextlink (widgetid, selectedkey,link_id)
	{		
		// updated ================================
		var customerid = readCookie('c'); 
		var title = document.getElementById('myform'+selectedkey).myname.value;		
		
		var mytime= '&ms='+new Date().getTime();
		
		var uploadstatusURL = 'ajax/updatetextlink.php?c='+customerid+'&w='+widgetid+'&lid='+link_id+'&t='+title+mytime;
		// alert(uploadstatusURL); 
		var xmlhttp = GetXMLHttpRequest();
			
		xmlhttp.open("GET", uploadstatusURL, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState==4)
			{
				if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
				{
					if (xmlhttp.responseText.indexOf('error') < 0)
						{
							document.getElementById('title'+selectedkey).innerHTML=title+'<br><b>Updated</b>';
						}
				}
			}
		return true;
	}
	
function updateassettitle (assetid, selectedkey, token)
	{		
		
		// updated ================================
		var customerid = readCookie('c'); 
		var title = document.getElementById('myform'+selectedkey).myname.value;		
		
		var uploadstatusURL = 'ajax/updateassettitle.php?c='+customerid+'&a='+assetid+'&t='+title+'&token='+token;
		// alert(uploadstatusURL); 
		var xmlhttp = GetXMLHttpRequest();
			
		xmlhttp.open("GET", uploadstatusURL, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState==4)
			{
				if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
				{
					if (xmlhttp.responseText.indexOf('error') < 0)
						{
							document.getElementById('title'+selectedkey).innerHTML=title+' <b>Updated</b>';
						}
				}
			}
		return true;
	}

function previewwidget_bak(customerid,widgetid)
	{
		// obsolete
		var actualwidgetid;
		var media_type;

		if (widgetid < 0)
			{
				actualwidgetid = - widgetid;
				media_type = 1; 
			}
			else
			{
				actualwidgetid = widgetid;
				media_type = 0; 
			}
		var getembedURL = 'ajax/previewwidget.php?w='+actualwidgetid+'&media_type='+media_type+'&aid='+customerid;
		// alert(uploadstatusURL); 
		var xmlhttp = GetXMLHttpRequest();
			
		xmlhttp.open("GET", getembedURL, false);
		xmlhttp.send(null);
		if (xmlhttp.readyState==4)
			{
				if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
				{
					if (xmlhttp.responseText.indexOf('error') < 0)
						{
							document.getElementById('video_preview').innerHTML=xmlhttp.responseText;
							document.getElementById('preview_link_label').innerHTML='Click to hide the widget';
							document.getElementById('video_preview').style.visibility='visible';
							document.getElementById('video_preview').style.height='300px';
						}
				}
			}
		return true;	
	}

function previewwidget(customerid,widgetid)
	{
		// obsolete
	}
	
function togglepreview(customerid,widgetid)
	{
		if (document.getElementById('video_preview').innerHTML == '')
			{
				previewwidget(customerid,widgetid);
			}
			else
			{	
				if (document.getElementById('preview_link_label').innerHTML == 'Click to view')
					{
						document.getElementById('preview_link_label').innerHTML='Click to hide';
						document.getElementById('video_preview').style.visibility='visible';
						document.getElementById('video_preview').style.height='300px';
					}
					else
					{
						document.getElementById('preview_link_label').innerHTML='Click to view';
						document.getElementById('video_preview').style.visibility='hidden';
						document.getElementById('video_preview').style.height='0px';
					}
			}
		return true;	
	}

function copyvalue(spot,new_value)
	{
		document.getElementById('ac_me_assignment'+spot).value=new_value;
		document.getElementById('ac_me_assignment'+spot).style.color='red';
		document.getElementById('ac_me_assignment'+spot).style.fontWeight='bold';
		return true;
	}

function createXMLHttpRequest()
	{
	   try { return new XMLHttpRequest(); } catch(e) {}
	   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	   alert("XMLHttpRequest not supported");
	   return null;
 	}

function GetFileList ()
	{	
		
		// Check first if the script is processing another file
		// Often times there is a timeout on large files, and the cookie process and stop aren't reset to continue the script
		
		var customerid = readCookie('c'); 
		var cookieprocess = readCookie('process'); 
		var cookiestop = readCookie('stop'); 
		var cookiehold = readCookie('holdnextquery'); 
		var mytime= '&ms='+new Date().getTime();
		
		if (cookieprocess == 1)
			{
				//alert('in the loop');
				var activeurl = 'ajax/checkthread.php?customerid='+customerid+mytime;
				var xmlhttp = createXMLHttpRequest();
				//alert(activeurl);
				xmlhttp.open("GET", activeurl, true);
				xmlhttp.send(null);
				xmlhttp.onreadystatechange = function ()
					{
						if (xmlhttp.readyState != 4)
							{
								return;
							}
							else
							{
								if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
									{
										 if (xmlhttp.responseText.indexOf('and more to process') > 0)
											{
												document.cookie="holdnextquery=0"; // restart processing
												document.cookie="stop=0"; // unstop everything
											}
											else
											{
												if (xmlhttp.responseText.indexOf('no more processing') > 0)
													{
														document.cookie="holdnextquery=1"; // stop further processing
														document.cookie="stop=1"; // stops everything
													}
												
											}
									}
							}
					}
			}
		ProcessFileList();
		return true; 
	}

function ProcessFileList ()
	{		
		var customerid = readCookie('c'); 
		var cookieprocess = readCookie('process'); 
		var cookiestop = readCookie('stop'); 
		var cookiehold = readCookie('holdnextquery'); 
		var mytime= '&ms='+new Date().getTime();
		
		if ((cookieprocess == 1) && (cookiehold !=1) && (cookiestop !=1))
			{
				document.getElementById("loading").style.visibility = 'visible';
				var user = readCookie('u'); 
				var password = readCookie('p'); 
				var uploadstatusURL = 'ajax/checkuploadstatus.php?customerid='+customerid+'&u='+user+'&p='+password+mytime;
				var xmlhttp = createXMLHttpRequest();
				// Added 6/1
				document.cookie="holdnextquery=1"; // temporarily stop the process to avoid repeat queries
				xmlhttp.open("GET", uploadstatusURL, true);
				xmlhttp.send(null);
				xmlhttp.onreadystatechange = function ()
					{
						if (xmlhttp.readyState != 4)
							{
								return;
							}
							else
							{
								if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
									{
										document.getElementById("uploadprogress").innerHTML = document.getElementById("uploadprogress").innerHTML+xmlhttp.responseText;
										if (xmlhttp.responseText.indexOf('process complete.') > 0)
											{
												// we are done!
												//alert('Done with video, And ALL COMPLETE');
												document.cookie="stop=1"; // stop the process
												document.getElementById("loading").style.visibility = 'hidden';
												
											}
											else
											{
												// GetFileList (); // recursive commented out 7/21
												// We're not dependent on this anymore
												//alert('Done with video, Continuing...');
												document.cookie="stop=0"; // unstop the process
												document.cookie="holdnextquery=0"; // do not hold the next query anymore
											}
										return true;
									}
									
							}
					
					}
				xmlhttp.send(null);				
			}
			else
			{
				// alert('Done with all the work');
			}

	}

function popupwindow(url)
	{
		newwindow=window.open(url,'name','height=320,width=360');
		if (window.focus) {newwindow.focus()}
		return false;
	}

function mediumpopupwindow(url)
	{
		newwindow=window.open(url,'name','height=480,width=640');
		if (window.focus) {newwindow.focus()}
		return false;
	}
	
function launchtutorial(url)
	{
		newwindow=window.open(url,'name','height=520,width=660');
		if (window.focus) {newwindow.focus()}
		return false;
	}
	
function getcontextualhelp (words)
	{		
		if (document.getElementById('related-posts'))
			{
				var mytime= '&ms='+new Date().getTime();
				var xmlhttp = GetXMLHttpRequest();
			
				var embedurl = '../wp/wp-content/custom-api/related-posts.php?w='+encodeURI(words)+mytime;
				//alert(embedurl);
				xmlhttp.open("GET", embedurl, false);
				xmlhttp.send(null);
				if (xmlhttp.readyState==4)
					{
						if ((xmlhttp.status==200) && (xmlhttp.responseText != null))
						{
							if (xmlhttp.responseText.indexOf('error') < 0)
								{
									document.getElementById('related-posts').innerHTML=xmlhttp.responseText;
								}
						}
					}
			}
		
		return true;
		
	}


function verify(msgtype)
	{
		msg_delete_site = "PLEASE READ!! Deleting a site will erase all records associated with it. This operation cannot be undone. Are you sure you want to continue?";
		msg_connection_exchange = "ARE YOU SURE? This operation cannot be undone. Are you sure you want to continue? (Recommended: Click Cancel)";
        msg_generic = "This operation cannot be undone. Are you sure you want to continue?";
        if (msgtype=='deletesite')
            {
                return confirm(msg_delete_site);
            }
            else if (msgtype=='exchangedeleteconnection')
			{
					return confirm(msg_connection_exchange);
			}
			else
            {
                return confirm(msg_generic);
            }
        }

function select_all()
	{
		var text_val=eval("document.form1.type");
		text_val.focus();
		text_val.select();
	}
	
function realtime_launch (widget_id)
	{
			// customer id defined above as aid
			window.open("http://vms.liveclicker.com/vms/realtime.php?account_id="+aid+"&key="+account_key+"&widget_id="+widget_id,"Window1","menubar=no,width=316,height=800,toolbar=no, location=no, titlebar=no, scrollbars=yes");
	}
	
