
//returns the GET argument when passed the key, will return false if not found
function getARG(ARGkey) {
	var URLarray = location.href.split('?');
	var ARGarray = URLarray[1].split('&');
	
	for(i=0;i<ARGarray.length;i++) {
		KeyVal = ARGarray[i].split('=');
		if(KeyVal[0]==ARGkey) return KeyVal[1];
	}
	return false;
}

function setCookie(cookieName,cookieVal) {
	nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear() + 1);
	document.cookie = cookieName+"="+cookieVal+"; expires="+nextyear.toGMTString()+"; path=/";
}

function getCookie(cookieName) {
	cookieArr = document.cookie.split(';');
	for(i=0;i<cookieArr.length;i++) {
		KeyVal = cookieArr[i].split('=');
		if((KeyVal[0]==cookieName)||(KeyVal[0]==" "+cookieName)) return KeyVal[1];
	}
	return false; //if not found
}


function checkText(theField,theForm) {
	var theString = document.forms[theForm].elements[theField].value;
	var theLength = document.forms[theForm].elements[theField].value.length;
	for (i = 0; i < theLength; i++) {
		s = theString.charCodeAt(i);
		if (((s >= 65) && (s <= 90)) || (s == 32) || ((s >= 97) && (s <= 122))) var isValid = 'yes';
		else {
			var isValid = 'no';			
			break;
			}		
		}
	if (isValid == 'no') {
		//alert('You have used numeric, special or accented characters.\nPlease re-enter value using alphabetic characters only.');
		document.forms[theForm].elements[theField].value = '';
		document.forms[theForm].elements[theField].focus();
		return false;
		}
	else return true;		
	}
	
// following function disallows accented or special entries into fields

function checkNumText(theField,theForm) {
	var theString = document.forms[theForm].elements[theField].value;
	var theLength = document.forms[theForm].elements[theField].value.length;
	for (i = 0; i < theLength; i++) {
		s = theString.charCodeAt(i);
		if (((s >= 65) && (s <= 90)) || ((s >= 48) && (s <= 57)) || (s == 32) || ((s >= 97) && (s <= 122))) var isValid = 'yes';
		else {
			var isValid = 'no';			
			break;
			}		
		}
	if (isValid == 'no') {
		//alert('You have used special or accented characters.\nPlease re-enter value using alphanumeric characters only.');
		document.forms[theForm].elements[theField].value = '';
		document.forms[theForm].elements[theField].focus();
		return false;
		}	
	else return true;	
	}		
		
// following function only allow numeric entry into certain fields

function checkNum(theField,theForm) {
	var s = document.forms[theForm].elements[theField].value;
	if(s=='.') ;
	else if (isNaN(s)) {
		document.forms[theForm].elements[theField].focus();
		if (isNaN(parseInt(s))) 
			document.forms[theForm].elements[theField].value = '';
		else	
			document.forms[theForm].elements[theField].value = parseInt(s);
		}
	}

//following function creates new popup window

function goPopupExt(myUrl,myWidth,myHeight, scrollbars) {
	//now = new Date();
	popup = window.open(myUrl,'thenewWin','top=50,left=50,toolbar=no,location=no,directories=no,status=no,resizable=yes,copyhistory=no,width=' + myWidth + ',height=' + myHeight + ',scrollbars=' + scrollbars);
	popup.focus();
	}

function goPopupExt(myUrl,myWidth,myHeight, scrollbars,winName) {
	//now = new Date();
	popup = window.open(myUrl,winName,'top=50,left=50,toolbar=no,location=no,directories=no,status=no,resizable=yes,copyhistory=no,width=' + myWidth + ',height=' + myHeight + ',scrollbars=' + scrollbars);
	popup.focus();
	}
	

function swapImages(imName,imSrc) {
	document.images[imName].src = imSrc;
}




//DETERMINE X,Y COORDINATES of mouse

var xpos, ypos;


// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

jQuery(document).ready(function(){
   $(document).mousemove(function(e){
	   tempX = e.pageX;
	   tempY = e.pageY;
   }); 
})


//get scroll offset
var xOffset = 0;
var yOffset = 0;

function setScrollOffset() {
	if(navigator.userAgent.search(/MSIE/)==-1) {
		return;
	}
	if (self.pageYOffset) // all except Explorer
	{
		xOffset = self.pageXOffset;
		yOffset = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		xOffset = document.documentElement.scrollLeft;
		yOffset = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		xOffset = document.body.scrollLeft;
		yOffset = document.body.scrollTop;
	}
}



function copyToClipboard(s)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", s);
	}
	else
	{
		// You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
	   if (!clip) return;
	   
	   // create a transferable
	   var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
	   if (!trans) return;
	   
	   // specify the data we wish to handle. Plaintext in this case.
	   trans.addDataFlavor('text/unicode');
	   
	   // To get the data from the transferable we need two new objects
	   var str = new Object();
	   var len = new Object();
	   
	   var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
	   
	   var copytext=meintext;
	   
	   str.data=copytext;
	   
	   trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
	   
	   var clipid=Components.interfaces.nsIClipboard;
	   
	   if (!clip) return false;
	   
	   clip.setData(trans,null,clipid.kGlobalClipboard);	   
	}
}

var nav_timer = false;

//menu navigation
$(function() {
	$('#sub_rentals').hide();
	$('#nav_rentals,#sub_rentals').mouseover(function() {
		if(nav_timer) clearTimeout(nav_timer);
	  $('#sub_rentals').show();
	});
	
	$('#nav_rentals,#sub_rentals').mouseout(function() {
	  nav_timer = setTimeout("hideNav()",100);
	});
	

		
});

function hideNav() {
	$('#sub_rentals').fadeOut('fast');
}



var subnavitems = new Array();
var subnavitemcounter = 0;
var subnavTimer;

function ShowSubMenu() {
	if(arguments.length>0) clearTimeout(subnavTimer);
	for(i=0;i<subnavitems.length;i++) {
		visibility="hidden";
		$('#nav_'+subnavitems[i]).removeClass('hoverit');
		for(j=0;j<arguments.length;j++) {
			if(arguments[j]==subnavitems[i]) {
				visibility="visible";
				$('#nav_'+arguments[j]).addClass('hoverit');
			}
		}
		document.getElementById('submenu_'+subnavitems[i]).style.visibility=visibility;
	}	
}

function HideSubMenu() {
	subnavTimer = setTimeout("ShowSubMenu()",500);
}

/*jquery extensions*/

/**
 *	iFlip
 *	Author: keta
 *
 *	Simple plugin to switch images.
 *	@ a (str) - starting image
 *	@ b (str) - image to switch to on click.
 */

jQuery.fn.iflip = function( a, b ) {
	
	
		curr = $(this).attr('src');
	
		if( curr == a ) {
			$(this).attr('src', b);
		} else if( curr == b ) {
			$(this).attr('src', a);
		}	
}

//COMMON FUNCTIONS


function hideElement(elName) {
	$('#'+elName).hide();
}

function showElement(elName) {
	// redundancy
	$('#'+elName).removeClass('hidden');
	$('#'+elName).show();
}

function toggleDisplay() {
	elName = arguments[0];
	$('#'+elName).slideToggle(400);
	if(arguments.length>1) {
		for(i=1;i<arguments.length;i++) $('#'+arguments[i]).slideUp(400);
	}
}


var hideTimer;

function timerShow(elName) {
	clearTimeout(hideTimer);
	showElement(elName);
}

function timerHide(elName) {
	hideTimer = setTimeout("hideElement('"+elName+"')",500);
}

//CART FUNCTIONS
function loadCart() {
	$.get("/ajax/loadCart.php", function(data){
		eval(data);
	});	
}

function addToCart(prod_id,quantity) {
	$.get("/ajax/addToCart.php?prod_id="+prod_id+"&quantity="+quantity, function(data){
		eval(data);
	});	
}

function addSpecialToCart(special_id) {
	$.get("/ajax/addSpecialToCart.php?special_id="+special_id, function(data){
		eval(data);
	});
}

function addToOrder(prod_id,quantity) {
	$.get("/ajax/addToCart.php?prod_id="+prod_id+"&quantity="+quantity, function(data){
		//nothing
	});
}

function updateQuantity(order_item_id,quantity) {
	$.get('ajax/updateQuantity.php?order_item_id='+order_item_id+'&quantity='+quantity, function(data) {
		eval(data);
	});
}

//PRODUCT PAGE

function checkAvailability(prod_id) {
				
	startDate = document.check_availability.startDate.value;
	dropOffTime = document.check_availability.dropOffTime.options[document.check_availability.dropOffTime.selectedIndex].value;
	pickUpTime = document.check_availability.pickUpTime.options[document.check_availability.pickUpTime.selectedIndex].value;
	extraDays = document.check_availability.extraDays.options[document.check_availability.extraDays.selectedIndex].value;
	cityState = document.check_availability.cityState.value;
	zipCode = document.check_availability.zipCode.value;
	
	$.get("/ajax/checkAvailability.php?startDate="+startDate+"&dropOffTime="+dropOffTime+"&pickUpTime="+pickUpTime+"&extraDays="+extraDays+"&cityState="+cityState+"&zipCode="+zipCode+"&prod_id="+prod_id, function(data) {
		$('#check-availability-results').html(data);
	});
	$('#check-availability-form').hide();
	$('#check-availability-results').html("<div style='font-size:16px'><b>Checking Availability...</b></div>");
	$('#check-availability-results').show();		
}

function showAvailabilityForm() {
	$('#check-availability-results').hide();	
	$('#check-availability-form').show();	
}

function checkProductStatus(prod_id) {
	$.get("/ajax/checkProductStatus.php?prod_id="+prod_id, function(data){
		eval(data);
		loadCart();
	});	
}



function findCityByZip(theZip,mode) {
	if(theZip.length==5) {
		$.get("/ajax/getCitiesByZip.php?zip="+theZip+"&mode="+mode, function(data) {
			eval(data);
		});	
	}	
}


function getWeather(zip) {
	if($('#weather_forecast').html()!='') {
		$('#weather_forecast').slideToggle('400');	
		return;
	}
	$.ajax({
	  url: '/ajax/officepad/weatherForecast.php?zip='+zip,
	  success: function(data) {
	    $('#weather_forecast').html(data);
	    $('#weather_forecast').slideToggle('400');
	  }
	});	
}

