﻿function virsonaGreeting(communityid,communityname,proxychoice) {
    xmlhttp = null;
    if (typeof XMLHttpRequest != "undefined") {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
            xmlhttp.overrideMimeType("text/xml");
    }
    else if (typeof window.ActiveXObject != "undefined"){
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch(e){
            try {
                xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
            }
            catch(e){
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    xmlhttp = null;
                }
            }
        }
    }

    // Set the callback function
    xmlhttp.onreadystatechange = function() {

        switch(xmlhttp.readyState) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
	    var response = xmlhttp.responseText;
	    var doc;
	    if (window.ActiveXObject){ //IE
		doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(response);
	    } else 	{
		var parser = new DOMParser();
		doc = parser.parseFromString(response,"text/xml");
	    }

    	    var sessionid = doc.getElementsByTagName('session')[0].attributes.getNamedItem('id').value;
    	    document.getElementById('vsession').value = sessionid;
            displayvResponse(communityname + ': ' + doc.getElementsByTagName('response')[0].childNodes[1].nodeValue); 
	    break;
        default:
            break;
        }
    }


    // Create the path for the web services request (without the domain name)
    if (document.getElementById('vsession').value == '' || document.getElementById('vsession').value == undefined) {
	    var path = 'service/enter.ashx?cvid=' + communityid;
    } else {
	    var path = 'service/enter.ashx?cvid=' + communityid + '&session=' + document.getElementById('vsession').value;
    };

    // Create the path to the appropriate proxy (without the domain name)
    if (proxychoice == 'asp') {
        var url = 'virsonaproxy.aspx?vidget=' + urlencode(path);
    } else if (proxychoice == 'asp') {
        var url = 'virsonaproxy.aspx?vidget=' + urlencode(path); 
    }

    // Make the actual request
    xmlhttp.open('GET',url,true);
    xmlhttp.send(null);
}

function vSend(communityid,communityname,proxychoice) {
    // Process Input
    vinputtext = document.getElementById('vchat').value;

    document.getElementById('vchatbox').innerHTML = document.getElementById('vchatbox').innerHTML + '<br /><br />You: ' + vinputtext;

    var size = document.getElementById('vchatbox').scrollHeight;
    document.getElementById('vchatbox').scrollTop = 100000;
    document.getElementById('vchat').value = '';
    var myfocus = document.getElementById("vchat");
    myfocus.focus();
    myfocus.select();
    typingMsg();

    xmlhttp = null;
    if (typeof XMLHttpRequest != "undefined") {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
            xmlhttp.overrideMimeType("text/xml");
    }
    else if (typeof window.ActiveXObject != "undefined"){
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch(e){
            try {
                xmlhttp = new ActiveXObject("MSXML2.XMLHTTP");
            }
            catch(e){
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    xmlhttp = null;
                }
            }
        }
    }

    // Set the callback function
    xmlhttp.onreadystatechange = function() {

        switch(xmlhttp.readyState) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
	    var response = xmlhttp.responseText;
	    var doc;
	    if (window.ActiveXObject){ //IE
		doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(response);
	    } else 	{
		var parser = new DOMParser();
		doc = parser.parseFromString(response,"text/xml");
	    }

    	    var sessionid = doc.getElementsByTagName('session')[0].attributes.getNamedItem('id').value;
    	    document.getElementById('vsession').value = sessionid;
            displayvResponse(document.getElementById('vchatbox').innerHTML + '<br />' + communityname + ': ' + doc.getElementsByTagName('text')[0].childNodes[0].nodeValue);
            
	    var tdat = doc.getElementsByTagName('text')[0].attributes.getNamedItem('tdat');
            var vlog = doc.getElementsByTagName('text')[0].attributes.getNamedItem('vlog'); 
            if (tdat != null && tdat.value != "" && tdat.value != undefined) {
		teachDataResponse(tdat.value);
		//clientCustomCode("tdat",tdat.value);
            } else if (vlog != null && vlog.value != "" && vlog.value != undefined) {
                vlogResponse(vlog.value);
		//clientCustomCode("vlog",vlog.value);
            }
            break;
        default:
            break;
        }
    }


    // Create the path for the web services request (without the domain name)
    if (document.getElementById('vsession').value == '' || document.getElementById('vsession').value == undefined) {
	    var path = 'service/chat.ashx?cvid=' + communityid + '&input=' + vinputtext;
    } else {
	    var path = 'service/chat.ashx?cvid=' + communityid + '&session=' + document.getElementById('vsession').value + '&input=' + vinputtext;
    };

    // Create the path to the appropriate proxy (without the domain name)
    if (proxychoice == 'php') {
        var url = 'vidgetproxy.php?vidget=' + urlencode(path);
    } else if (proxychoice == 'asp') {
        var url = 'virsonaproxy.aspx?vidget=' + urlencode(path); 
    }

    // Make the actual request
    xmlhttp.open('GET',url,true);
    xmlhttp.send(null);
}

function displayvResponse(vresponse) {
	document.getElementById('vchatbox').innerHTML = vresponse;
	document.getElementById('indicator').style.display = 'none';
	var size = document.getElementById('vchatbox').scrollHeight;
	document.getElementById('vchatbox').scrollTop = 100000;
}

function loadScript(scriptURL) {
    var newScript = document.createElement("script");
    newScript.src = scriptURL;
    document.body.appendChild(newScript);
}

function urlencode(s) {
    s = encodeURIComponent(s);
    return s.replace(/~/g,'%7E');
}

function typingMsg() {
    document.getElementById('indicator').style.display = '';
}

function onEnter( evt, frm ) {
    var keyCode = null;

    if( evt.which ) {
        keyCode = evt.which;
    } else if( evt.keyCode ) {
    keyCode = evt.keyCode;
    }
    if( 13 == keyCode ) {
        frm.vButton.click();
        return false;
    }
    return true;
}

function loadXMLDoc(dname) {
  var xmlDoc;
  if (window.XMLHttpRequest) {
    xmlDoc=new window.XMLHttpRequest();
    xmlDoc.open("GET",dname,false);
    xmlDoc.send("");
    return xmlDoc.responseXML;
  }
  // IE 5 and IE 6
  else if (window.ActiveXObject) {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async=false;
    xmlDoc.load(dname);
    return xmlDoc;
  }
  return null;
}

// This function is called whenever a teach data response is returned by the Virsona
// The argument, tdat, is an integer which uniquely identifies the teach data entry that was returned
function teachDataResponse(tdat) {
	xmlDoc=loadXMLDoc("/scripts/actioncodes.xml");
	actioncodes=xmlDoc.getElementsByTagName("actioncode");
	for (i=0;i<actioncodes.length;i++) {
		if (actioncodes[i].getAttribute("type") == "tdat" && actioncodes[i].getAttribute("value") == tdat) {
			trigger=actioncodes[i].parentNode;
			window.open(trigger.getAttribute("address"),"_blank","height=600,width=800");
		}
	}
}

// This function is called whenever a vlog response is returned by the Virsona
// The argument, vlog, is an integer which uniquely identifies the vlog entry that was returned
function vlogResponse(vlog) {
	xmlDoc=loadXMLDoc("/scripts/actioncodes.xml");
	actioncodes=xmlDoc.getElementsByTagName("actioncode");
	for (i=0;i<actioncodes.length;i++) {
		if (actioncodes[i].getAttribute("type") == "vlog" && actioncodes[i].getAttribute("value") == vlog) {
			trigger=actioncodes[i].parentNode;
			window.open(trigger.getAttribute("address"),"_blank","height=600,width=800");
		}
	}
}

function checkKeyCode(ev)
{
    	
if(window.event) // IE
  {
  keynum = ev.keyCode;
  }
else if(ev.which) // Netscape/Firefox/Opera
  {
  keynum = ev.which;
  }
    if (keynum == 13)
    {
        vSend('2577','The Professor','asp');
        return false;
    }
    else 
    {
        return String.fromCharCode(keynum);
    }
}
