﻿var objTimer = new Object;
var g_currentImg = new Number;

window.onload = function() {
    prepLayout();
    var path = window.location.href.split('/')[4];
    if (path == '' || path == undefined) {
        navigate('home');
    } else {
        if (path.split('=').length > 1) {
            var pg = path.split('=')[1];
            navigate(pg);
        } else {
            initImage();
        }
    }
    
}
window.onresize = function() {prepLayout();}

function getCopy(copy){
	var thebody = document.getElementById('bodyCopyPanelM');	
	thebody.innerHTML = copy;
	prepLayout();
	initImage();
	fetchHours();
}

function changeGallery(){
	var content = document.getElementById('photosBody');
	var randomNum = Math.floor(Math.random() * (6 - 1 + 1) + 1);
	var randomNum2 = Math.floor(Math.random() * (6 - 1 + 1) + 1);
	var randomNum3 = Math.floor(Math.random() * (6 - 1 + 1) + 1);
	
	content.innerHTML = [
						 '<img src="images/photos/big/photo' + randomNum + '.jpg" alt="Picture 1" style="width:200px;height:133px;margin-bottom:10px" /><br />',
                         '<img src="images/photos/big/photo' + randomNum2 + '.jpg" alt="Picture 2" style="width:200px;height:133px;margin-bottom:10px" /><br />',
						 '<img src="images/photos/big/photo' + randomNum3 + '.jpg" alt="Picture 3" style="width:200px;height:133px;margin-bottom:10px" /><br />'
						 ].join('');
}

function prepLayout() {
    var container = document.getElementById('contentMain');
    var lgutter = document.getElementById('gutterLt');
    var rgutter = document.getElementById('gutterRt');
    
    if (!window.innerWidth) { //IE
        if (!(document.documentElement.clientWidth == 0)) {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        } else { //quirks mode
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
    } else { // W3C
        w = window.innerWidth;
        h = window.innerHeight;
    }

    var content = document.getElementById('pageBody');
    var layheight = content.offsetHeight;
    h = (layheight > h) ? layheight : h;
    
    container.style.height = h + 'px';
    lgutter.style.height = h + 'px';
    rgutter.style.height = h + 'px';
}

function bookmarkLink() {
    var title = 'Pasadena Golf Center';
    var url = 'http://www.pasadenagolfcenter.com/';
    
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite(url, title);
    } else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}

function navigate(path){
	var ifr = document.getElementById('ifr');
	ifr.src = 'admin_copy_fetch.php?pg=' + path;
}

//hours functions
function fetchHours() {
    var ifr = document.getElementById('ifr');
    ifr.src = 'admin_hours_fetch.php';
}

function getHours(text) {
    var container = document.getElementById('hoursText');
    container.innerHTML = text;
}

//contact us functions
	function isEmail(str) {
		var supported = 0;
		if (window.RegExp) {
			var tempStr = "a";
			var tempReg = new RegExp(tempStr);
			if (tempReg.test(tempStr)) supported = 1;
		}
		if (!supported) 
			return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
		var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
		var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
		return (!r1.test(str) && r2.test(str));
	}

	function register()	{
		var msg = '';
		var bErr = false;
		var f = document.contact;	
	
		if(!isEmail(f.email.value)){
			msg += " - valid e-mail address (yourname@yourdomain.com)\n";
			bErr = true;
		}
		if(f.comments.value==''){
			msg += "- Comments\n";
			bErr = true;
		}
		
		if(bErr){
			alert("Please provide the following information:\n\n" + msg);
		}else{
			document.getElementById('contact').submit();
		}
	}
	
	// image fader functions
	function initImage() {
		var i = 0
		while (i < 3){	
			var imageId = 'img' + (i + 1);
			var linkId = 'lnk' + (i + 1);
			var image = document.getElementById(imageId);
			var mylink = document.getElementById(linkId);
			var randomNum = Math.floor(Math.random() * (28 - 1 + 1) + 1);
			
			image.src = 'images/photos/big/photo' + randomNum + '.jpg';
			image.alt = 'Picture ' + randomNum;
			mylink.href = 'images/photos/big/photo' + randomNum + '.jpg';
			
			setOpacity(image, 0);
			image.style.visibility = 'visible';
			fadeIn(imageId,0);
			i++;
		}
	}
	
	function setOpacity(obj, opacity) {
		opacity = (opacity == 100)?99.999:opacity;
		obj.style.filter = "alpha(opacity:"+opacity+")"; // IE/Win
		obj.style.KHTMLOpacity = opacity/100;   // Safari<1.2, Konqueror
		obj.style.MozOpacity = opacity/100;  // Older Mozilla and Firefox
		obj.style.opacity = opacity/100;    // Safari 1.2, newer Firefox and Mozilla, CSS3
	}
	
	function fadeIn(objId,opacity) {
	  if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
		  setOpacity(obj, opacity);
		  opacity += 10;
		  window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}
	  }
	}

	//contact us save callback
	function contactSaved(){
		//alert('Your message was sent successfully.');	
		document.getElementById('bodyCopyPanelM').innerHTML = '<div class="pgTitleNew">Thank you for your inquiry.</div>  Your message has been sent successfully.';	
	}

	function contactDenied(err){
		switch(err){
			case 'captchafail':
			alert('Verification phrase incorrect.  Please try again.');
			document.getElementById('recaptcha_response_field').value = '';
			document.getElementById('recaptcha_response_field').focus();
			break;
		}
	}



	//mailing list functions
	function joinList(){
		window.location.href = 'join.php';	
	}

	function submitJoin(){
		var msg = '';
		var bErr = false;
		var f = document.joiner;	
		
		if(f.fname.value==''){
			msg += "- First Name\n";
			bErr = true;
		}
		if(f.lname.value==''){
			msg += "- Last Name\n";
			bErr = true;
		}
		if(!isEmail(f.email.value)){
			msg += " - valid e-mail address (yourname@yourdomain.com)\n";
			bErr = true;
		}

		
		if(bErr){
			alert("Please provide the following information:\n\n" + msg);
		}else{
			f.submit();
		}
	}
	
	function joinerSaved(){
		document.getElementById('bodyCopyPanelM').innerHTML = '<div class="pgTitleNew">Thank you for joining our E-mail Club.</div>  You should receive an e-mail shortly that contains your free coupon!  Please add <strong>contactus@pasadenagolfcenter.com</strong> to your safe senders list to ensure you receive the coupon.';	
	}
	
	function joinerDenied(err){
		switch(err){
			case 'existsfail':
			document.getElementById('bodyCopyPanelM').innerHTML = '<div class="pgTitleNew">We\'re Sorry.</div>  There was an error submitting your information.  <p style="color:#ff0000">Your e-mail address already exists in our system.</p> <p>If you believe you received this information in error, please contact us on (410) 439-GOLF.</p>';
			break;
			case 'captchafail':
			alert('Verification phrase incorrect.  Please try again.');
			document.getElementById('recaptcha_response_field').value = '';
			document.getElementById('recaptcha_response_field').focus();
			break;
		}
	}
	
	function unsubSaved(){
		document.getElementById('bodyCopyPanelM').innerHTML = '<div class="pgTitleNew">Unsubscribe Successful.</div>  You have been removed from our mailing list.';	
	}
	
	function unsubDenied(err){
		switch(err){
			case 'existsfail':
			document.getElementById('bodyCopyPanelM').innerHTML = '<div class="pgTitleNew">We\'re Sorry.</div>  There was an error during the unsubscribe process.  <p style="color:#ff0000">Your e-mail address does not exist in our system.</p> <p>If you believe you received this information in error, please contact us on (410) 439-GOLF.</p>';
			break;
			case 'captchafail':
			alert('Verification phrase incorrect.  Please try again.');
			document.getElementById('recaptcha_response_field').value = '';
			document.getElementById('recaptcha_response_field').focus();
			break;
		}
	}
	
	
	function couponOK(){
		var msg = document.getElementById('couponmsg');
		msg.innerHTML = 'The coupon number is valid.';
		setTimeout("document.getElementById('couponmsg').innerHTML = '';",5000);
	}
	
	function couponFail(err){
		var msg = document.getElementById('couponmsg');
		msg.innerHTML = 'This coupon number doesn\'t exist or it has already been used.';
		setTimeout("document.getElementById('couponmsg').innerHTML = '';",5000);
	}
		
		
