// start ajaxrequest.js


// Following is a javascript function that makes a httprequest - AJAX. This is the AJAX bit and all that is needed in that manner.
// Only in this one we won't be using XML in our response, we will accept and handle
// pure text and html and display this response directly to the user within the
// desired <div id> tags. It can even be used to include pure html files as a substitute
// solution to the "old" frames method where as no php or other scripting language is nessesary on the server.
// but use it with care - it is not a search engine supported method and indexing will fail. Workaround for this is not included here

function MyAjaxRequest(target_div,file,check_div)
{
var MyHttpRequest = false;
var MyHttpLoading = '<br><br><div class="center"><img src="/images/loading.gif" border="0" alt="Loading" /></div>';
var ErrorMSG = 'Sorry - You must update your browser and have javascript enabled.';

if(check_div)
{
var check_value = escape(document.getElementById(check_div).value);

}
else
{
var check_value = '';
}



if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
{
try
{
MyHttpRequest = new XMLHttpRequest();
}
catch(e)
{
MyHttpRequest = false;
}
}
else if(window.ActiveXObject) // client use Internet Explorer
{
try
{
MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
MyHttpRequest = false;
}
}
}
else
{
MyHttpRequest = false;
}



if(MyHttpRequest) // browser supports httprequest
{
var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching

var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file
if(file_array[1] == 'php') // no query string, just calling a php file
{
  var query_string = '?rand=' + random;
}
else if(file_array[1] == 'htm' || file_array[1] == 'html') // calling a htm or html file
{
  var query_string = '';
}
else // we have presumable a php file with a query string attached
{
  var query_string = check_value + '&rand=' + random;
}


MyHttpRequest.open("get", url_encode(file + query_string), true); // <-- run the httprequest using GET


// handle the httprequest
MyHttpRequest.onreadystatechange = function ()
{
if(MyHttpRequest.readyState == 4) // done and responded
{

document.getElementById(check_div).style.border='2px solid #000000';
document.getElementById(target_div).innerHTML = ''; // display result
document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result

if('check_div' != 'information'){

if(document.getElementById('status0').innerHTML == 'Bad' || document.getElementById('status0').innerHTML == '')
{
document.getElementById('username').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('username').style.border='2px solid #00FF00';
}

if(document.getElementById('status1').innerHTML == 'Bad' || document.getElementById('status1').innerHTML == '')
{
document.getElementById('email').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('email').style.border='2px solid #00FF00';
}

if(document.getElementById('status2').innerHTML == 'Bad' || document.getElementById('status2').innerHTML == '')
{
document.getElementById('password').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('password').style.border='2px solid #00FF00';
}

if(document.getElementById('status3').innerHTML == 'Bad' || document.getElementById('status3').innerHTML == '')
{
document.getElementById('displayname').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('displayname').style.border='2px solid #00FF00';
}

if(document.getElementById('status4').innerHTML == 'Bad' || document.getElementById('status4').innerHTML == '')
{
document.getElementById('firstname').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('firstname').style.border='2px solid #00FF00';
}

if(document.getElementById('status5').innerHTML == 'Bad' || document.getElementById('status5').innerHTML == '')
{
document.getElementById('lastname').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('lastname').style.border='2px solid #00FF00';
}

if(document.getElementById('status6').innerHTML == 'Bad' || document.getElementById('status6').innerHTML == '')
{
document.getElementById('gender').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('gender').style.border='2px solid #00FF00';
}

if(document.getElementById('status7').innerHTML == 'Bad' || document.getElementById('status7').innerHTML == '')
{
document.getElementById('country').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('country').style.border='2px solid #00FF00';
}

if(document.getElementById('status8').innerHTML == 'Bad' || document.getElementById('status8').innerHTML == '')
{
document.getElementById('city').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('city').style.border='2px solid #00FF00';
}

if(document.getElementById('status9').innerHTML == 'Bad' || document.getElementById('status9').innerHTML == '')
{
document.getElementById('state').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('state').style.border='2px solid #00FF00';
}

if(document.getElementById('status10').innerHTML == 'Bad' || document.getElementById('status10').innerHTML == '')
{
document.getElementById('month').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('month').style.border='2px solid #00FF00';
}

if(document.getElementById('status11').innerHTML == 'Bad' || document.getElementById('status11').innerHTML == '')
{
document.getElementById('day').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('day').style.border='2px solid #00FF00';
}

if(document.getElementById('status12').innerHTML == 'Bad' || document.getElementById('status12').innerHTML == '')
{
document.getElementById('year').style.border='2px solid #FF0000';
document.getElementById('submit').style.border='2px solid #FF0000';
document.getElementById('submit').disabled="true";
var okay = 'no';
}
else
{
document.getElementById('year').style.border='2px solid #00FF00';
}

if(okay != 'no')
{
document.getElementById('submit').style.border='2px solid #00FF00';
document.getElementById('submit').disabled="";
}
}

}
else
{
document.getElementById(target_div).innerHTML = MyHttpLoading; // still working
}
}
MyHttpRequest.send(null);
}
else 
{
document.getElementById(target_div).innerHTML = ErrorMSG; // the browser was unable to create a httprequest
}
}
// end of "AJAX" function


// Here follows a function to urlencode the string we run through our httprequest, it has nothing to do with AJAX itself
// If you look carefully in the above httprequest you se that we use this url_encode function around the file and query_string
// This is very handy since we are using GET in our httprequest and for instance
// any occurrance of the char # (from textboxes etc) will brake the string we are sending to our file - we don't want that to brake!
// It will also convert spaces to +

function url_encode(string)
{
var string;
var safechars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/-_.&?=";
var hex = "0123456789ABCDEF";
var encoded_string = "";
for(var i = 0; i < string.length; i++)
{
var character = string.charAt(i);
if(character == " ")
{
encoded_string += "+";
}
else if(safechars.indexOf(character) != -1)
{
encoded_string += character;
}
else
{
var hexchar = character.charCodeAt(0);
if(hexchar > 255)
{
encoded_string += "+";
}
else
{
encoded_string += "%";
encoded_string += hex.charAt((hexchar >> 4) & 0xF);
encoded_string += hex.charAt(hexchar & 0xF);
}
}
}
return encoded_string;
}

   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } 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) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('register').innerHTML = result;            
			document.getElementById('information').innerHTML = 'Success!, Welcome to Stangle.Info.  Check your email for account validation instructions.';
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "username=" + encodeURI( escape(document.getElementById("username").value )) +
                    "&email=" + encodeURI( escape(document.getElementById("email").value )) +
					"&password=" + encodeURI( escape(document.getElementById("password").value )) +
					"&displayname=" + encodeURI( escape(document.getElementById("displayname").value )) +
					"&firstname=" + encodeURI( escape(document.getElementById("firstname").value )) +
					"&lastname=" + encodeURI( escape(document.getElementById("lastname").value )) +
					"&gender=" + encodeURI( escape(document.getElementById("gender").value )) +
					"&country=" + encodeURI( escape(document.getElementById("country").value )) +
					"&city=" + encodeURI( escape(document.getElementById("city").value )) +
					"&state=" + encodeURI( escape(document.getElementById("state").value )) +
					"&month=" + encodeURI( escape(document.getElementById("month").value )) +
					"&day=" + encodeURI( escape(document.getElementById("day").value )) +
					"&year=" + encodeURI( escape(document.getElementById("year").value )) ;
      makePOSTRequest('registervalidation.php?field=register', poststr);
   }

// end .js file