


////////////////////////////////////////////////////////////////////////////////
////////////////////// url helpers //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////	

function MakeSeoSafe(str)
{
	return str.toLowerCase().replace(/\(|\)|\/|\s|\-|'|_|\.|,|&/g, "-").replace(/-+/g,"-");
}



////////////////////////////////////////////////////////////////////////////////
////////////////////// date helpers //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////	
	
function GetDate()
{
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	if (minutes < 10)
		minutes = "0" + minutes
	var seconds = currentTime.getSeconds();
	if (seconds < 10)
		seconds = "0" + seconds;

	var sDateStr = month + "-" + day + "-" + year + " " + hours + "-" + minutes + "-" + seconds;
	return sDateStr;
}

	
////////////////////////////////////////////////////////////////////////////////
////////////////////// array helpers //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////	
	
	
////////////////////////////////////////////////////////////////////////////////
////////////////////// array helpers //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////	
	
	
////////////////////////////////////////////////////////////////////////////////
////////////////////// array helpers //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////	
	




////////////////////////////////////////////////////////////////////////////////
////////////////////// susa helpers //////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////	
	

function SwitchLanguage(lang)
{
	//lang = "es";
	var sPathToWebRoot = (susa.globals.sPathToWebRoot == undefined) ? "" : susa.globals.sPathToWebRoot;
	var newurl = "http://" + location.host + "/" + sPathToWebRoot + lang + "/";
	var url = location.href;
	var urlminushost = url.replace(susa.globals.sAbsPathToWebRoot, "");

	// case of only domain, not even lang code ex: http://studyusa.com/
	if(urlminushost == "")
	{
	}
	else
	{
		var fMatchFound = false;
		
		// check if in an article if so redirect to the article home
		var sSearch = /\/([a-z]{2}|zh-tw|zh-cn)\/a\/([a-z0-9\-]+)/;
		var temp = url.match(sSearch);

		if(temp != null)
		{
			newurl = newurl + "a/sections/";
			fMatchFound = true;
		}
		
		// else just switch the lang
		if(fMatchFound == false)
		{
			var sSearch = /(\.com|\.loc)\/([a-z]{2}|zh-tw|zh-cn)([\/]?)/;
			var temp = url.match(sSearch);
			
			if(temp != null)
			{
				var langmarker = temp[2];
				
				var aPathParts = location.pathname.split(langmarker + "/");
				var sBuffer = "";
				for(var x in aPathParts)
				{
					if(x == 0)
					{}
					else if(x == 1)
						sBuffer += aPathParts[x];
					else
						sBuffer += langmarker + "/" + aPathParts[x];
				}
				
				var sCurrentPathFromLangCode = sBuffer;
				
				if(sCurrentPathFromLangCode)
					newurl = newurl + sCurrentPathFromLangCode;
			}
		}
	}
	
	//console.log(newurl);
	window.location.href = newurl;
}

function GetCurrentLanguage()
{
	var url = location.href;
	var sSearch = /\/([a-z]{2}|zh-tw|zh-cn)(\/|$)/;
	var aResult = sSearch.exec(url);
	if(aResult != null && aResult.length > 1)
		return aResult[1];
	else
		return "en";
}

function RegisterJumpMenu()
{
	if($("select#jumpMenu").length > 0)
	{
		$("select#jumpMenu").change(function()
		{
			var lang = $(this).val();
			SwitchLanguage(lang);
		});
	}
}

function GoToTop()
{ 
	var sUrl = window.location.toString();
	var iHashPos = sUrl.indexOf('#'); 
	if(iHashPos > 0)
	{
		sUrl = sUrl.substr(0,iHashPos); //strip of hash to end if present
	}
	window.location = sUrl + "#topofpage";
}


function ReloadUrlWithNoAnchors()
{
	if(window.location.href != "")
	{
		var url = window.location.href;
		var sSearch = /(.*)#([A-Z]{2})$/;
		var temp = url.match(sSearch);

		if(temp != null)
		{
			//alert(temp[1]);
			window.location.href = temp[1];
		}
	}
}




function RecordStat()
{
	$.ajax({
		type: 'post',
		url: 'StatController.php?action=record_stat',
		data: {
			_sReferringPage: document.referrer,
			fFromStatic: 1,
			sLangCode: susa.globals.sLangCode
		},
		success: function(data) {}
	});
}
















//----------------------------------------------------------------------------->

// ie fix
if(typeof String.prototype.trim !== 'function') 
{
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}


var susa = {
	global: {}
};
susa.globals = {};

susa.globals.sPathToWebRoot = "";
susa.globals.sAlternateBasePath = "http://studyusa.loc/";
susa.globals.sLangCode = GetCurrentLanguage();
susa.globals.sAbsPathToWebRoot = "http://" + location.host + "/" + susa.globals.sPathToWebRoot;


if(typeof $.datepicker == "object")
{
	// save original function to call in our new one
	var _gotoToday = $.datepicker._gotoToday;

	// make a new _gotoToday function that does what the old one
	// did, but adds some extra feature
	$.datepicker._gotoToday = function(id) {
			 _gotoToday.call( this, id );
			 var target = $(id),
					 inst = this._getInst(target[0]);

			//Added by Ryan Waterer on 1/30/2009 to have it return
			// the value when the person selects the "Today" button
			 this._selectDate(id, this._formatDate(inst,
					 inst.selectedDay, inst.drawMonth, inst.drawYear));
	}
}


