//=====================================================================
//  USE THIS FILE FOR YOUR MAIN JS FUNTIONS. 
//
//  This could include pop up windows, any google javascript, etc. 
//=====================================================================

function submitForm(which) {
	which.submitted.value = 'yes';
	which.submit();
}

function popUp(url,width,height,scrollbars,resizable,querystring,windowname) {
	/*
	Ultimate Pop Up Script
	Simple Example <a href="javascript:void(0);" onclick="popUp('test.php')">Pop Up</a>
	Normal Example <a href="javascript:void(0);" onclick="popUp('test.php','200','300')">Test</a>
	Full Example   <a href="javascript:void(0);" onclick="popUp('test.php','200','300', 0, 1,'?cid=1', 'popup1')">Test 2</a>
	
	To use default just use null in t place of the variable
	  IE: <a href="javascript:void(0);" onclick="popUp('test.php','200','300', null, null,'?cid=1', 'popup1')">Test 2</a>
	  This uses the default variable for scrollbars and resizable
	*/ 
	if(typeof width == "undefined" || width == null)	var width	= 500;
	if(typeof height == "undefined" || height == null)	var height	= 400;
	
	if(typeof scrollbars == "undefined" || scrollbars == null)		var scrollbars	= 0;
	if(typeof resizable == "undefined" || resizable == null)		var resizable	= 0;
	if(typeof querystring == "undefined" || querystring == null)	var querystring	= '';
	if(typeof windowname == "windowname" || windowname == null)		var windowname	= 'popup';
	
	var full_url = url + querystring;

	var w;
	w = window.open(full_url, windowname, "width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+scrollbars+",resizable="+resizable);
	w.focus();
}

function swapListingImage(whichImage, image_div, image_resizer, width) {
	if (whichImage != '') {
		if(typeof image_resizer == "undefined" || width == null)	var image_resizer	= 'img.php';
		if(typeof width == "undefined" || width == null)			var width			= 200;
		
		document.getElementById(image_div).src = '/app/helpers/'+image_resizer+'?w='+width+'&constrain&img='+whichImage;
	}
}

function bookmarkSite(page_url, page_title) {
	/*
	Ultimate Bookmark/Add-to-Favorites Script
	Browsers Compatible: IE5+ Win, IE5 Mac, FF (Win/*nix), Netscape 6+, Opera 7+, Safari, Konqueror 3, iCab 3
	*/
	var user_agent	= navigator.userAgent.toLowerCase();
	var isKonq		= (user_agent.indexOf('konqueror') != -1);
	var isSafari	= (user_agent.indexOf('webkit') != -1);
	var isMac		= (user_agent.indexOf('mac') != -1);
	var buttonStr	= isMac ? 'Command/Cmd' : 'CTRL';

	if(window.external && (!document.createTextNode || (typeof(window.external.AddFavorite) == 'unknown'))) {
		// IE5+ Win
		window.external.AddFavorite(page_url, page_title);
	} else if (window.sidebar) {
		// FF Win
		window.sidebar.addPanel(page_title, page_url, "");
	} else if(isKonq) {
		// Konquerer
		alert('You need to press CTRL + B to bookmark our site.');
	} else if(window.opera) {
		// Opera (doesn't support bookmarking
		void(0);
	} else if(window.home || isSafari) {
		// FF *nix, Netscape, Safari, iCab
		alert('You need to press '+buttonStr+' + D to bookmark our site.');
	} else if(!window.print || isMac) {
		// IE Mac and Safari 1.0
		alert('You need to press Command/Cmd + D to bookmark our site.');    
	} else {
		alert('In order to bookmark this site you need to do so manually through your browser.');
	}
}

function uTimeout() {
	/*
	Ultimate Timeout Script
	--creating the timeout object--
	var myTimeout = new uTimeout()
	--standard--
	myTimeout.init(myFunction, 1000); // dont use the '()' on the function name..
	--repeating--
	myTimeout.init(myFunction, 1000, 3); // will repeat 3 times
	--passing arguments--
	myTimeout.init(myFunction, 1000, null, 'foo', 'bar', 1.6); // will call myFunction('foo', 'bar', 1.6)
	--cancelling the timeout--
	myTimeout.cancel();
	*/
    var _func				= null;		// function to call when the timeout expires
    var _timeout			= null;		// timeout length (milliseconds)
    var _repeat				= 0;		// number of times to repeat the function
    var _args				= [];		// array of arguments to pass to the function
    var _timeoutRunning		= false;	// flag to see if timeout is already running
    var _cancelled			= false;	// flag to see if timeout has been cancelled
	var _timeoutNum			= null;		// holder for the timeout
	
    this.init = function(func, timeout, repeat) {
        var i;
        
		if (_timeoutRunning || !func || !timeout)
			return false; // timeout exists, no function specified, or no timeout specified
        
        _func				= func;
        _timeout			= timeout;
        _timeoutRunning	= true;
        _cancelled			= false;
        _repeat				= repeat ? repeat : 0;
        
        _args				= [];
        for (i=3; i<arguments.length; i++) {
			// build the argument list
            _args[_args.length] = arguments[i];
        }
		
		// set the timeout!!
        _timeoutNum = setTimeout(_exec, _timeout);
    }

    this.cancel = function() {
		// cancel the timeout
		clearTimeout(_timeoutNum);
        _cancelled			= true;
        _timeoutRunning		= false;
        _repeat				= 0;
    }
    
    function _exec() {
        _timeoutRunning = false;
        if (_cancelled)
			return; // timeout was cancelled
			
        _func.apply(null, _args);
        
        if (_repeat > 0) {
			// the timeout is to be repeated!
            _repeat--;
            _timeoutNum = setTimeout(_exec, _timeout);
        }
    }
}

function isValidEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)  return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true;
}

function toggleDiv(anchorId, contentId, anchorShowContents, anchorHideContents) {
	if (document.getElementById(contentId).style.display == '') {
		document.getElementById(contentId).style.display = 'none';
		document.getElementById(anchorId).innerHTML = anchorShowContents;
	} else {
		document.getElementById(contentId).style.display = '';
		document.getElementById(anchorId).innerHTML = anchorHideContents;
	}
}
