function createRequest(){
	var req;
	if(typeof(XMLHttpRequest) != 'undefined') req = new XMLHttpRequest();
	else req = false;

	if(!req) {
		try {
			req = new ActiveXObject("MSXML2.XMLHTTP");
		} catch (olderMS) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(oops) {
				alert('not able to create');
			}
		}
	}
	return(req)
}

function tryInnerHTML() {
	var x=document.getElementById('eg1');
	x.innerHTML = 'I guess it works!';
	x.style.color='red'	
}

function openWP() {
	window.open("samples/word_processor.html",null,
    "height=300,width=300,status=no,toolbar=no,menubar=no,location=no");
}

function openDrag() {
	window.open("samples/drag_and_drop.html",null,
    "height=300,width=300,status=no,toolbar=no,menubar=no,location=no");
}

function getKey(e) {

	if(!e) e = window.event;
	if     (e.keyCode) asciiCode = e.keyCode;
	else if(e.which)   asciiCode = e.which;

	theCharacter = String.fromCharCode(asciiCode);
	if(asciiCode < 32) return(false); // do not output weird characters
	document.getElementById('eg2').innerHTML += theCharacter;
	if(asciiCode == 32) return(false); // output space, but do not page down
}

function checkBackspaceAndEnter(e) {

	if(!e) e = window.event;
	if     (e.keyCode) asciiCode = e.keyCode;
	else if(e.which)   asciiCode = e.which;

	objWork = document.getElementById('eg2');
	strWork = objWork.innerHTML;

	// if backspace, remove last character
	if(asciiCode == 8) objWork.innerHTML = strWork.substring(0,strWork.length - 1);

	// if linefeed or carriage return, append break tag
	if(asciiCode == 10 || asciiCode==13) objWork.innerHTML += '<br />';

	// cancel non-text events so backspace key does not send browser back to previous page
	if(asciiCode < 32) return(false);
}

function moveIt(e) {
   if(!e) e = window.event;
   if(dragging) {
	   imgAlien.style.left = e.clientX - offsetAtStartOfDraggingX;
	   imgAlien.style.top  = e.clientY - offsetAtStartOfDraggingY;
	}
   return(false);
}

function dragStart(e) {
   if(!e) e = window.event;
   dragging = true;
   // find distance between mouse and corner of alien on initial click
   // by subtracting exact position of face (remove 'px' from position)
   offsetAtStartOfDraggingX = e.clientX - parseInt(imgAlien.style.left);
   offsetAtStartOfDraggingY = e.clientY - parseInt(imgAlien.style.top);
   return(false);
}

function stopDrag() {
	dragging = false;
}

function quote() {
	req = createRequest();
	ticker = document.getElementById('idTicker').value;
	var url = 'http://download.finance.yahoo.com/d/quotes.csv?s=' + ticker + '&f=sl1d1t1c1ohgv&e=.csv';
	req.open('GET','samples/proxy.php?proxy_url=' + escape(url),true);
	req.onreadystatechange = showPrice;
	req.send(null);
}


function showPrice() {
	if(req.readyState != 4) return;
	data = req.responseText.split(',');
	document.getElementById('idPrice').innerHTML = data[1];
}

function getCity() {
	req2 = createRequest();
	var city = document.getElementById('idCity').value;
	var url = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=ALLwH.XV34FqHD5DHTFx37.dXOxWOR6IIK7LAo0l41JRSq0MZjA_SqyORq1An58hAiwj&city=' + escape(city);
	req2.open('GET','samples/proxy.php?proxy_url=' + escape(url),true);
	req2.onreadystatechange = showCity;
	req2.send(null);
}

function showCity() {
	if(req2.readyState != 4) return;
	alert(req2.responseText);
}

function initDrag() {
	dragging = false;
	imgAlien = document.getElementById('eg3');
	imgAlien.onmousedown  = dragStart;
	document.onmousemove  = moveIt;
	document.onmouseup    = stopDrag;
}

function initWP() {
	document.onkeypress   = getKey;
	document.onkeydown    = checkBackspaceAndEnter;
}

function addTable() {

    elmResultsArea = document.getElementById('idNewTable');

    elmTABLE = document.createElement('table');
    elmTBODY = document.createElement('tbody');
    elmTR    = document.createElement('tr');
    elmTD1   = document.createElement('td');
    elmTD2   = document.createElement('td');
    elmTD3   = document.createElement('td');
    nodText1 = document.createTextNode('just');
    nodText2 = document.createTextNode('like');
    nodText3 = document.createTextNode('this!');

    elmTD1.appendChild(nodText1);
    elmTD2.appendChild(nodText2);
    elmTD3.appendChild(nodText3);
    elmTR.appendChild(elmTD1);
    elmTR.appendChild(elmTD2);
    elmTR.appendChild(elmTD3);
    elmTBODY.appendChild(elmTR);
    elmTABLE.appendChild(elmTBODY);

    elmTABLE.setAttribute("border", "1");

    elmResultsArea.appendChild(elmTABLE);

}

function showSOAP() {
    var searchTerm = document.getElementById('idSOAP').value;
    key    = '2dO+NVZQFHKRXYkB69MiP8Sh4YjZX16Q';

    alert(
    '<?xml version="1.0" encoding="UTF-8"?>'+
    '\n\n'+
    '<SOAP-ENV:Envelope'+
    ' xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"'+
    ' xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"'+
    ' xmlns:xsd="http://www.w3.org/1999/XMLSchema">'+
    '<SOAP-ENV:Body><ns1:doGoogleSearch'+
    ' xmlns:ns1="urn:GoogleSearch"'+
    ' SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'+
    '<key xsi:type="xsd:string">' + key + '</key> <q'+
    ' xsi:type="xsd:string">' + searchTerm + '</q> <start'+
    ' xsi:type="xsd:int">0</start> <maxResults'+
    ' xsi:type="xsd:int">10</maxResults> <filter'+
    ' xsi:type="xsd:boolean">true</filter> <restrict'+
    ' xsi:type="xsd:string"></restrict> <safeSearch'+
    ' xsi:type="xsd:boolean">false</safeSearch> <lr'+
    ' xsi:type="xsd:string"></lr> <ie'+
    ' xsi:type="xsd:string">latin1</ie> <oe'+
    ' xsi:type="xsd:string">latin1</oe>'+
    '</ns1:doGoogleSearch>'+
    '</SOAP-ENV:Body>'+
    '</SOAP-ENV:Envelope>'
    );
}


function showJSON() {
	alert(globalResults.toJSONString());	
}

function askQuestion() {
    txtQuestion = document.getElementById('idQuestion').value;
    txtYahooURL = 'http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid=ALLwH.XV34FqHD5DHTFx37.dXOxWOR6IIK7LAo0l41JRSq0MZjA_SqyORq1An58hAiwj&output=json&callback=yahooShowIt' +
                  '&query=' + escape(txtQuestion);

    elmNewArea    = document.getElementById('idScriptArea');

    elmNodeScript = document.createElement('script');
    elmNodeScript.setAttribute('src',txtYahooURL);
    elmNewArea.appendChild(elmNodeScript);

}
function yahooShowIt(yahooData) {
	elmAnswer = document.getElementById('idAnswer');
	elmAnswer.innerHTML = '<br />';
	x = 0;
    //for(x=0; x<yahooData.all.questions.length; x++) {
        elmAnswer.innerHTML += yahooData.all.questions[x].ChosenAnswer + '<hr>';
	//	if(x>1) break;
    //}
}

//--------------------------------------
