	var http_request;
    function makeHttpRequest(formName, callbackFunction) {
		if (LOGGED_IN == false){
			go('http://www.xtube.com/login.php');
			return false;
		}
		 	
        http_request = false;
        var form = document.forms[formName];
        var URL = form.action;
		var formVars = new Array();
		var data = "";
	
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            
            if (http_request.overrideMimeType) 
                http_request.overrideMimeType('text/xml');
     
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) 
            return false;

		// get form data
		for (var i = 0; i < form.elements.length; i++) {
			var formElement = form.elements[i];
			formVars[formElement.name] = formElement.value;
		}
		
		// encode form data
		for (var eleName in formVars)
			data += "&" + eleName + "=" + encodeURIComponent(formVars[eleName]);

        http_request.onreadystatechange = callbackFunction;
        http_request.open('POST', URL, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(data);

    }


	
	var xmlHttp;

	function createXMLHttpRequest() {
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            xmlHttp = new XMLHttpRequest();
            
            if (xmlHttp.overrideMimeType) 
                xmlHttp.overrideMimeType('text/xml');
     
        } else if (window.ActiveXObject) { // IE
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }	
	}

	function refreshLinkingCodeList() {	
		var sponsor_id = document.getElementById("sponsor_id").value;
		
		if(sponsor_id == 0) {
			clearLinkingCodeList();	
		} else {			
			document.getElementById('explain_linking_code').style.display='block';	
			document.getElementById('explain_linking_code').innerHTML = "<h4><font color=green>Fetch Linking Codes ...</font></h4>";
			
		}
		
		var url = "getLinkingCodes.php?"+createQueryString(sponsor_id)+"&ts="+new Date().getTime();
		
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.open('get', url, true);
        xmlHttp.send(null);
		
	}

	function createQueryString (sponsor_id) {
		var queryString = "sponsor_id="+sponsor_id;
		
		return queryString;
		
	}

	function handleStateChange () {
		
		if(xmlHttp.readyState==4) {
			if(xmlHttp.status==200) {
				
				updateLinkingCodeList();
			}
		}
	}

	function updateLinkingCodeList () {
		clearLinkingCodeList();
		
		var results = xmlHttp.responseXML;
		var option = null;
		var linkingCode = null;
		var linking_id = '';
		var linking_code = '';
		var description = '';

  		var explain_linking_code_text = "<h4>As a webmaster, you can put your sponsor's linking code here to earn your referral bonus.This Link code will be displayed as: <u>User suggested link</u>, on the video view page.When a user clicks this link the page will redirect to your sponsor's site.</h4>";
		
		var add_linking_code_text = "<h4>No Linking Code Exist, <a href=http://s1.xtube.com/my_linking_code.php?act=add>Add New One</a></h4>";
		
		var frm_linking_id = document.getElementById("linking_id");
		var linkingCodes = results.getElementsByTagName("linkingCode");
		
		var frm_sponsor_id = document.getElementById("sponsor_id").value;
		
		if(linkingCodes.length>0) {
			option = document.createElement("option");
			option.setAttribute("value","0");
			option.appendChild(document.createTextNode("Select a linking Code"));
			frm_linking_id.appendChild(option);
		}
		
		for(var i=0;i<linkingCodes.length;i++) {
			linkingCode = linkingCodes[i];
			linking_id = linkingCode.getElementsByTagName("linking_id")[0].firstChild.nodeValue;
			linking_code = linkingCode.getElementsByTagName("linking_code")[0].firstChild.nodeValue;
			linking_code = decodeURI(linking_code);
			
			description = linkingCode.getElementsByTagName("description")[0].firstChild.nodeValue;
			//description = decodeURIComponent(description);
			
			option = document.createElement("option");
			option.setAttribute("value",linking_id);
			option.appendChild(document.createTextNode(description));
			frm_linking_id.appendChild(option);
			
		}
		
		if(i>0) {
			frm_linking_id.style.display='block';	
			document.getElementById('add_linking_code').style.display='block';
			document.getElementById('explain_linking_code').style.display='none';	
			
			if(document.getElementById('setLinkingButton')) {
				document.getElementById('setLinkingButton').disabled=false;	
			}
			
		} else {
			
			if(frm_sponsor_id==0) {
				frm_linking_id.style.display='none';	
				document.getElementById('add_linking_code').style.display='none';	
				document.getElementById('explain_linking_code').style.display='block';
				document.getElementById('explain_linking_code').innerHTML = explain_linking_code_text;
					
			} else {
				frm_linking_id.style.display='none';
				document.getElementById('add_linking_code').style.display='none';	
				document.getElementById('explain_linking_code').style.display='block';	
				document.getElementById('explain_linking_code').innerHTML = add_linking_code_text;
			}
			
		}
	}

	function clearLinkingCodeList() {
		var frm_linking_id = document.getElementById("linking_id");
		while (frm_linking_id.childNodes.length>0) {
			frm_linking_id.removeChild(frm_linking_id.childNodes[0]);	
			
		}
			
	}

	function setLinkingCode(id,type) {	
		var sponsor_id = document.getElementById("sponsor_id").value;
		var linking_id = document.getElementById("linking_id").value;
		
		if(sponsor_id == 0 || linking_id == 0) {
			alert("Please select a sponsor and Linking Code !");
			return false;
			
		} else {			
			document.getElementById("setLinkingButton").disabled = true;	
			document.getElementById("setLinkingButton").value = "Processing ...";			
		}
		
		
		var url = "setLinkingCode.php?type="+type+"&id="+id+"&sponsor_id="+sponsor_id+"&linking_id="+linking_id+"&ts="+new Date().getTime();
		
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handlesetLinkingCode;
        xmlHttp.open('get', url, true);
        xmlHttp.send(null);
		
	}

	function handlesetLinkingCode () {
		
		if(xmlHttp.readyState==4) {
			if(xmlHttp.status==200) {
				var res_code = xmlHttp.responseXML.getElementsByTagName('res_code')[0].firstChild.data;
				
				if(res_code=='SUCCESS') {
					document.getElementById("setLinkingButton").disabled = true;	
					document.getElementById("setLinkingButton").value = "Processing Done !";			
				} else {
					alert("Error Happen ! Try again.");
					document.getElementById("setLinkingButton").disabled = false;							
				}
				
			} else {
				alert("Error Happen ! Try again.");
				document.getElementById("setLinkingButton").disabled = false;					
			}
		}
	}




	function postComment(form_id) {		
		if (LOGGED_IN == false){
			go('http://www.xtube.com/login.php?url='+escape(CURRENT_URL));
			return false;
		}
		
		var comment_form = document.forms[form_id];
        var comment = comment_form.comment;
        var comment_button = comment_form.comment_button;
       
        if (comment.value.length == 0 || comment.value == null) {
            alert("You must enter a comment!");
            comment.focus();
            return false;
        }

        if (comment.value.length > 500) {
            alert("Your comment must be shorter than 500 characters!");
            comment.focus();
            return false;
        }
		
		makeHttpRequest(comment_form.name,commentResXML);
		
        return true;
	}
	
	
	