var xmlHttp;

function getXmlHttpObject( handler ) {
	var objXmlHttp=null

	if ( navigator.userAgent.indexOf( "Opera" ) >= 0 ) {
		alert( "This example doesn't work in Opera" )
		return
	}
	if ( navigator.userAgent.indexOf( "MSIE" ) >=0 ) {
		var strName="Msxml2.XMLHTTP"
		if ( navigator.appVersion.indexOf( "MSIE 5.5" ) >= 0 ){
			strName = "Microsoft.XMLHTTP"
		}
		try{ 
			objXmlHttp = new ActiveXObject( strName )
			objXmlHttp.onreadystatechange = handler
			return objXmlHttp
		}catch( e ) { 
			alert( "Error. Scripting for ActiveX might be disabled" )
			return
		}
	}
	if ( navigator.userAgent.indexOf( "Mozilla" ) >= 0 ) {
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler
		return objXmlHttp
	}
}

function validateEmail ( email ) {
    var url = '/include/ajax/ajaxbroker.php?action=validateEmail&email=' + email;
    xmlHttp=getXmlHttpObject( handleValidateEmail );
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleValidateEmail() {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        msg = xmlHttp.responseText;
        if ( msg != 'ok' ) {
            document.getElementById('errorInfo1').innerHTML = "This email address is already in use.<br>";
        }
        else {
            document.getElementById('errorInfo1').innerHTML = "";
        }
    }
}

function validateUserName ( userName ) {
    var url = '/include/ajax/ajaxbroker.php?action=validateUserName&userName=' + userName;
    xmlHttp=getXmlHttpObject( handleValidateUserName );
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleValidateUserName() {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        msg = xmlHttp.responseText;
        if ( msg != 'ok' ) {
            document.getElementById('errorInfo2').innerHTML = "This user name is already in use.<br>";
        }
        else {
            document.getElementById('errorInfo2').innerHTML = "";
        }
    }
}

function validateFriendUserName ( userName ) {
    var url = '/include/ajax/ajaxbroker.php?action=validateFriendUserName&userName=' + userName;
    xmlHttp=getXmlHttpObject( handleValidateFriendUserName );
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleValidateFriendUserName() {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        msg = xmlHttp.responseText;
        if ( msg == 'not user' ) {
            document.getElementById('errorInfo2').innerHTML = "The user name is not exist.<br>";
        }else if (msg == 'pending'){
            document.getElementById('errorInfo2').innerHTML = "The user name is already invited.<br>";
        }else if (msg == 'friend'){
            document.getElementById('errorInfo2').innerHTML = "The user name is already your friend.<br>";
        }
        else {
            document.getElementById('errorInfo2').innerHTML = "";
        }
    }
}

function validateFriendEmail ( email ) {
    var url = '/include/ajax/ajaxbroker.php?action=validateFriendEmail&email=' + email;
    xmlHttp=getXmlHttpObject( handleValidateFriendEmail );
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleValidateFriendEmail() {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        msg = xmlHttp.responseText;
        if ( msg == 'not user' ) {
            document.getElementById('errorInfo1').innerHTML = "The email is not exist.<br>";
        }else if (msg == 'pending'){
            document.getElementById('errorInfo1').innerHTML = "The user is already invited.<br>";
        }else if (msg == 'friend'){
            document.getElementById('errorInfo1').innerHTML = "The user is already your friend.<br>";
        }
        else {
            document.getElementById('errorInfo1').innerHTML = "";
        }
    }
}

function sendUserRequest ( userId ) {
    var url = '/include/ajax/ajaxbroker.php?action=sendUserRequest&userId=' + userId;
    xmlHttp=getXmlHttpObject(function(){handleSendUserRequest(userId)});
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleSendUserRequest(userId) {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        document.getElementById('request-'+userId).innerHTML = "Friend Request Sent...";
        document.getElementById('request-'+userId).href = "";
        document.getElementById('request-'+userId).attributes['onclick'].value = "return false;";
        document.getElementById('request-'+userId).style.textDecoration = "none";
        document.getElementById('request-'+userId).style.color = "black";
	}
}
function confirmRequest ( userId ) {
    var url = '/include/ajax/ajaxbroker.php?action=confirmRequest&userId=' + userId;
    xmlHttp=getXmlHttpObject(function(){handleConfirmRequest(userId)});
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleConfirmRequest(userId) {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        document.getElementById('confirm-'+userId).innerHTML = "Friend Request Confirmed...";
        document.getElementById('confirm-'+userId).href = "";
        document.getElementById('confirm-'+userId).attributes['onclick'].value = "return false;";
        document.getElementById('confirm-'+userId).style.textDecoration = "none";
        document.getElementById('confirm-'+userId).style.color = "black";
        document.getElementById('decline-'+userId).innerHTML = "";
        document.getElementById('bar-'+userId).innerHTML = "";
	}
}
function declineRequest ( userId ) {
    var url = '/include/ajax/ajaxbroker.php?action=declineRequest&userId=' + userId;
    xmlHttp=getXmlHttpObject(function(){handleDeclineRequest(userId)});
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleDeclineRequest(userId) {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        document.getElementById('decline-'+userId).innerHTML = "Friend Request Declined...";
        document.getElementById('decline-'+userId).href = "";
        document.getElementById('decline-'+userId).attributes['onclick'].value = "return false;";
        document.getElementById('decline-'+userId).style.textDecoration = "none";
        document.getElementById('decline-'+userId).style.color = "black";
        document.getElementById('confirm-'+userId).innerHTML = "";
        document.getElementById('bar-'+userId).innerHTML = "";
	}
}

function validateSchool (schoolName) {
    var url = '/include/ajax/ajaxbroker.php?action=validateSchool&schoolName=' + schoolName;
    xmlHttp=getXmlHttpObject( handleValidateSchool);
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleValidateSchool() {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
        msg = xmlHttp.responseText;
        if ( msg != 'ok' ) {
            document.getElementById('errorInfo2').innerHTML = "This school is not recognized.<br>";
        }
        else {
            document.getElementById('errorInfo2').innerHTML = "";
        }
    }
}

function changeCity(element,selectedElement) {
    var cityName = element.value;
    var url = '/include/ajax/ajaxbroker.php?action=changeCity&cityName=' + cityName + '&city_id=' + selectedElement;
    xmlHttp=getXmlHttpObject(handleChangeCity);
    xmlHttp.open( "GET", url , true );
    xmlHttp.send( null );
}

function handleChangeCity() {
    if ( xmlHttp.readyState==4 || xmlHttp.readyState=="complete" ) { 
       eval(xmlHttp.responseText);
       var form = document.getElementById('city').form;
       var sel = form.neighborhood;
	   if (city_id >0)
	   {
			document.getElementById('city_id').value=city_id;
	   }
       sel.options.length = 0;
       if (arr_value.length == 1) {
            document.getElementById('id_neighborhood').style.display = "none";
       }
       else {
            document.getElementById('id_neighborhood').style.display = "";
       }
       for (var i=0;i<arr_value.length;i++){
           sel.options[sel.options.length] = new Option(arr_text[i], arr_value[i], false, false)
        }
    }
}