//This will contain common javascript functions used in each page

//This function will change the language of the page
function setLanguage(language)
{
	//Initializing variables
	var currentPathArray = "";
	var newURL = "";
	var newPath = "";

	//Getting current url path
	var currentPath = window.location.pathname;

	//If currentPath is not available then set a default path to it 
	if(!currentPath.length || currentPath == "index.html" || currentPath == "/")
	{
		currentPath = "eng.index.html";
	}

	//Splitting the currentPath string at '.' and assigning the new prefix to this
	var currentPathArray = currentPath.split(".");

	if(language.length)
	{
		currentPathArray[0] = language;
	}
	else
	{
		currentPathArray[0] = "eng";
	}
	var newPath = currentPathArray.join(".");
	var newURL = window.location.protocol + "//" + window.location.host + "/" + newPath + "" + window.location.search + "" + window.location.hash;
	if(newURL.length > newPath.length)
	{
		window.location.href = newURL;
	}
	else
	{
		window.location.href = "http://www.bagtoearth.com/eng.index.html";
	}

}

function setLanguageIndex(language)
{
	if(language == 'fr')
	{
		window.location.href = "http://www.bagtoearth.com/fr.index.html" ;
	}
	else
	{
		window.location.href = "http://www.bagtoearth.com/eng.index.html"
	}
}

function openWinVideos(pageName)
{
	var newWindow = window.open(pageName,'_blank',
    "height=400,width=650,status=no,toolbar=no,menubar=no,location=no,fullscreen=no");
}


 // Initialize version 1.0 of Google AJAX API
 google.load("language", "1");

//Translates from english to any language
 function translate(lang) {
   var source = document.getElementById("source").innerHTML;
   var len = source.length;

   // Google Language API accepts 500 characters per request 
   var words = 500;

   // This is for English pages, you can change the
   // sourcelang variable for other languages
   var sourcelang = "en";
   document.getElementById("translate").innerHTML = "";

  for(i=0; i<=(len/words); i++) 
 {
     google.language.translate (source,"en", lang, function (result) {
     if (!result.error) {
     document.getElementById("translate").innerHTML
           = document.getElementById("translate").innerHTML
           + result.translation;
    } }); 
}  

  // Hide the text written in the original language
  document.getElementById("source").style.display = 'none';
  return false;
 }

