var http = createRequestObject();

// Perhaps a stregety pattern should be used so we can just have all code submit to one formCheck() that udpates based on a passed "page" value.
// Then we could place all of your formCheck code in a single document and reduce complexity.
// Keith Brings.





function ajax_checkIinvite()
{



// <<EXTEND>>
// This code should use variable flags to inform check_form methods of it's results. 
// I will look into this later.
// <<//EXTEND>>

    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
			//alert(response);
            var response_code = response.split(',')[0]; // primitive. I suspect some sort of JSON approach could be used but this will suffice for now.
			var response_id   = response.split(',')[1];
			var response_user_id   = response.split(',')[2];
			ajax_checkIinvite_response[response_id] = response_code; 
			// Stores results with out our resorting to querying the test string and breaking our formcheck everytime the output text is changed.


			// response_code
			//
			// 0 = who knows. no response. Should be fine to invite.
			// 1 = user exists (may or may not be invited)
			// 2 = user already invited
			// 3 = empty email field.
			// 4 = invalid email
			///////////////////

			//
			// <<EXTEND>> Some Enumeration would be nice 
			//
			//////////
 
			


			switch(response_code)
			{ 
				
			case 0:    // 0 = who knows. no response. Should be fine to invite.
	            document.getElementById('check_friend' + response_id).style.color = 'green';
              document.getElementById('check_friend' + response_id).innerHTML = '';
			break;
			case 1:    // 1 = user exists (may or may not be invited)
	            document.getElementById('check_friend' + response_id).style.color = 'red';
              document.getElementById('check_friend' + response_id).innerHTML = 'That User is Already Registered. <a href="friendrequest.html?friend_id=' + response_user_id + '">Add to friends</a>.';
			break;
			case 2:   // 2 = user already invited
	          document.getElementById('check_friend' + response_id).style.color = 'red';
              document.getElementById('check_friend' + response_id).innerHTML = 'You Have Already Invited that Person!';
		    break;
			case 3: // 3 = empty email field.
	            document.getElementById('check_friend' + response_id).style.color = 'green';
              document.getElementById('check_friend' + response_id).innerHTML = '';		
			break; 	
			case 4:  // 4 = invalid email
	            document.getElementById('check_friend' + response_id).style.color = 'red';
              document.getElementById('check_friend' + response_id).innerHTML = 'That is not a Valid Email Address!';
			break;


			default:
			//document.getElementById('check_friend' + response_id).innerHTML = 'Invalid Response From Query';			
			// alert( "Invalid response recieved by ajax_checkIinvite  " , email );		
			};
        }
    }
    catch(e){}
    finally{}
}





//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<<EXTEND>>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// 
//   I am going to modify my ajax so that the source of origin nickname is included. 
//    I will then store return variables in that source file. In this manor our pages can all
//   access their specific result data. I will also begin passing the name of the target to update 
//   (for instance "isKosher_fs" this will alleviate the need for _shorthandfilename convention
//     I have used here to distinguish the ajax_isKosher_fs and ajax_isKosher functions.
//
// !!!!!!!!!!!!!!!<<EXTEND>>!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//    This is a priority going onwards. These functions will become ridiculous to deal with otherwise
//     and run the risk of peculiar interactions when multiple calls are joined on a single page.
//
//   -- Keith Brings 1/19/2008
/////////////////////////////////////////////////////////////////////////////////////////////////////





function ajax_isKosher()
{

    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
//            window.alert(response);
            if (response == '0') { 
            // the messages were not the same --> curse words or innapropriate chareceters.
			// It would be nice if I knew how to pass variables back here as I could list the adjusted search query.
	            document.getElementById('isKosher').style.color = 'red';
              document.getElementById('isKosher').innerHTML = 'Please only search for alphanumeric characters and remember no swearing!';
               clean = false;
            }
            else {
clean = true;
	            document.getElementById('isKosher').style.color = 'red';
              document.getElementById('isKosher').innerHTML = '';
            }
        }


    }

    catch(e){}
    finally{}
    proceed = true; // let searchbox->submit proceed.
}
function ajax_isKosher_fs()
{

    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
//            window.alert(response);
            if (response == '0') { 
            // the messages were not the same --> curse words or innapropriate chareceters.
			// It would be nice if I knew how to pass variables back here as I could list the adjusted search query.
	            document.getElementById('isKosher_fs').style.color = 'red';
              document.getElementById('isKosher_fs').innerHTML = 'Please only search for alphanumeric characters and remember no swearing!';
               clean_fs = false;
            }
            else {
clean_fs = true;
	            document.getElementById('isKosher_fs').style.color = 'red';
              document.getElementById('isKosher_fs').innerHTML = '';
            }
        }


    }

    catch(e){}
    finally{}
    proceed_fs = true; // let searchbox->submit proceed.
}

// the function is the same as check username, but the message is different
function ajax_checkReferrer()
{

	var responseArray = new Array();

    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
//            window.alert(response);  //response comes from ajax_check_username.html

			responseArray = response.split( ',,,' ); //tokenize response variable using ',,,' as separator
//			window.alert(responseArray[0]);
//			window.alert(responseArray[1]);

		  if (responseArray[0] == '0') {

			//replace old username if found to be vulgar in ajax_check_username.html
			document.getElementById('referrer').value = responseArray[1]; 
	         document.getElementById('referrer_status').style.color = 'red';
              document.getElementById('referrer_status').innerHTML = 'username not found!';

            }
            else {
               document.getElementById('referrer_status').style.color = 'green';
              document.getElementById('referrer_status').innerHTML = 'username found!';
           }
        }
    }
    catch(e){}
    finally{}
}

function ajax_checkUserName()
{

	var responseArray = new Array();

    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
//            window.alert(response);  //response comes from ajax_check_username.html

			responseArray = response.split( ',,,' ); //tokenize response variable using ',,,' as separator
			//window.alert(responseArray[0]);
			//window.alert(responseArray[1]);

			
		  if (responseArray[0] == 0) {

			//replace old username if found to be vulgar in ajax_check_username.html
			document.getElementById('username').value = responseArray[1]; 

              document.getElementById('username_available').style.color = 'green';
             username_exists = true;
              document.getElementById('username_available').innerHTML = 'username available!';
            }
            else {
            username_exists = false;
	         document.getElementById('username_available').style.color = 'red';
              document.getElementById('username_available').innerHTML = 'username not available!';
            }
        }
    }
    catch(e){}
    finally{}
}


function ajax_checkEmail()
{
    try{
    
        if((http.readyState == 4) && (http.status == 200)){
            var response = http.responseText;
//            window.alert(response);
	  
            if (response == 0) {
			email_exists = false;
            }
            else {
			email_exists = true;
            }
        }
    }
    catch(e){}
    finally{}
}



function ajax_getUserByName()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
//  window.alert(response);
            document.getElementById('recip_id').value = response;
        }
    }
    catch(e){}
    finally{}
}

function ajax_timePointAward()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
	    //setTimeout('resetTimer(' + response + ')', 2000);        
	    setTimeout('resetTimer(600)', 4000);
        
        var date = new Date();
        sendRequestTextGet('ajax_point_update.html', 'cachebust=' + date.getTime(), 'ajax_pointUpdate');
        }
    }
    catch(e){}
    finally{}

//  window.alert(response);

}

function ajax_petChatUpdate()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
          var response = http.responseText;
          var re = new RegExp('^<!-- chat -->');
          if (response.match(re))
		        document.getElementById('pet_chat_window').innerHTML = response;
        }
    }
    catch(e){}
    finally{}
}

function ajax_pointUpdate()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
		  	  var points = response;
		      document.getElementById('header_point_total').innerHTML = points  + ' CD';
        }
    }
    catch(e){}
    finally{}
}

function ajax_ratingUpdate() {
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
            eval("Data = " + response);
            if (!Data['avg_rating']) return true;
            //alert (Data);
            if (document.getElementById(Data['photo_id'] + '_avg_rating'))
              document.getElementById(Data['photo_id'] + '_avg_rating').innerHTML = Data['avg_rating'];
            if (document.getElementById(Data['photo_id'] + '_num_votes'))
              document.getElementById(Data['photo_id'] + '_num_votes').innerHTML = Data['num_votes'];
            rating = Math.ceil ( Data['avg_rating'] * 2) / 2;
            for (i = 1; i <= 5; i++) {
              img = document.getElementById(Data['photo_id'] + '_rating_' + i);
              if (rating >= i) img.name = 'star-rated';
              else if (rating >= i - 0.5) img.name = 'star-half';
              else img.name = 'star-blank';

              img.src = 'profile/' + img.name + '.gif';
              document.getElementById(Data['photo_id'] + '_ratinghref_' + i).onclick = function() { return false; };
              img.onmouseover = function() { };
              img.onmouseout = function() { };
            }
		      //document.getElementById('header_point_total').innerHTML = points  + ' CD';
        }
    }
    catch(e){}
    finally{}

    //alert (response);
}

function ajax_timeLeftUpdate()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
			timeLeft = response;
			resetTimer(timeLeft);
        }
    }
    catch(e){}
    finally{}
}

function ajax_changeLogin()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
		  	  var loginBoxHTML = response;
		      document.getElementById('ajaxafied_login').innerHTML = loginBoxHTML;
       }
    }
    catch(e){}
    finally{}
}

function ajax_changeLeaderBoard()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
		  	  var leadersDisplay = response;
		      document.getElementById('main_leaders_div').innerHTML = leadersDisplay;
       }
    }
    catch(e){}
    finally{}
}

function ajax_changeMyCdeLeaderBoard()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
		  	  var myCdeLeadersDisplay = response;
		      document.getElementById('myCDE_leaders').innerHTML = myCdeLeadersDisplay;
       }
    }
    catch(e){}
    finally{}
}

function ajax_ApproveDesign()
{
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
		  	  var approveDesign = response;
		      
       }
    }
    catch(e){}
    finally{}
}


function createRequestObject() {
    var xmlhttp;
    try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch(e) {
        try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
        catch(f) { xmlhttp=null; }
    }
    if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
        xmlhttp=new XMLHttpRequest();
    }

   return  xmlhttp;
}


function sendRequestTextGet(script_name, vars, response_handler) {
//  alert(script_name);
    var rnd = Math.random();
//    vars = escape(vars);
//    window.alert(script_name + ', ' + vars + ', ' + response_handler);
//    window.alert(vars);
var date = new Date();
if (vars.length == 0)
{
vars = "cachebust=" + date.getTime();
} else vars += "&cachebust=" + date.getTime();



//alert(vars);



    try{
        http.open('GET',  script_name + '?' + vars);
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        // Note: (Keith Brings) these if's should be daisy chained to improve efficiency.

	switch(response_handler)
        {
	case 'ajax_petChatUpdate':
        http.onreadystatechange = ajax_petChatUpdate;	
	break;
	
	case 'ajax_pointUpdate':
        http.onreadystatechange = ajax_pointUpdate;
        break;

        case 'ajax_getUserByName':
        http.onreadystatechange = ajax_getUserByName;
        break;

        case 'ajax_timePointAward':
        http.onreadystatechange = ajax_timePointAward;
        break;

        case 'ajax_ratingUpdate':
        http.onreadystatechange = ajax_ratingUpdate;
        break;

        case 'ajax_checkIinvite':
        http.onreadystatechange = ajax_checkIinvite;
        break;

        case 'ajax_isKosher_fs':
        http.onreadystatechange = ajax_isKosher_fs;
        break;

        case 'ajax_isKosher':
        http.onreadystatechange = ajax_isKosher;
        break;

        case 'ajax_checkEmail':
        http.onreadystatechange = ajax_checkEmail;
        break;

        case 'ajax_checkReferrer':
        http.onreadystatechange = ajax_checkReferrer;
        break;

        case 'ajax_checkUserName':
        http.onreadystatechange = ajax_checkUserName;
        break;

       case 'ajax_changeLogin':
        http.onreadystatechange = ajax_changeLogin;
        break;

        case 'ajax_getInternalAd':
        http.onreadystatechange = ajax_getInternalAd;
        break;
        
        case 'ajax_Leaderboards':
        http.onreadystatechange = ajax_changeLeaderBoard;
        break;
        
        case 'ajax_myCdeLeaderboard':
        http.onreadystatechange = ajax_changeMyCdeLeaderBoard;
        break;
        


	default:
	//http.onreadystatechange = handleResponseText;
	break;

	}
        http.send(null);
    }
    catch(e){}
    finally{}
}


function handleResponseText() {
    try{
        if((http.readyState == 4)&& (http.status == 200)){
            var response = http.responseText;
            if (response == 0) {

            }
            window.alert(response);
        }
    }
    catch(e){alert("hello");}
    finally{}
}

function ajax_getInternalAd() {


}
