/*
=============================================
 Development By: Infinity Arts Interactive, LLC - www.InfinityArts.com, © Copyright 2008 - All rights reserved.
 Description: Public JS Functions
 Date: 4/9/2008
=============================================
*/

// ============================================================ FUNCTIONS: WINDOW & BROWSER ============================================================
	
	var popWin = ""; // function:newWindow(oPage, oW, oH, oScroll); close_pops();
	
	function newWindow(oPage, oW, oH, oScroll){
		if (popWin) popWin.close();
		
		xpos = (screen.width) ? (screen.width - oW)/2 : 0;
		ypos = (screen.height) ? (screen.height - oH)/2 : 0;
		
		if (oScroll == 'Y')
			popWin = window.open(oPage,"newWin","toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+oW+",height="+oH+",left="+xpos+",top="+ypos);
		else
			popWin = window.open(oPage,"newWin","toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+oW+",height="+oH+",left="+xpos+",top="+ypos);
		
		popWin.focus();
	}
	
	function close_pops() {
		if (navigator.appName.toLowerCase() != "netscape") {
			if (popWin) popWin.close();
		}
	}
		
	// FIXES NETSCAPE RESIZE BUG BY RELOADING AFTER WINDOW RESIZE
	function WM_netscapeCssFix() {
		if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth || document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight)
			document.location = document.location;
	}
	
	function WM_netscapeCssFixCheckIn() {
		if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) {
			if (typeof document.WM == 'undefined'){
				document.WM = new Object;
			}
			
			if (typeof document.WM.WM_scaleFont == 'undefined') {
				document.WM.WM_netscapeCssFix = new Object;
				document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
				document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
			}
			
			window.onresize = WM_netscapeCssFix;
		}
	}
	
	WM_netscapeCssFixCheckIn();
	
	// FIXES MICROSOFT ACTIVATING ACTIVEX CONTROL WITH SWF FILES
	function IE_activexControlFix() {
		var theObjects = document.getElementsByTagName("object");
		
		for (var i = 0; i < theObjects.length; i++) { 
			theObjects[i].outerHTML = theObjects[i].outerHTML;
		}
	}
	
	// BROWSER DETECTION
	var BrowserDetect = {
		// returns: BrowserDetect.browser, BrowserDetect.version, BrowserDetect.OS;
		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: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{	string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{	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.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	BrowserDetect.init();

// ============================================================ FUNCTIONS: MISC ============================================================	
	
	function get_pageIdno() {
		var URL = location.href;
		var idno = '';
		
		if (URL.search("idno=") > -1) {			
			var temp_array = URL.split("?");
			temp_array = temp_array[1].split("&");
			
			for (var i=0; i<temp_array.length; i++) {
				var sub_array = temp_array[i].split("=");
				if (sub_array[0] == "idno") {
					idno = sub_array[1];
					break;
				}
			}
		} else {
			var temp_array = URL.split("/");
			
			for (var i=0; i<temp_array.length; i++) {
				if (temp_array[i].search("browse-") > -1) {
					idno = temp_array[i].replace(/browse-/gi, '');
					break;
				}
			}
		}
		
		return idno;
	}
	
	function calendarMove(oSDate, oDate) {
		var http = false;
					
		if(window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
		else if(window.XMLHttpRequest) http = new XMLHttpRequest();
		
		if(!http) return false;
		
		http.open('POST', '/includes/functions/blog_calendar.asp', true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		http.onreadystatechange = function() {
			if (http.readyState < 4)
				document.getElementById('DIVcalendar').innerHTML = '<img src="/assets/images/icon_loader.gif" align="middle" style="width:20px; height:20px; margin-right:8px; float:left;"> <div class="calLoad">Loading Calendar...</div>';
			else if (http.readyState == 4) {
				document.getElementById('DIVcalendar').innerHTML = http.responseText;
			}
		}
		
		http.send('actiontype=calmove&the_date='+oDate+'&start_date='+oSDate);
		return false;
	}
	
	function deleteCookie(oName, oPath, oDmn) {
		if (getCookie(oName)) document.cookie = oName+"="
			+ ((oPath)? ";path="+oPath:"")
			+ ((oDmn)? ";domain="+oDmn:"")
			+ ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}

	function getCookie(oName) {
		var dc = document.cookie;
		var prefix = oName+ "=";
		var begin = dc.indexOf("; " + prefix);
		
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else {
			begin += 2;
		}
		
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) end = dc.length;
		
		return unescape(dc.substring(begin + prefix.length, end));
	}

	function setCookie(oName, oVal, oExp, oPath, oDmn, oSec) {
		//oExp = receive/set by minutes;
		
		var today = new Date();
		today.setTime(today.getTime());
			if (oExp) oExp = oExp * 1000 * 60;
		
		var expires_date = new Date(today.getTime() + (oExp));
		
		document.cookie = oName+"="+escape(oVal)
			+ ((oExp)? ";expires="+expires_date.toGMTString():"")
			+ ((oPath)? ";path="+oPath:"" )
			+ ((oDmn)? ";domain="+oDmn:"")
			+ ((oSec)? ";secure":"");
	}

// ============================================================ FUNCTIONS: FORMS & DATE ============================================================
	function processKey(f) {
	    var keyvalue = window.event.keyCode;
	    if (keyvalue==13) f.submit();
	}
	
	function formError(oMsg, oField) {
		alert(oMsg);
		oField.focus();
	}
	
	function isDate(dateStr, field) {
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = dateStr.match(datePat);
		
		if (matchArray == null) {
			alert("Please enter a valid "+field+" Date as mm/dd/yyyy.");
			return false;
		}
		
		month = matchArray[1];
		day = matchArray[3];
		year = matchArray[5];
		
		if (month < 1 || month > 12) {
			alert("The "+field+" Date month, must be between 1 and 12.");
			return false;
		}
		if (day < 1 || day > 31) {
			alert("The "+field+" Date day, must be between 1 and 31.");
			return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			alert("The "+field+" Date month, "+month+" doesn`t have 31 days!")
			return false;
		}
		if (month == 2) {
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap)) {
				alert("The "+field+" Date February "+year+", doesn`t have "+day+" days!");
				return false;
			}
		}
		
		return true;
	}
	
	function dateAdd(intval, numb, oDate){
		switch(intval){
			case "M":
				base.setMonth(base.getMonth() + numb);
				break;
			case "YYYY":
				base.setFullYear(base.getFullYear() + numb);
				break;
			case "D":
				base.setDate(base.getDate() + numb);
				break;
			case "H":
				base.setHours(base.getHours() + numb);
				break;
			case "N":
				base.setMinutes(base.getMinutes() + numb);
				break;
			case "S":
				base.setSeconds(base.getSeconds() + numb);
				break;
			default:
		}
		return base
	}
	
	function valForm_contactus(oForm, oAction) {
		if (oForm.directo.options[oForm.directo.selectedIndex].value == "") {
			formError("Please select a Message Topic to continue.",oForm.directo);
			return;
		}
		if (oForm.fname.value == "") {
			formError("Please enter your First Name to continue.",oForm.fname);
			return;
		}
		if (oForm.lname.value == "") {
			formError("Please enter your Last Name to continue.",oForm.lname);
			return;
		}
		if (oForm.email.value.search("@") == -1 || oForm.email.value.search("[.*]") == -1){
			formError('Please enter a valid E-mail Address (for example: myemail@domain.com).', oForm.email);
			return;
		}
		if (oForm.phone.value != "") {
		 	if (isNaN(oForm.phone.value) || oForm.phone.value.search("[.*]") != -1 || oForm.phone.value.search("[-*]") != -1 || oForm.phone.value.length < 10) {
				formError("Please enter a valid Phone Number to continue (enter numbers only).",oForm.phone);
				return;
			}
		}
		if (oForm.subject.value == "") {
			formError("Please enter the Subject to continue.",oForm.subject);
			return;
		}
		if (oForm.msg.value == "") {
			formError("Please enter your Message to continue.",oForm.msg);
			return;
		}
		
		document.getElementById('DIVcontactBTN').style.display = 'none';
		document.getElementById('DIVcontactWait').style.display = 'block';
		
		oForm.actiontype.value = oAction;
		oForm.submit();
	}


// ============================================================ FUNCTIONS: COORIDNATES ============================================================	
	
	function get_XYcoordinates(element) {
		var useWindow = false;
		var coordinates = new Object();
		var x = 0;
		var y = 0;
		var use_gebi = false;
		var use_css = false;
		var use_layers = false;
		
		if (document.getElementById) use_gebi = true;
		else if (document.all) use_css = true;
		else if (document.layers) use_layers = true;
		
		if (use_gebi && document.all) {
			x = get_Xposition(document.all[element]);
			y = get_Yposition(document.all[element]);
		} else if (use_gebi) {
			x = get_Xposition(document.getElementById(element));
			y = get_Yposition(document.getElementById(element));
		} else if (use_css) {
			x = get_Xposition(document.all[element]);
			y = get_Yposition(document.all[element]);
		} else if (use_layers) {
			var found = 0;
			for (var i=0; i<document.anchors.length; i++) {
				if (document.anchors[i].name==element) {
					found = 1; 
					break;
				}
			}
	
			if (found == 0) {
				coordinates.x = 0;
				coordinates.y = 0;
				return coordinates;
			}
			
			x = document.anchors[i].x;
			y = document.anchors[i].y;
		} else {
			coordinates.x = 0;
			coordinates.y = 0;
			return coordinates;
		}
		
		coordinates.x = x;
		coordinates.y = y;
		return coordinates;
	}
	
	function get_Xposition (element) {
		var ol = element.offsetLeft;
		while ((element = element.offsetParent) != null) {
			ol += element.offsetLeft;
		}
		return ol;
	}
	
	function get_Yposition (element) {
		var ot = element.offsetTop;
		while((element = element.offsetParent) != null) {
			ot += element.offsetTop;
		}
		return ot;
	}
	
	function get_XYscrollPos() {
		var scrollX, scrollY;
      
		if (document.all) {
			if (!document.documentElement.scrollLeft) scrollX = document.body.scrollLeft;
			else scrollX = document.documentElement.scrollLeft;
		  
			if (!document.documentElement.scrollTop) scrollY = document.body.scrollTop;
			else scrollY = document.documentElement.scrollTop;
		} else {
			scrollX = window.pageXOffset;
			scrollY = window.pageYOffset;
		}
		
		var scroll = new Object();
		scroll.x = scrollX;
		scroll.y = scrollY;
		
		return scroll;
	}

// ============================================================ FUNCTIONS: BLOG CONTROLS ============================================================
	
	function IAcntrl_blogComments(oPos) {
		if (document.getElementById('IAcntrl_blogPostCount')) {
			for (var i=1;i<document.getElementById('IAcntrl_blogPostCount').value; i++) {
				if (document.getElementById('DIVblogComments'+i)) document.getElementById('DIVblogComments'+i).style.display = 'none';
			}
		}
		
		document.getElementById('DIVblogComments'+oPos).style.display = 'block';
		IAcntrl_blogReCaptcha('DIVreCaptcha'+oPos);
	}
	
	function IAcntrl_blogCommentsPost(oForm, oPos) {
		if (oForm.flname.value == "") {
			formError("Please enter your Name to continue.",oForm.flname);
			return;
		}
		if (oForm.email.value.search("@") == -1 || oForm.email.value.search("[.*]") == -1){
			formError('Please enter a valid E-mail Address (for example: myemail@domain.com).', oForm.email);
			return;
		}
		if (oForm.comments.value == "") {
			formError("Please enter your Comment to continue.",oForm.comments);
			return;
		}
		
		var http = false;
		var sendVars = getFormVars(oForm);
		
		document.getElementById('DIVblogComments'+oPos).innerHTML = '<img src="/assets/images/icon_loader.gif" align="absmiddle" style="margin-right:5px; width:16px; height:16px;" /> <strong>Processing...</strong>';
		
		if (window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest) http = new XMLHttpRequest();
		
		if (!http) { alert('Sorry, your Web Browser is not configured to work with this feature.'); return; }
		
		http.open("POST", oForm.action, true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				document.getElementById('DIVblogComments'+oPos).innerHTML = http.responseText;
				IAcntrl_blogReCaptcha('DIVreCaptcha'+oPos);
			}
		}
		
		http.send(sendVars);
	}
	
	function IAcntrl_blogReCaptcha(oEl) {
		Recaptcha.create('6LdhLgUAAAAAACKEVhxX069fDcpty41_hRER9tFf',oEl,{ theme: "red", callback: Recaptcha.focus_response_field });
	}

// ============================================================ FUNCTIONS: AJAX CONTROLS ============================================================

	function getFormVars(oForm)  {
		var sendVars = '';
		for(i=0; i<oForm.elements.length; i++)
			sendVars += oForm.elements[i].name+'='+escape(oForm.elements[i].value)+'&';
		
		return sendVars;
	}
	
	function loadAJAX(oEl, oCom, oParams) {
		document.getElementById(oEl).innerHTML = '<img src="/assets/images/icon_loader.gif" align="absmiddle" style="margin-right:5px; width:16px; height:16px;"> <strong>Processing...</strong>';
		
		var http = false;			
				
		if(window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest) http = new XMLHttpRequest();
		
		if (!http) { alert('Sorry, your Web Browser is not configured to work with this feature.'); return; }
		
		http.open("POST", oCom, true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		http.onreadystatechange = function() {
			if(http.readyState == 4) {
				if (http.responseText == 'refresh') location.href = location.href;
				else document.getElementById(oEl).innerHTML = http.responseText;
			}
		}
		
		http.send(oParams);
	}

	function returnAJAX(oCom, oParams, oFunc) {
		var returnVal;

		var http = false;
				
		if (window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest) http = new XMLHttpRequest();
		
		if (!http) { alert('Sorry, your Web Browser is not configured to work with this feature.'); return; }
		
		http.open("POST", oCom, true);
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		http.onreadystatechange = function() {
			if (http.readyState == 4) {			
				returnVal = http.responseText;
				if (oFunc) oFunc(returnVal);
				else return returnVal;
			}
		}
		
		http.send(oParams);
	}