var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
/*if(sPage == 'searchcollege.php') sPage = 'editcollege.php';
if(sPage == 'searchschool.php') sPage = 'editschool.php';*/
function sprintf(){
	if (!arguments || arguments.length < 1 || !RegExp){
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str)){
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);
		numMatches++;
		if (pType == '%'){
			subst = '%';
		}
		else{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length){
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
			var justifyRight = true;
			var precision = -1;
			var minLength = -1;

			if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			else if (pPad) pad = pPad;

			if (pJustify && pJustify === "-") justifyRight = false;

			if (pMinLength) minLength = parseInt(pMinLength);

			if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
			switch(pType){
				case 'b': subst = parseInt(param).toString(2); break;
				case 'c': subst = String.fromCharCode(parseInt(param)); break;
				case 'd': subst = parseInt(param) ? parseInt(param) : 0; break;
				case 'u': subst = Math.abs(param); break;
				case 'f': subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param); break;
				case 'o': subst = parseInt(param).toString(8); break;
				case 's': subst = param; break;
				case 'x': subst = ('' + parseInt(param).toString(16)).toLowerCase(); break;
				case 'X': subst = ('' + parseInt(param).toString(16)).toUpperCase(); break;
			}
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

function countryAjaxRequest(type,callback,list){
	showLoader();
	if($('country_code_id')) $('country_code_id').value='';
	if($('country_name_id')) $('country_name_id').value='';
	if($('region_code_id')) $('region_code_id').value='';
	if($('region_name_id')) $('region_name_id').value='';
	if($('city_code_id')) $('city_code_id').value='';
	if($('city_name_id')) $('city_name_id').value='';
	if($('item_code_id')) $('item_code_id').value='';
	if($('item_name_id')) $('item_name_id').value='';
	//$('listdesc').innerHTML = '';
	createCountryListBreadcrumb();
	if((list != 'searchcollege') && (list != 'searchcity')){
		toggleAddPendingSchool('none',list);
		toggleAddUserSchool('none','','',list);
	}
	else{
		toggleSearch('none','','',list);
	}
	var url = sPage+'?xml=co&disp='+type+'&list='+list;
	new Ajax(url,{method: 'get', onComplete: callback, update:'navigator'}).request();
	return false;
};
function regionAjaxRequest(type,callback,co,coname,initial,list){
	showLoader();
	if($('country_code_id')) $('country_code_id').value=co;
	if($('country_name_id')) $('country_name_id').value=coname;
	createRegionListBreadcrumb(list);
	if((list != 'searchcollege') && (list != 'searchcity')){
		toggleAddPendingSchool('none',list);
		toggleAddUserSchool('none','','',list);
	}
	else{
		toggleSearch('none','','',list);
	}

	//showListDescription('states/provinces',coname,initial);
	var url = sPage+'?xml=re&disp='+type+'&country='+co+'&coname='+coname+'&ini='+initial+'&list='+list;
	/*var handler = 'regionAjaxRequest(\''+type+'\',showRegionResponse,\''+co+'\',\''+coname+'\',\'%s\',\''+list+'\');';
	createAlphaFilter(handler,initial);*/
	new Ajax(url,{method: 'get', onComplete: callback, update:'navigator'}).request();
	return false;
};
function cityAjaxRequest(type,callback,co,re,rename,initial,list){
	showLoader();
	if($('region_code_id')) $('region_code_id').value=re;
	if($('region_name_id')) $('region_name_id').value=rename;
	createCityListBreadcrumb(list);
	if((list != 'searchcollege') && (list != 'searchcity')){
		toggleAddPendingSchool('none',list);
		toggleAddUserSchool('none','','',list);
	}
	else{
		toggleSearch('none','','',list);
	}
	var url = sPage+'?xml=ci&disp='+type+'&country='+co+'&region='+re+'&stname='+rename+'&ini='+initial+'&list='+list;
	//var handler = 'cityAjaxRequest(\''+type+'\',showCityResponse,\''+co+'\',\''+re+'\',\''+rename+'\',\'%s\',\''+list+'\');';
	//createAlphaFilter(handler,initial);
	new Ajax(url,{method: 'get', onComplete: callback, update:'navigator'}).request();
	return false;
};
function schoolAjaxRequest(type,callback,co,re,ci,ciname,initial,list){
	showLoader();

	if((list != 'colleges') && (list != 'searchcollege')){
		if($('city_code_id')) $('city_code_id').value=ci;
		if($('city_name_id')) $('city_name_id').value=ciname;
	}
	createSchoolListBreadcrumb(list);
	if((list != 'searchcollege') && (list != 'searchcity')){
		toggleAddPendingSchool('none',list);
		toggleAddUserSchool('none','','',list);
	}
	else{
		toggleSearch('none','','',list);
	}

	var url = sPage+'?xml=schools&disp='+type+'&country='+co+'&region='+re+'&city='+ci+'&ctname='+ciname+'&ini='+initial+'&list='+list;
	new Ajax(url,{method: 'get', onComplete: callback, update:'navigator'}).request();
	return false;
};
function showLoader(){
    $('breadcrumb').style.visibility = "hidden";
    $('currstatus').innerHTML = '';
	$('navigator').style.display = "block";
	$('loading').innerHTML = "<img src='http://images.friendster.com/images/ajax-loader.gif'>";
	$('loading').style.display = "block";
	
}
function hideLoader(){
	$('loading').innerHTML = "";
	$('loading').style.display = "none";
	$('breadcrumb').style.visibility = "visible";
}
function showListDescription(type,from,initial){
	desc = 'Showing '+type+' in <i>'+from+'</i>';
	if(initial != '') desc += " beginning with '<i>"+initial+"</i>'";
	$('listdesc').innerHTML = desc;
}
function showCountriesResponse(request){
	hideLoader();
	//$('listdesc').innerHTML = '';
};
function showRegionResponse(request){
	hideLoader();
};
function showCityResponse(request){
	hideLoader();
};
function showSchoolResponse(request){
	hideLoader();
};
function createCountryListBreadcrumb(){
	$('breadcrumb').innerHTML='Select your country';
};
function createRegionListBreadcrumb(list){
	$('breadcrumb').innerHTML='<a class="fakeLink" onclick="countryAjaxRequest(\'html\',showCountriesResponse,\''+list+'\');">Select your country</a> > '+$('country_name_id').value;
};
function createCityListBreadcrumb(list){
	$('breadcrumb').innerHTML='<a class="fakeLink" onclick="countryAjaxRequest(\'html\',showCountriesResponse,\''+list+'\');">Select your country</a> > '+
								'<a class="fakeLink" onclick="regionAjaxRequest(\'html\',showRegionResponse,\''+
								$('country_code_id').value+'\',\''+
								$('country_name_id').value+'\',\'*\',\''+list+'\');">'+
								$('country_name_id').value+'</a>';
	if($('region_name_id').value != ''){
	   $('breadcrumb').innerHTML +=	' > '+$('region_name_id').value;	
	}					
};
function createSchoolListBreadcrumb(list){
	if((list == 'colleges') || (list == 'searchcollege')) createCityListBreadcrumb(list);
	else{
    	$('breadcrumb').innerHTML='<a class="fakeLink" onclick="countryAjaxRequest(\'html\',showCountriesResponse,\''+list+'\');">Select your country</a> > '+
    								'<a class="fakeLink" onclick="regionAjaxRequest(\'html\',showRegionResponse,\''+
    								$('country_code_id').value+'\',\''+
    								$('country_name_id').value+'\',\'*\',\''+list+'\');">'+
    								$('country_name_id').value+'</a>';
    	if($('region_name_id').value != ''){							
    	   $('breadcrumb').innerHTML += ' > <a class="fakeLink" onclick="cityAjaxRequest(\'html\',showCityResponse,\''+
    								$('country_code_id').value+'\',\''+
    								$('region_code_id').value+'\',\''+
    								$('region_name_id').value+'\',\'*\',\''+list+'\');">'+
    								$('region_name_id').value+'</a>';
    	}
    	
    	if($('city_name_id').value != ''){
    	   $('breadcrumb').innerHTML +=	' > '+$('city_name_id').value;	
    	}
	}
};
function createFormListBreadcrumb(list){
	$('breadcrumb').innerHTML='<a class="fakeLink" onclick="countryAjaxRequest(\'html\',showCountriesResponse,\''+list+'\');">Select your country</a> > '+
								'<a class="fakeLink" onclick="regionAjaxRequest(\'html\',showRegionResponse,\''+
								$('country_code_id').value+'\',\''+
								$('country_name_id').value+'\',\'*\',\''+list+'\');">'+
								$('country_name_id').value+'</a> ';
	if($('region_name_id').value != ''){
	    $('breadcrumb').innerHTML += ' > <a class="fakeLink" onclick="cityAjaxRequest(\'html\',showCityResponse,\''+
								$('country_code_id').value+'\',\''+
								$('region_code_id').value+'\',\''+
								$('region_name_id').value+'\',\'*\',\''+list+'\');">'+
								$('region_name_id').value+'</a>';
	}

	if((list != 'colleges') && (list != 'searchcollege') && ($('city_name_id').value != '')){
		$('breadcrumb').innerHTML += ' > <a class="fakeLink" onclick="schoolAjaxRequest(\'html\',showSchoolResponse,\''+
								$('country_code_id').value+'\',\''+
								$('region_code_id').value+'\',\''+
								$('city_code_id').value+'\',\''+
								$('city_name_id').value+'\',\'*\',\''+list+'\');">'+
								$('city_name_id').value+'</a>';
	}

};
function toggleAddPendingSchool(dispstyle,list){
	$('submit_pending_schoolid').value = 1;
	$('submit_user_schoolid').value = 0;
	$('alphafilter').innerHTML = '';
	$('navigator').innerHTML = '';
	$('addPendingSchool').style.display = dispstyle;
	//$('listdesc').innerHTML = '';
	//$('listdesc').display = 'none';
	if(dispstyle == 'block'){
		createFormListBreadcrumb(list);
		var loctxt = $('country_name_id').value+', '+$('region_name_id').value;
		if($('city_name_id').value != '') loctxt += ', '+ $('city_name_id').value;
		$('selected_location').innerHTML = loctxt;
	}
	
	if($('region_code_id').value == ''){
	    $('goback_pending').onclick = function(){ regionAjaxRequest('html',showRegionResponse,
								$('country_code_id').value,
								$('country_name_id').value,'*',list);};								
	}
	else if($('city_code_id').value == ''){	    
	    $('goback_pending').onclick = function(){ cityAjaxRequest('html',showCityResponse,
    								$('country_code_id').value,
    								$('region_code_id').value,
    								$('region_name_id').value,'*',list);};
	}
	else{
    	$('goback_pending').onclick = function(){ schoolAjaxRequest('html',showSchoolResponse,
    								$('country_code_id').value,
    								$('region_code_id').value,
    								$('city_code_id').value,
    								$('city_name_id').value,'*',list);};	    
	}
	$('goback_pending').className = 'fakeLink';
};
function toggleAddUserSchool(dispstyle,icode,iname,list){
	$('submit_pending_schoolid').value = 0;
	$('submit_user_schoolid').value = 1;
	$('alphafilter').innerHTML = '';
	$('navigator').innerHTML = '';
	$('addUserSchool').style.display = dispstyle;
	//$('listdesc').innerHTML = '';
	//$('listdesc').display = 'none';
	if($('item_id_id')) $('item_id_id').value=icode;
	if($('item_name_id')) $('item_name_id').value=iname;
	if(dispstyle == 'block'){
		createFormListBreadcrumb(list);
		var utxt = $('item_name_id').value;
		var uloctxt = $('country_name_id').value+', '+$('region_name_id').value;
		if($('city_name_id').value != '') uloctxt += ', '+ $('city_name_id').value;

		$('selected_school').innerHTML = utxt;
		$('selected_school_location').innerHTML = uloctxt;
	}

	if($('region_code_id').value == ''){
	    $('goback').onclick = function(){ regionAjaxRequest('html',showRegionResponse,
								$('country_code_id').value,
								$('country_name_id').value,'*',list);};								
	}
	else if($('city_code_id').value == ''){	    
	    $('goback').onclick = function(){ cityAjaxRequest('html',showCityResponse,
    								$('country_code_id').value,
    								$('region_code_id').value,
    								$('region_name_id').value,'*',list);};
	}
	else{
    	$('goback').onclick = function(){ schoolAjaxRequest('html',showSchoolResponse,
    								$('country_code_id').value,
    								$('region_code_id').value,
    								$('city_code_id').value,
    								$('city_name_id').value,'*',list);};	    
	}
	$('goback').className = 'fakeLink';
};

function toggleSearch(dispstyle,icode,iname,list){
	$('alphafilter').innerHTML = '';
	$('navigator').innerHTML = '';
	$('searchForm').style.display = dispstyle;
	//$('listdesc').innerHTML = '';
	//$('listdesc').display = 'none';
	if($('item_id_id')) $('item_id_id').value=icode;
	if($('item_name_id')) $('item_name_id').value=iname;
	if(dispstyle == 'block'){
		createFormListBreadcrumb(list);
		var utxt = $('item_name_id').value;
		var uloctxt = $('country_name_id').value+', '+$('region_name_id').value;
		if($('city_name_id').value != '') uloctxt += ', '+ $('city_name_id').value;

		$('selected_school').innerHTML = utxt;
		$('selected_school_location').innerHTML = uloctxt;
	}

	if($('region_code_id').value == ''){
	    $('goback').onclick = function(){ regionAjaxRequest('html',showRegionResponse,
								$('country_code_id').value,
								$('country_name_id').value,'*',list);};								
	}
	else if($('city_code_id').value == ''){	    
	    $('goback').onclick = function(){ cityAjaxRequest('html',showCityResponse,
    								$('country_code_id').value,
    								$('region_code_id').value,
    								$('region_name_id').value,'*',list);};
	}
	else{
    	$('goback').onclick = function(){ schoolAjaxRequest('html',showSchoolResponse,
    								$('country_code_id').value,
    								$('region_code_id').value,
    								$('city_code_id').value,
    								$('city_name_id').value,'*',list);};	    
	}
	$('goback').className = 'fakeLink';
}

function createAlphaFilter(handler,initial){
	alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	/*if(initial != ''){
		filter = '<a class="fakeLink" onclick="'+sprintf(handler, '')+'"><strong>***</strong></a> ';
	}
	else{
		filter = '<strong>***</strong> ';
	}*/
    filter = '';
	for (i=0; i<alpha.length;i++) {
	    letter=alpha.substring(i,i+1);
	    if(letter != initial){
	    	filter += '<a class="fakeLink" onclick="'+sprintf(handler, letter)+'"><strong>'+letter+'</strong></a>  ';
	    }
	    else{
	    	filter += '<strong>'+letter+'</strong>  ';
	    }
	}
	$('alphafilter').innerHTML = filter;
}
function toggleNavigator(dispstyle){
    if(dispstyle=='show'){
        $('addschoolspan').innerHTML = '';
        $('addschoolspan').style.display = 'none';
        $('navigator_control').style.display = 'block';
    }
}
function submitForm(hid,hid2){
	$(hid).value = 1;
	$(hid2).value = 0;
	//document.getElementById('schoolform_id').submit();
	//document.forms['schoolform_id'].submit();
	$('schoolform_id').submit();
	return false;
}
function submitSearch(){
	$('searchform_id').submit();
}
//-----------------------------------------------------------------------------------
// asynchronous call for rss feeds
var xmlreq;
var isIE = ((window.ActiveXObject) ? true : false);

function getXML(url) {
  if(window.XMLHttpRequest){
    xmlreq = new XMLHttpRequest();

    window.setTimeout(function() {
      if (xmlreq.readyState < 4) {
        xmlreq.abort();
      }
    },10000); // abort after 10 sec

    try{
      xmlreq.onreadystatechange = xmlResponseHandler;
      xmlreq.open("GET", url, true);
      xmlreq.send(null);
    } catch(e){
      xmlreq.abort();
      return;
    }
  } else if(window.ActiveXObject){
    isIE = true;
    try{
      xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e){
      xmlreq.abort();
      return;
    }
    if(xmlreq){
      window.setTimeout(function() {
        if (xmlreq.readyState < 4) {
          xmlreq.abort();
        }
      },10000); // abort after 10 sec

      try{
        xmlreq.onreadystatechange = xmlResponseHandler;
        xmlreq.open("GET", url, true);
        xmlreq.send();
      } catch(e){
        xmlreq.abort();
        return;
      }
    }
  }
}


function showSchoolFields() {

}

function showCollegeFields() {

}
