/* (c) 2006-7 Ghost(TM) Inc. (http://G.ho.st/home) . All rights reserved.
G.ho.st licenses this software to you under the terms of the 
Common Public License Version 1.0 http://www.opensource.org/licenses/cpl.php 
*/

/**====================================================================================== **
/*  File: general.js     				                                              **
/*  Creator: Mohammad Taweel...                                                             **
/*  Create date: 2009-01-28                                                               **
/*  Authors: Mohammad Taweel...					                                          **
/*                                                                                        **
/**====================================================================================== */
/* 
 * This javascript library should be included the first thing in vc.html & main.jsp, where it is responsible for
 * limiting the domains from where those pages are accessed.
*/


// VCURL defines the URL that the vc.html should be loaded from (or is loaded from) which is the main domain URL
// that is visible to the user or could be used directly in the browser to load the vc.html
var VCURL = window.location.protocol + "//" + window.location.host;


if ( (location.pathname == "/") || (location.pathname == "/home/") || ((location.pathname == "/main.jsp") || (location.pathname == "/home/main.jsp")) || (location.pathname == "/vc.html") ) {
	if ( (location.hostname.indexOf("cdn.") >=0 ) || (location.hostname.indexOf("www.")>=0 ) || (location.hostname.indexOf("server.")>=0) || (location.hostname.indexOf("upload.")>=0) || (location.hostname.indexOf("secureapi.")>=0) ) {
	// if the user is calling the main.jsp or vc.html we need to make sure that he is doing so directly without adding any subdomain prefix's (e.g cdn, www ...)
	// The cases for this is that:
	// 1- if the user is calling the "server only" in the url (e.g. http://www.g.ho.st/)
	// 2- if the user is calling the server's home page only in the url (e.g. http://cdn.g.ho.st/home/)
	// 3- if the user is calling main.jsp directly (e.g. http://www.g.ho.st/main.jsp) or (http://www.g.ho.st/home/main.jsp)
	// 4- if the user is calling vc.html directly (e.g. http://cdn.g.ho.st/vc.html)
 	 	var cleanHost = location.host.substring(location.host.indexOf(".")+1);
 	 	var cleanLocation =  location.protocol + '//' + cleanHost + location.pathname+window.location.search;
 		window.location = cleanLocation;
	}
} 
/**
 * The main pages (homepage "main.jsp" & "vc.html" ) should not be accessed from the sharing domain.
 * As a security measure, this should prevent calling the sing-in API using the share domain which is used for G.ho.st
 * file system, where one can open a certain html file that contains a script that reads the cookies from the sharing
 * domain. 
 */
if (location.hostname.toLowerCase().match("sharing")) { // if name contains sharing
	
	var hName = location.hostname.toLowerCase();
	
	// files.ghostsharing.com
	// files.test.ghostsharing.com
	// ghostsharing.com
	// test.ghostsharing.com
	// files.dev.testghostsharing.com
	//dev.testghostsharing.com
	
	
	if(hName.match("testghostsharing")){ // coming from dev domains
		
		// files.qa.testghostsharing.com
		//qa.testghostsharing.com 
		
		var pref = "";
		
		if(hName.match("files")){ // with files prefix 
			pref = hName.split(".")[1];	
		}else{ // without files prefix 
			pref = hName.split(".")[0];
		}
		
		window.location.hostname=pref+".testghost.com";
		
	}else{ // coming from production domains
		// ghostsharing 
		
	
	// files.ghostsharing.com 
	//ghostsharing.com
	
	// files.test.ghostsharing.com 
	// test.ghostsharing.com
	
		if(hName.match("files")){
		
			var tokens = hName.split(".");
			if(tokens.length==3){
				
				window.location.hostname="g.ho.st";
				
			}else{
				
				window.location.hostname=tokens[1]+".g.ho.st";
				
			}
			
			
			
		}else{
			
			// no files 
			
			var tokens = hName.split(".");
			// ghostsharing.com 
			if(tokens.length==2){
				window.location.hostname="g.ho.st";
			}else{
				
				// sub.g.ho.st
				window.location.hostname=tokens[0]+".g.ho.st";
				
			}
			
		}
		
		
		var tokens = hName.split(".");
		
		
		if(hName.match("test")){ // files.test.ghostsharing.com or test.ghostsharing.com
				window.location.hostname="test.g.ho.st";
		}else{ // files.ghostsharing.com
			window.location.hostname="g.ho.st";
		}
		
	}
	
}

function getXMLHttpRequest() {
	var request = false;
	if (window.ActiveXObject) {
		var versions = ["Microsoft.XmlHttp","MSXML2.XmlHttp","MSXML2.XmlHttp.3.0","MSXML2.XmlHttp.4.0","MSXML2.XmlHttp.5.0"];
		for (var i = 0; i<versions.length; i++) {
			try {
				request  = new ActiveXObject(versions[i]);
				break;
			} catch(e) {}
		}
	} else if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	}
	return request;
}