var pingid = '', host = window.location.hostname, domain = (host.search(/(www\.dvdesign\.com\.au|dvdesign\.com\.au)/i) != -1) ? 'http://www.dvdesign.com.au/yvw/turf-exemption/' : ((document.location.protocol == 'https:') ? 'https://www.warmseasongrass.com.au/' : 'http://www.warmseasongrass.com.au/'), ajaxurl = domain+'sendtofriend.php';
$(document).ready(function() {
	$('#page-title a').hover(function(){ $(this).find('img').attr('src', domain+'interface/warm-season-grass-over.gif'); }, function(){ $(this).find('img').attr('src', domain+'interface/warm-season-grass.gif'); });
	pingid = generatePing();
	sendPing();
});
function printPage() {
	window.print();
}
function emailPage() {
	if ($('#email-popup').length > 0) {
		$('#email-popup').dialog('destroy');
		$('#email-popup').remove();
	}
	$('#epage').append($('<div/>').attr('id', 'email-popup').css('display', 'none').append($('<div/>').attr('id', 'email-input').html(emailPageHtml())));
	$('#email-popup').dialog({ modal:true, title:'Email link to this page', width:400, dialogClass:'emailPage', close:function(){ $('#email-popup').dialog('destroy'); $('#email-popup').remove(); } });
}
function emailPageHtml() {
	return '<div class="row note">To send an email with a link to this page simply follow the steps below.</div><div class="row nb">1. Enter the name and email address of the person to send to.</div><div class="row"><label for="friend-name">To name</label><input type="text" name="friend-name" id="friend-name" value="" size="30" class="medium" maxlength="250" /></div><div class="row"><label for="friend-email">To email</label><input type="text" name="friend-email" id="friend-email" value="" size="30" class="medium" maxlength="250" /></div><div class="row nb">2. Enter your name and email. This wiil be used to let the recipient know who sent them the email.</div><div class="row"><label for="your-name">Your name</label><input type="text" name="your-name" id="your-name" value="" size="30" class="medium" maxlength="250" /></div><div class="row"><label for="your-email">Your email</label><input type="text" name="your-email" id="your-email" value="" size="30" class="medium" maxlength="250" /></div><div class="row nb">3. Include a personalised message in the email (optional).</div><div class="row"><label for="your-msg">Message</label><textarea name="your-msg" id="your-msg" cols="30" rows="4" class="medium"></textarea></div><div class="row btn"><input type="button" value="Close" onclick="$(\'#email-popup\').dialog(\'close\')" /> <input type="button" value="Send email" onclick="sendEmailPage()" /></div>';
}
function sendEmailPage() {
	if ($('#friend-name').val() == '') {
		alert('Please enter a \'To\' name');
		$('#friend-name').focus();
	}
	else if ($('#friend-email').val() == '') {
		alert('Please enter a \'To\' email address');
		$('#friend-email').focus();
	}
	else if ($('#your-name').val() == '') {
		alert('Please enter your name');
		$('#your-name').focus();
	}
	else if ($('#your-email').val() == '') {
		alert('Please enter your email');
		$('#your-email').focus();
	}
	else {
		$.ajax({
			type: "POST",
			url: ajaxurl,
			data: 'friend-name='+$('#friend-name').val() + '&friend-email=' + $('#friend-email').val() + '&your-name=' + $('#your-name').val() + '&your-email=' + $('#your-email').val() + '&your-msg=' + $('#your-msg').val() + '&pingid=' + pingid + '&page-url=' + self.location.href,
			success: function(msg) {
				$('#email-input').html(msg);
				setTimeout(function(){ 
					$('#email-popup').dialog('close');
				}, 3000);
			},
			error: function(req, stat, err) {
				$('#email-input').html(err);
				setTimeout(function(){ 
					$('#email-popup').dialog('close');
				}, 3000);
			}
		 });
	}
}
function generatePing() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var len = 12;
	var str = '';
	for (var i=0; i<len; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		str += chars.substring(rnum,rnum+1);
	}
	return str;
}
function sendPing() {
	/*
	$.ajax({
		type: "POST",
		url: ajaxurl,
		data: "ping="+pingid
	});
	*/
}
function openTerms() {
	window.open('terms-and-conditions.html', 'terms', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=520,width=740');
}
function fontsizeup() {
  var active = getActiveStyleSheet();
  switch (active) {
    case 'A-': 
      setActiveStyleSheet('A');
      break;
    case 'A': 
      setActiveStyleSheet('A+');
      break;
    case 'A+':
			setActiveStyleSheet('A++');
			break;
		case 'A++':
			break;
    default:
      setActiveStyleSheet('A++');
      break;
  }
}
function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'A++': 
      setActiveStyleSheet('A+');
      break;
    case 'A+': 
      setActiveStyleSheet('A');
      break;
    case 'A': 
      setActiveStyleSheet('A-');
      break;
    case 'A-':
      break;
    default:
      setActiveStyleSheet('A-');
      break;
  }
}
function fontsizereset() {
	setActiveStyleSheet(getPreferredStyleSheet());
}
function setActiveStyleSheet(title, w) {
	var i, a;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if (a.getAttribute("title") == title) {
				a.disabled = false;
			}
    }
  }
}
function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}
function getPreferredStyleSheet() {
  return ('A');
}
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}
function deleteCookie(name) {
	var yd = new Date();
	yd.setTime(yd.getTime()-(1*24*60*60*1000));
	document.cookie = name+"="+(yd.toGMTString())+"; path=/";
}
window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}
window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}
