// Author: Al Sierra
// Version: 6.03.2009
// Description: AJAX for main functions
var tfy = {};
tfy.main = {};
tfy.main.ValidateString =
	 function (s){
        if(s==undefined || s==null || s.length==0) {
            return false;
        }
        
        // Check for white space
        reWhiteSpace = new RegExp(/^\s+$/);
        if (reWhiteSpace.test(s)) {
            return false;
        }
     
        //Check for html tags
        reHtmlTags = new RegExp(/<(.|\n)/);
        if (reHtmlTags.test(s)) {
            return false;
        }
        return true;
    };
tfy.main.ValidateEmptyString =
	 function (s){
        if(s==undefined || s==null || s.length==0) {
            return false;
        }
        
        // Check for white space
        reWhiteSpace = new RegExp(/^\s+$/);
        if (reWhiteSpace.test(s)) {
            return false;
        }
        return true;
    };
tfy.main.ValidateEmail = 
	function (email) {
        //Check if there is valid password data
        if(email==undefined || email==null || email.length==0) {
            return false;
        }
        // Check for white space
        reWhiteSpace = new RegExp(/^\s+$/);
        if (reWhiteSpace.test(email)) {
            return false;
        }
        //Check for proper email format
        var e1=email.indexOf("@");
        if (e1==-1) {
            return false;
        }else {
            var emailsub1 = email.substr(e1+1);
            var esub1=emailsub1.indexOf(".");
            if (esub1==-1) {
                return false;
            }
        }
        return true;
    };
tfy.main.ValidateNumber = 
	function (number) {
	 	reNumeric = new RegExp(/^[0-9]+$/);
        if (!reNumeric.test(number)) {
        	return false;
        }
        return true;
    };
tfy.main.ValidateFloat = 
	function (number) {
	 	reNumeric = new RegExp(/^[-+]?[0-9]+(\.[0-9]+)?$/);
        if (!reNumeric.test(number)) {
        	return false;
        }
        return true;
    };
tfy.main.ValidateZip = 
	function (number) {
		if(number.length != 5){
			return false;
		}
	 	reNumeric = new RegExp(/^[0-9]+$/);
        if (!reNumeric.test(number)) {
        	return false;
        }
        return true;
    };
tfy.main.ValidateHtmlTags = 
	function (s) {
	 	//Check for html tags
        reHtmlTags = new RegExp(/<(.|\n)/);
        if (reHtmlTags.test(s)) {
            return false;
        }
        return true;
    };
tfy.main.HtmlPopUp = 
	function(URL, params){
		var tfym = tfy.main;
		var opts = "";
		if( params != undefined ){
			for( var p in params ){ 
				opts += (opts!=""?",":"");
				if( p == 'center' ){
					opts += "top="+(document.viewport.getHeight()-params['height'])/2;
					opts += ",left="+(document.viewport.getWidth()-params['width'])/2;
				}else{
					opts += p+"="+params[p];
				}
			}
		};
	 	var day = new Date();
		var id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "',opts);");
	};
tfy.main.FacebookShare = 
	function(URL){
	 	var day = new Date();
		var id = day.getTime();
		eval("page" + id + " = window.open(URL, '" + id + "',  'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=400,left = 340,top = 200');");
	};

tfy.main.getQuerystring = 
	function (key, default_){
		if (default_==null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(window.location.href);
		if(qs == null)
			return default_;
		else
			return qs[1];
	}; 
tfy.main.ValidateModuleSearch = 
	function(){
		var kw = $('searchkeywordmodule').value;
		return tfy.main.ValidateSearch(kw);
	};
	
tfy.main.ValidateMainSearch = 
	function(){
		var kw = $('searchkeywordmain').value;
		return tfy.main.ValidateSearch(kw);
	};
	
tfy.main.ValidateSearch = 
	function(kw){
		if(kw == "SEARCH"){ return false;}
		if(!tfy.main.ValidateString(kw)){
			return false;
		}
		return true;
	};
	
// Start Cookie Code (Public Domain)
tfy.main.getCookie = 
	function ( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	};

tfy.main.setCookie = 
	function ( name, value, expires, path, domain, secure ) {
		//alert(value);
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
		  expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+"="+escape( value ) +
		  ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		  ( ( path ) ? ";path=" + path : "" ) +
		  ( ( domain ) ? ";domain=" + domain : "" ) +
		  ( ( secure ) ? ";secure" : "" );
	};

tfy.main.deleteCookie = 
	function ( name, path, domain ) {
		if ( tfy.main.getCookie( name ) ) document.cookie = name + "=" +
		  ( ( path ) ? ";path=" + path : "") +
		  ( ( domain ) ? ";domain=" + domain : "" ) +
		  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	};
// End Cookie Code (Public Domain)

//Pagination Function	
tfy.main.Pagination = 
	function(pagenum,extra,pagename){
		url = pagename+".php?pagenum="+pagenum+extra;
		window.location = url;
	};
	
tfy.main.getScreen = 
	function() {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	  }
	  return [myWidth, myHeight];
	};
tfy.main.getYOffset = 
	function() {
		var pageY;
		if(typeof(window.pageYOffset)=='number') {
		   pageY=window.pageYOffset;
		}
		else {
		   pageY=document.documentElement.scrollTop;
		}
		return pageY;
	};
tfy.main.CheckBrowser = 
	function() {
		if(BrowserDetect.browser == 'Explorer'){
			var browser_version = parseInt(BrowserDetect.version);
			if(browser_version < 7){
				if(!tfy.main.getCookie('browserie6')){
					tfy.main.setCookie('browserie6','true',30,'/',document.domain,false);
					window.location = 'browser_check.php';
				}
			}
		}
	};
	
tfy.main.CheckBrowserIE6 = 
	function() {
		if(BrowserDetect.browser == 'Explorer'){
			var browser_version = parseInt(BrowserDetect.version);
			if(browser_version < 7){
				return true;
			}
		}
		return false;
	};

tfy.main.SetWelcome = 
	function (webinar_id){
		var msg_data = {};
		msg_data.cmd = 'SetWelcome';
		msg_data.webinar_id = webinar_id;
		new Ajax.Request('scripts/php/web_videos.php?', {
			parameters: $H(msg_data).toQueryString(),
			evalScripts: true,
			onSuccess: function(transport) {
				try {
					var JSON = eval("("+transport.responseText+")");
					if (JSON.emsg) {
						alert(JSON.emsg);
						if (JSON.timeout_redirect){
							window.location = JSON.timeout_redirect;
						}
						return false;
					} 
				}catch(e) {
					alert("The server's response could not be understood.\nPlease try again.");
					return false;
				}
			},
			onComplete: function(){
			}			
		});
	};
//Show Pop
tfy.main.ShowPop =
	function(pop_window_id){
		$('pop_item-close').onclick = function() {tfy.main.ClosePop(pop_window_id);};
		//Effect.Center($('shop_item_pop_window'));
		tfy.main.positionPop(pop_window_id);
		$(pop_window_id).style.position="fixed";
		new Effect.Appear($(pop_window_id),{duration: .1, to:1.0});
	};
//Close Pop	
tfy.main.ClosePop =
	function(pop_window_id){
		 $(pop_window_id).style.display="none";
	};
//positioning the popup layer in the center of the screen.
tfy.main.positionPop =
	function (pop_window_id) {
 	var a = tfy.main.getScreen();
	var myWidth = a[0], myHeight = a[1];
	
	var cWindowHeight = parseInt($(pop_window_id).style.height);
	if(isNaN(parseInt(cWindowHeight))) {
		cWindowHeight = $(pop_window_id).offsetHeight;
	}
	var cWindowWidth = parseInt($(pop_window_id).style.width);
	var cWindowX = myWidth/2 - cWindowWidth/2;
	var cWindowY = myHeight/2 - cWindowHeight/2;
	$(pop_window_id).style.left =cWindowX + "px";
	$(pop_window_id).style.top = cWindowY + "px";
	
};

// function:	switchCheckboxes
// desc:		Will Check / Uncheck all Checkboxes
// params:		the_form = a HTML Id of a form
//				elements_name = the name of the checkbox / see note
//				switcher_name = UNKNOWN
// NOTE:		checkboxes need to have the same name as elements_name
// Example:	<input type="checkbox" name="my_ename[]">, will arrive as Array in php.
tfy.main.switchCheckboxes =
	function(the_form, elements_name, switcher_name) {
		var elements = document.getElementById(the_form).elements[elements_name];
		var elements_cnt = ( typeof (elements.length) != 'undefined') ? elements.length : 0;

		if (elements_cnt) {
			for (var i = 0; i < elements_cnt; i++) {
				elements[i].checked = document.forms[the_form].elements[switcher_name].checked;
			}
		} else {
			elements.checked = document.forms[the_form].elements[switcher_name].checked;
		}
		return true;
};
	
	
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
