/*

General javascript for gametracker

*/
var childwindow;
var xMOUSEPOS = 0; // Horizontal position of the mouse on the screen
var yMOUSEPOS = 0; // Vertical position of the mouse on the screen
var xMOUSEPOSMAX = 0; // Width of the page
var yMOUSEPOSMAX = 0; // Height of the page
var xWINDOWSIZE = 0;
var yWINDOWSIZE = 0;
var xDIV = 0;
var yDIV = 0;

/***********MOUSE POSITION**************************************/
function captureMousePosition(e) {
	if (document.layers) {
		xMOUSEPOS = e.pageX - xDIV;
		yMOUSEPOS = e.pageY - yDIV;
		xMOUSEPOSMAX = window.innerWidth+window.pageXOffset;
		yMOUSEPOSMAX = window.innerHeight+window.pageYOffset;
	} else if (document.all) {
		xMOUSEPOS = window.event.x+document.body.scrollLeft - xDIV;
		yMOUSEPOS = window.event.y+document.body.scrollTop - yDIV;
		xMOUSEPOSMAX = document.body.clientWidth+document.body.scrollLeft;
		yMOUSEPOSMAX = document.body.clientHeight+document.body.scrollTop;
	} else if (document.getElementById) {
		// Netscape 6 behaves the same as Netscape 4 in this regard
		xMOUSEPOS = e.pageX - xDIV;
		yMOUSEPOS = e.pageY - yDIV;
		xMOUSEPOSMAX = window.innerWidth+window.pageXOffset;
		yMOUSEPOSMAX = window.innerHeight+window.pageYOffset;
	}
}

function captureWindowSize() {
	if (window.innerHeight) {
		xWINDOWSIZE = window.outerWidth;
		yWINDOWSIZE = window.outerHeight;
		yDIV = 150;
		xDIV = (Math.abs(xWINDOWSIZE - 1024) / 2) + 150;
	} else if (document.body.clientHeight) {
		xWINDOWSIZE = document.body.clientWidth;
		yWINDOWSIZE = document.body.clientHeight;
		yDIV = -50;
		xDIV = -30;
	}
}

function cookieWindowSize() {
	captureWindowSize();
	setCookie('windowsize',xWINDOWSIZE,0,5);
}

function toggleMobile() {
	if (getCookie('nomobile')) {
		setCookieDomain('nomobile',1,-1,1);
	} else {
		setCookieDomain('nomobile',1,365,1);
	}
	document.location.href='http://www.hotoutdoors.net';
}

function addEvent (elem, type, eventHandle) {
    if (elem == null || elem == undefined) {
        return;
    }
    if ( elem.addEventListener ) {
        elem.addEventListener( type, eventHandle, false );
    } else if ( elem.attachEvent ) {
        elem.attachEvent( "on" + type, eventHandle );
    }
}

function submitLoginForm(form) {
	loginArr = new Array();
	loginArr['username'] = form.uname.value;
	loginArr['password'] = form.password.value;
	x_loginUser(form.uname.value,form.password.value,setLoginoutform);
	return false;
}

function set_language(lang) {
	setCookie('gt_lang',lang,365,1);
	window.location.reload();
}

function setLoginoutform(userLoggedin) {
	if (userLoggedin) {
		document.getElementById('loginform').style.visibility = 'hidden';
		document.getElementById('logoutform').style.visibility = '';
	}
}

function checkPromo() {
    var el = document.getElementById('promoid');
	setCookie('promoid',el.value,2,1);
	window.location.reload();
}

function setSkin(skin) {
	x_setSkin(skin,'');
	Sleep(.5);
	window.location.reload();
}

function setWP_Type(value) {
	var exdate=new Date();
	exdate.setSeconds(exdate.getSeconds()+5);
	document.cookie="wptype"+ "=" +escape(value)+ ";expires=" +exdate.toGMTString();
	window.location.reload();	
}

function Sleep(naptime){
	naptime = naptime * 1000;
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();
	while(sleeping){
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if(alarmMSeconds - startingMSeconds > naptime){ 
			sleeping = false; 
		}
	}
}

function searchSite(site,value) {
	window.open('http://www.google.com/search?q=site:' + site + ' ' + value);
	return false;
}

function memberInvite(sname,email,name,clr) {
	var snm = document.getElementById(sname)
	var em = document.getElementById(email);
	var nm = document.getElementById(name)
	x_inviteMember(snm.value,em.value,nm.value,function(retval) {
		if (retval && clr) {
            alert("Your message has been sent to " + nm.value);
			snm.value = '';
			em.value = '';
			nm.value = '';
		}
	});
}

function cookie_exists ( cookie_name ) {
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	if ( results ) {
		return true;
	} else {
    	return false;
	}
}

function saveBuyInfo() {
	var ret = false;
	x_setBuySession('');
	ret = confirm("Continue to purchase membership?");
	return ret;
}

function setCookieDomain(c_name,value,expiredays,expireminutes) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	exdate.setMinutes(exdate.getMinutes()+expireminutes);
	document.cookie=c_name+ "=" +escape(value)+ ';path=/;domain=hotoutdoors.net'+
		((expireminutes==null) ? "" : ";expires="+exdate.toGMTString());
}

function setCookie(c_name,value,expiredays,expireminutes) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	exdate.setMinutes(exdate.getMinutes()+expireminutes);
	document.cookie=c_name+ "=" +escape(value)+
		((expireminutes==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) { 
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function toggleCookie(id,cookie_name) {
	var obj_checked = document.getElementById(id).checked;
	if (obj_checked) {
		setCookie(cookie_name,1);
	} else {
		setCookie(cookie_name,1,-1,1);
	}
}

function addWaypointParent(id,parentid) {
	x_addParent(id,parentid,'');
}

function removeWaypointParent(id) {
	x_removeParent(id,'');
	Sleep(.5);
	document.location.reload();
}

function deleteWaypoint(id) {
    if (confirm('Are you sure you want to delete waypoint?')) {
	    x_deleteWaypoint(id,function(retval) {
	        document.location.reload();
        });
    }
}

function showPrivateUserDialog(value) {
	var console = document.getElementById('privUserDialog');
	if (value) {
		x_showPrivUserDialog( function(retval) {
			console.innerHTML = retval;
		});
		console.style.display = 'block';
	} else {
		console.innerHTML = "";
		console.style.display = 'none';
	}
}

function searchUser(value,results) {
	x_searchUser(value,function(retval) {
		var resObj = document.getElementById(results);
		resObj.innerHTML = "";
		for(var i in retval) {
			resObj.innerHTML += '<a href="javascript:addPrivUser(\'' + retval[i] + '\')">' + retval[i] + '</a><br/>';
		}
	});	
}

function searchGroupUser(value,gid,results) {
	if (value == "") {
		return;
	}
	x_searchUser(value,function(retval) {
		var resObj = document.getElementById(results);
		resObj.innerHTML = "";
		for(var i in retval) {
			resObj.innerHTML += '<form action="/groups.php?id='+gid+'" method="post"><input type="hidden" name="userid" value="'+retval[i]+'"/><div class="report-list-item"><span style="padding-top:5px;float:left;width:60px;">'+retval[i]+'</span><span style="width:40px;float:right"><input type="submit" name="add_user" value="Add"/></span></div></form>';
		}
	});	
}

function verifyDisband(group) {
	var accept = confirm('Are you sure you want to disband group ('+group+')?');
	return accept;
}

function addPrivUser(val) {
	document.getElementById('wpprivateusers').value += ',' + val;
}

function showUserImages(loc,img,value, id) {
	var tmp = document.getElementById(loc);
	if (value) {
		x_getUserImages(function(retval) {
			tmp.innerHTML = "";
			for(var i in retval) {
				tmp.innerHTML += '<img src="/image.php?id=' + retval[i] + '&amp;s=70" onclick="showUserImages(\''+loc+'\',\''+img+'\',false,'+ retval[i] +')" />';
			}
		});
		tmp.style.display = 'block';
	} else {
		tmp.style.display = 'none';
		if (id) {
			document.getElementById(img).value = id;
			document.getElementById(loc+'_si').innerHTML = '<img src="/image.php?id=' + id + '&amp;s=70" onclick="showUserImages(\''+loc+'\',\''+img+'\',true)"/>';
		} else {
			document.getElementById(loc+'_si').innerHTML = '<a href="javascript:showUserImages(\''+loc+'\',\''+img+'\',true)">Select</a>';
			document.getElementById(img).value = "";
		}
	}
}

function submitPageChange(direction) {
	var page = document.getElementById('page');
	if (direction == "prev") {
		if (page.value > 1) {
			page.value--;
		}
	} else {
		if (parseInt(page.value) < parseInt(document.getElementById('impages').value)) {
			page.value++;
		}
	}
	document.searchform.submit();
}

function openChild(file,window) {
	childWindow=open(file,window);
	if (childWindow.opener == null) childWindow.opener = self;
}


function latlngView(lat,lng) {
	posLat = convertDecimalToDMS(lat.value);
	posLng = convertDecimalToDMS(lng.value);
	lat.value = posLat[0] + "deg " + posLat[1] + "' " + posLat[2] + "\" ";
	lng.value = posLng[0] + "deg " + posLng[1] + "' " + posLng[2] + "\"";
	
}

function convertDecimalToDMS(pos) {
	var position = Array();
	if (pos <= 0) {
		pos = pos * -1;
	}
	var degLat = parseInt(pos);					 // use
	var decLat = pos - degLat;
	var dmnLat = 60 * decLat;
	var minLat = parseInt(dmnLat);		  // use
	var dscLat = dmnLat - minLat;
	var secLat = parseInt(60 * dscLat);	 // use
	position = Array(degLat,minLat,secLat);
	return position;
} 

function convertDMSToDecimal(Deg,Min,Sec) {
	var decimal;
	if (Deg < 0) {
		decimal = -1.0 * Deg + 1.0 * Min/60.0 + 1.0 * Sec/3600.0;
		return -1.0 * decimal;
   } else {
		decimal = 1.0 * Deg + 1.0 * Min/60.0 + 1.0 * Sec/3600.0;
		return decimal;
   }
}

function DegMinSecString(pnt) {
	var lat = pnt.lat();
	var lng = pnt.lng();
	var position = Array();
	var dirLat;
	var dirLng;
	if (lat > 0) {
			dirLat = "North";
	} else {
		dirLat = "South";
		lat = lat * -1;
	}
	if (lng > 0) {
		dirLng = "East";
	} else {
		dirLng = "West";
		lng = lng * -1;
	}

	var degLat = parseInt(lat);					 // use
	var degLng = parseInt(lng);					 // use
	var decLat = lat - degLat;
	var decLng = lng - degLng;
	var dmnLat = 60 * decLat;
	var dmnLng = 60 * decLng;
	var minLat = parseInt(dmnLat);		  // use
	var minLng = parseInt(dmnLng);		  // use
	var dscLat = dmnLat - minLat;
	var dscLng = dmnLng - minLng;
	var secLat = parseInt(60 * dscLat);	 // use
	var secLng = parseInt(60 * dscLng);	 // use
	var sDeg = "Latitude: " + degLat + "&#176; " + minLat + "' " + secLat + "\" " + dirLat + "<br>Longitude: " + degLng + "&#176; " + minLng + "' " + secLng + "\" " + dirLng;
	return sDeg;
}

function toggleMapOptions() {
	if (cookie_exists('gtUserLogin')) {
		var opt = document.getElementsByName('date_select_type');
		if (opt[0]) {
			if (opt[0].checked) {
				document.getElementById('wpdate_start').disabled = true;
				document.getElementById('wpdate_start_cal').href = 'javascript:void(0)';
				document.getElementById('wpdate_end').disabled = true;
				document.getElementById('wpdate_end_cal').href = 'javascript:void(0)';
				document.getElementById('date_select_season').disabled = true;
			} else if (opt[1].checked) {
				document.getElementById('wpdate_start').disabled = false;
				document.getElementById('wpdate_start_cal').href = "javascript:NewCal('wpdate_start','yyyymmdd',true,24)";
				document.getElementById('wpdate_end').disabled = false;
				document.getElementById('wpdate_end_cal').href = "javascript:NewCal('wpdate_end','yyyymmdd',true,24)";
				document.getElementById('date_select_season').disabled = true;
			} else if (opt[2].checked) {
				document.getElementById('wpdate_start').disabled = true;
				document.getElementById('wpdate_start_cal').href = 'javascript:void(0)';
				document.getElementById('wpdate_end').disabled = true;
				document.getElementById('wpdate_end_cal').href = 'javascript:void(0)';
				document.getElementById('date_select_season').disabled = false;
			} else if (opt[3].checked) {
				document.getElementById('wpdate_start').disabled = true;
				document.getElementById('wpdate_start_cal').href = 'javascript:void(0)';
				document.getElementById('wpdate_end').disabled = true;
				document.getElementById('wpdate_end_cal').href = 'javascript:void(0)';
				document.getElementById('date_select_season').disabled = true;
			} else {
				alert("Error in map Options occured - You should never see this.");
			}
		}
	}
}

function parseDateStr(datestring) {
    var Sp1=datestring.indexOf('-',0)
    var Sp2=datestring.indexOf('-',(parseInt(Sp1)+1));
    var tSp1=datestring.indexOf(":",0)
    var tSp2=datestring.indexOf(":",(parseInt(tSp1)+1));
    var Month=datestring.substring(Sp1+1,Sp2);
    var Day=datestring.substring(Sp2+1,Sp2+3);
    var Year=datestring.substring(0,Sp1);
    var Hour=datestring.substring(tSp1,(tSp1)-2);
    var Minute=datestring.substring(tSp1+1,tSp2);
    var Second=datestring.substring(tSp2+1,tSp2+3);
	return new Date(Year,Month - 1,Day,Hour,Minute,Second);
}

function clearLoginField() {
	var el = document.getElementById('uname');
	if (el.value == "User Name") {
		el.value = "";
		setStyle('uname', 'color', '#000');
	} else if (el.value == '') {
		el.value = "User Name";
		setStyle('uname', 'color', '#898989');
	}
}

function clearPassField() {
	var el = document.getElementById('password');
	if (el.value == "Password") {
		el.value = "";
		setStyle('password', 'color', '#000');
	} else if (el.value == '') {
		el.value = "Password";
		setStyle('password', 'color', '#898989');
	}
}

function replaceHtml(el, html) {
	var oldEl = typeof el === "string" ? document.getElementById(el) : el;
	/*@cc_on // Pure innerHTML is slightly faster in IE
		oldEl.innerHTML = html;
		return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
}

function getStyle(el, style) {
   if(!document.getElementById) return;
   
	 var value = el.style[toStyleCase(style)];
   
	if(!value)
		if(document.defaultView)
			value = document.defaultView.
				 getComputedStyle(el, "").getPropertyValue(style);
	   
		else if(el.currentStyle)
			value = el.currentStyle[toStyleCase(style)];
	 
	 return value;
}

function setStyle(objId, style, value) {
	document.getElementById(objId).style[style] = value;
}

function toStyleCase( sInput ) {
	var oStringList = sInput.split('-');
	if(oStringList.length == 1)   
		return oStringList[0];
	var ret = sInput.indexOf("-") == 0 ?
	   oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
	for(var i = 1, len = oStringList.length; i < len; i++){
		var s = oStringList[i];
		ret += s.charAt(0).toUpperCase() + s.substring(1)
	}
	return ret;
}

Array.prototype.inWPArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i].mWPid == value) {
			return true;
		}
	}
	return false;
};

Array.prototype.inPLArray = function (wpid,pwpid) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i].mWPid == wpid && this[i].mParentWPid == pwpid) {
			return true;
		}
	}
	return false;
};

Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] == value) {
			return true;
		}
	}
	return false;
};


