
function truncate_form_element_value(element, length, indicator) {
  if ($(element).value.length > length) {
    $(element).value = $(element).value.truncate(length);
    alert('Sorry, that was over the maximum ' + length + ' characters');
  }
  remaining = length - $(element).value.length;
  $(indicator).innerHTML = remaining + ' of ' + length + ' characters remaining';
}

function update_preview_field(form_element, preview_field) {
  $(preview_field).innerHTML = $(form_element).value.gsub(/\n/, '<br />');
}

function switch_composition_form_to_sms(){
  ['MessageSubject', 'reply_to_mms', 'reply_to_wap', 'CardAttachment', 'FileAttachment', 'delivery_method', 'file_delivery_method',
    'preview_message_gc_url', 'preview_message_file_url', 'preview_subject', 'preview_card'].each(Element.hide);
  ['reply_to_sms'].each(Element.show);
  set_message_type_radios('sms_radio');
  truncate_sms_message();
  }


function switch_composition_form_to_card(){
  ['reply_to_sms', 'reply_to_wap', 'FileAttachment', 'file_delivery_method', 'preview_message_file_url'].each(Element.hide);
  ['MessageSubject', 'CardAttachment', 'reply_to_mms', 'delivery_method'].each(Element.show);
  if ($('gdm_mms').checked) {
    ['reply_to_sms', 'reply_to_wap', 'preview_message_gc_url'].each(Element.hide);
    ['reply_to_mms', 'preview_subject', 'preview_card'].each(Element.show);}
  if ($('gdm_wap').checked) {
    ['reply_to_mms', 'reply_to_sms', 'preview_subject', 'preview_card','MessageSubject'].each(Element.hide);
    ['reply_to_wap', 'preview_message_gc_url'].each(Element.show);}
  if ($('gdm_sms').checked) {
    ['reply_to_mms', 'reply_to_wap', 'preview_subject', 'preview_card','MessageSubject'].each(Element.hide);
    ['reply_to_sms', 'preview_message_gc_url'].each(Element.show);}
  set_message_type_radios('mms_radio');
  truncate_sms_message();  
}

function switch_composition_form_to_file(){
  ['MessageSubject', 'reply_to_sms', 'reply_to_mms', 'delivery_method', 'CardAttachment', 'preview_message_gc_url', 'preview_subject',
    'preview_card'].each(Element.hide);
  ['FileAttachment', 'reply_to_wap', 'file_delivery_method'].each(Element.show);
  if ($('fdm_wap').checked) {
    ['reply_to_sms', 'reply_to_mms', 'preview_message_file_url'].each(Element.hide);
    ['reply_to_wap'].each(Element.show);}
  else {
    ['reply_to_mms', 'reply_to_wap'].each(Element.hide);
    ['reply_to_sms', 'preview_message_file_url'].each(Element.show);}
  set_message_type_radios('wap_radio');
  truncate_sms_message();  
  }

function set_message_type_radios(selected_radio){
  if (!$(selected_radio).checked){
    switch(selected_radio){
      case 'sms_radio':
        $('sms_radio').checked = true;
        $('mms_radio').checked = false;
        $('wap_radio').checked = false;
        break;
      case 'mms_radio':
        $('sms_radio').checked = false;
        $('mms_radio').checked = true;
        $('wap_radio').checked = false;
        break;
      case 'wap_radio':
        $('sms_radio').checked = false;
        $('mms_radio').checked = false;
        $('wap_radio').checked = true;
        break;
    }
  }
}
function change_delivery_method_to_mms(){
  ['reply_to_sms', 'reply_to_wap', 'preview_message_gc_url'].each(Element.hide);
  ['MessageSubject', 'reply_to_mms', 'preview_subject', 'preview_card', 'file_size_warning'].each(Element.show);
  truncate_sms_message();
}

function change_delivery_method_to_sms(){
  ['MessageSubject', 'reply_to_mms', 'reply_to_wap', 'preview_subject', 'preview_card', 'file_size_warning'].each(Element.hide);
  ['reply_to_sms', 'preview_message_gc_url'].each(Element.show);
  truncate_sms_message();
}

function change_delivery_method_to_wap(){
  ['preview_message_gc_url', 'MessageSubject', 'reply_to_sms', 'reply_to_mms', 'preview_subject', 'file_size_warning', 'preview_card'].each(Element.hide);
  ['reply_to_wap'].each(Element.show);
  truncate_sms_message();  
}

function change_file_delivery_method_to_wap(){
  ['MessageSubject', 'reply_to_sms', 'reply_to_mms', 'preview_message_file_url', 'preview_subject'].each(Element.hide);
  ['reply_to_wap'].each(Element.show);
  truncate_sms_message();  
}

function change_file_delivery_method_to_sms(){
  ['MessageSubject', 'reply_to_mms', 'reply_to_wap', 'preview_subject'].each(Element.hide);
  ['reply_to_sms', 'preview_message_file_url'].each(Element.show);
  truncate_sms_message();  
}

function getImgSize(imgSrc){
	var newImg = new Image();
	newImg.src = imgSrc;
	var height = newImg.height;
	var width = newImg.width;
	return width;
	if (width > 176) {
	      alert ('The image size is '+width+'*'+height);
	    }
}

function radioCheck(me,group) { 
  var checked = me.checked; 
  if (checked) for (var i = 1; i < arguments.length; i++) { 
  var ck = document.getElementById(arguments[i]); 
  if (ck) ck.checked = false; 
} 
me.checked = checked; // checkbox action 
//me.checked = true; // radiobox action 
}

function findDOM(objectId) {
  if (document.getElementById) {
    return (document.getElementById(objectId));}
  if (document.all) {
    return (document.all[objectId]);}
}

function zoom(type,imgx) {
  imgd = findDOM(imgx);
  if (type=="+" && imgd.width < 240) {
    imgd.width = 240;
    imgd.style.margin = "-32px 0 0 -32px";
    }
  if (type=="-" && imgd.width > 20) {
    imgd.width = 176;
    imgd.style.margin = "0";
  }
}

function ShowSelectedDeliveryInfo() {  
  selector = document.website_download.card_delivery_method
  len = selector.length
  i = 0
  chosen = "none"

  for (i = 0; i < len; i++) {
    if (selector[i].selected) {
    chosen = selector[i].value
    } 
  if (!(selector[i].selected)) {
    Element.hide(selector[i].value+'_info')
    } 
  }
  Element.show(chosen+'_info')
}


function truncate_sms_message() {
  var ele = document.getElementById('mail_body'); 
  var area = document.getElementById('contact_form_indicator'); 
  
  if ($('mms_radio').checked && $('gdm_mms').checked) {
    area.innerHTML = ""; }
  else {    
    var sig_size = get_signature_and_url_size()
    var total = ele.value.length + sig_size;
    var net_message_size = 160 - sig_size;
    var remaining = 160 - total;
    if (total > 160) {
      ele.value = ele.value.truncate(net_message_size); 
      alert('Sorry, that was over the maximum ' + 160 + ' characters'); }
    area.innerHTML = '<b>' + total + '</b> characters used (max: ' + 160 + ')'; }
}


// gets the additional character length that will be added to the body of the text message for SMS or WAP messages 
// If there's a file or Card, add the length of that URL etc.

function get_signature_and_url_size() {
  if ($('signature_select').checked) {
    var signature_length = parseFloat($('sig_length').value); }
  else {
    var signature_length = 0; }

  if ($('card_url_length')) {
    var url_length = parseFloat($('card_url_length').value); }
  else {
    var url_length = 0; }

  if ($('file_url_length')) {
    var file_url_length = parseFloat($('file_url_length').value); }
  else {
    var file_url_length = 0; }

  if ($('sms_radio').checked) {
     return signature_length }

  if ($('mms_radio').checked) {
    if ($('gdm_sms').checked) {
       return signature_length + url_length }       
    if ($('gdm_wap').checked) {
      return signature_length } }

  if ($('wap_radio').checked) {
    if ($('fdm_sms').checked) {
       return signature_length + file_url_length }       
    else {
      return signature_length } }   
}

function mouseover(element, image) { 
  document.getElementById(element).style.background='url(/images/' + image + ')'; 
} 

function mouseout(element, image) { 
  document.getElementById(element).style.background='url(/images/' + image + ')'; 
} 

function get_window_size() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
    myWidth = document.body.clientWidth;    
  }
    return [myWidth, myHeight]
}

function set_div_height() {
  var browser_height = get_window_size()[1];
  // var browser_width = get_window_size()[0];
  var header_height = document.getElementById('header_members').offsetHeight;
  var sb_bottom_height = document.getElementById('sb_bottom').offsetHeight;
  var sb_top_height = browser_height - header_height - sb_bottom_height;
  var content_height = browser_height - header_height;
  document.getElementById('sb_top').style.height = sb_top_height + 'px'; 
  document.getElementById('content_area').style.height = content_height + 'px'; 
  // window.alert( 'browser= ' + browser_height  + ', sb_top_height =' + sb_top_height + ', content_height =' + content_height  + ', browser width =' + browser_width);
}

function set_popup_height() {
  var browser_height = get_window_size()[1];
  var header_height = document.getElementById('header_members').offsetHeight;
  var content_height = browser_height - header_height;
  document.getElementById('content').style.height = content_height + 'px'; 
}

// 1st, increaes #content to push the footer to fill page
// 2nd, resizes #side_bar to match #content
// problems: menu is part of content_area, but may be only above content on some layouts
// one layout will have equal content + side_bar sizes, another the side_bar will be taller by the height of the menu. Hence menu*2 in the calculation

// function set_website_columns_height() {
//   var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
//   var window_height = get_window_size()[1];
//   var page_layout = document.getElementById('page_layout').offsetHeight;
//   var header = document.getElementById('header').offsetHeight;
//   var menu = document.getElementById('menu').offsetHeight;
//   var footer = document.getElementById('footer').offsetHeight;
//   if (window_height > page_layout+14) {
//     document.getElementById('content').style.minHeight = window_height-header-menu*2-footer-38 + 'px'; 
//     if (IE6) {
//     document.getElementById('content').style.height = window_height-header-menu*2-footer-38 + 'px'; }
//   };
//   window.alert('window= ' + window_height + ', page_layout= ' + page_layout + ', header= ' + header  + ', menu= ' + menu + ', footer= ' + footer  );
//   // window.alert('page_layout= ' + document.getElementById('page_layout').offsetHeight + '  content= ' + document.getElementById('content').offsetHeight );
//   
//   if (document.getElementById('side_bar') != null){
//     var content_area = document.getElementById('content_area').offsetHeight;
//     var side_bar = document.getElementById('side_bar').offsetHeight;
//     document.getElementById('side_bar').style.minHeight = content_area - menu + footer  + 'px'; 
//     if (IE6) {
//       document.getElementById('side_bar').style.height = content_area + 'px'; }
//   };
// 
// }


function set_website_columns_height() {
  
  var browser_type=BrowserDetect.browser
  var browser_version=BrowserDetect.version
  // window.alert('browser_type= ' + browser_type +' version= ' + browser_version + domain );
  
 
  
  var content = document.getElementById('content').offsetHeight;
  var side_bar = document.getElementById('side_bar').offsetHeight;
  if (content > side_bar) {
    // window.alert('side_bar= ' + side_bar + ', content= ' + content );
    document.getElementById('side_bar').style.minHeight = content + 'px'; 
    if  (browser_type=="Explorer"&&browser_version<7) { 
      document.getElementById('side_bar').style.height = content + 25 + 'px'; }
  };
}



function go_anchor(n){
  document.getElementById("content").scrollTop = document.getElementById(n).offsetTop
}



function switch_value(element){
  var el = document.getElementById(element);
  if (el.value == 0) {
    el.value = 1;
    // window.alert('changed to 1'); 
    }
  else {
    el.value = 0;
    // window.alert('changed to 0'); 
  }
}





var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
    // {
    //  string: navigator.userAgent,
    //  subString: "Firefox",
    //  identity: "Firefox"
    // },
    // {
    //  string: navigator.vendor,
    //  subString: "Camino",
    //  identity: "Camino"
    // },
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();





// Drop Down Menu

var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// close layer when click-out
document.onclick = mclose;


// Input Focus Changes - for login box & Search fields


// Variable to save prompt messages
var fieldPrompts = new Array();

function inputFocus(fieldObj) {
if (fieldObj.className=='inputPrompt') {
if (fieldObj.id=='user_hashed_password') {
//change field type and reset obj reference
changeInputType('user_hashed_password', 'password');
fieldObj = document.getElementById('user_hashed_password');
}
fieldPrompts[fieldObj.id] = fieldObj.value;
fieldObj.value = '';
fieldObj.className = 'inputStd';
fieldObj.select();
fieldObj.focus();
}
}

function inputBlur(fieldObj) {
if (fieldObj.value=='') {
if (fieldObj.id=='user_hashed_password') {
//change field type and reset obj reference
changeInputType('user_hashed_password', 'text');
fieldObj = document.getElementById('user_hashed_password');
}
fieldObj.value = fieldPrompts[fieldObj.id];
fieldObj.className = 'inputPrompt';
}
}

function changeInputType(objID, oType) {
var oldObject = document.getElementById(objID);
var newObject = document.createElement('input');
newObject.type = oType;
if(oldObject.value) newObject.value = oldObject.value;
if(oldObject.size) newObject.size = oldObject.size;
if(oldObject.name) newObject.name = oldObject.name;
if(oldObject.id) newObject.id = oldObject.id;
if(oldObject.onfocus) newObject.onfocus = oldObject.onfocus;
if(oldObject.onblur) newObject.onblur = oldObject.onblur;
if(oldObject.className) newObject.className = oldObject.className;
oldObject.parentNode.replaceChild(newObject,oldObject);
return;
}

function setPrompts() { //Run onload of the page
document.getElementById('user_user_name').className = 'inputPrompt';
// document.getElementById('altfield').className = 'inputPrompt';

changeInputType('user_hashed_password', 'text');
document.getElementById('user_hashed_password').value = 'password';
document.getElementById('user_hashed_password').className = 'inputPrompt';
}


function CheckBrowser(){
  var domain=document.domain
  var browser_type=BrowserDetect.browser
  var browser_version=BrowserDetect.version
  // window.alert('browser_type= ' + browser_type +' version= ' + browser_version + domain );
  if ((browser_type=="Safari"&&browser_version<520) | 
      (browser_type=="Explorer"&&browser_version<7) | 
      (browser_type=="Mozilla"&&browser_version<1.8) |
      (browser_type=="Opera"&&browser_version<9) )
    window.location.replace("https://"+domain+"/members/update_browser")
  // else if (browser_type=="Explorer"&&browser_version<7)
  //  window.location.replace("https://192.168.1.2/members/update_browser")
  // else
  // window.location="https://192.168.1.2/members/login/"
}




function setSearch() { //Run onload of the page
document.getElementById('big_search').className = 'search_prompt';
}

function searchFocus(fieldObj) {
if (fieldObj.className=='search_prompt') {
fieldPrompts[fieldObj.id] = fieldObj.value;
fieldObj.value = '';
fieldObj.className = 'search_input';
fieldObj.select();
fieldObj.focus();
}
}



function searchBlur(fieldObj) {
if (fieldObj.value=='') {
fieldObj.value = fieldPrompts[fieldObj.id];
fieldObj.className = 'search_prompt';
}
}



function toggleEditor(id) {
  if (!tinyMCE.get(id))
    tinyMCE.execCommand('mceAddControl', false, id);
  else
    tinyMCE.execCommand('mceRemoveControl', false, id);
  }


function data_copy()
  {
  if (document.add_account_form.copy[0].checked){
  document.add_account_form.account_contact_details_first_name.value=document.add_account_form.contact_details_first_name.value;
  document.add_account_form.account_contact_details_last_name.value=document.add_account_form.contact_details_last_name.value;
  document.add_account_form.account_contact_details_mobile.value=document.add_account_form.contact_details_mobile.value;
  document.add_account_form.account_contact_details_phone.value=document.add_account_form.contact_details_phone.value;
  document.add_account_form.account_contact_details_fax.value=document.add_account_form.contact_details_fax.value;
  document.add_account_form.account_contact_details_email.value=document.add_account_form.contact_details_email.value;
  document.add_account_form.account_contact_details_website.value=document.add_account_form.contact_details_website.value;
  document.add_account_form.account_contact_details_address.value=document.add_account_form.contact_details_address.value;
  document.add_account_form.account_contact_details_suburb.value=document.add_account_form.contact_details_suburb.value;
  document.add_account_form.account_contact_details_state.value=document.add_account_form.contact_details_state.value;
  document.add_account_form.account_contact_details_postcode.value=document.add_account_form.contact_details_postcode.value;
  document.add_account_form.account_contact_details_business_name.value=document.add_account_form.contact_details_business_name.value;
  // document.add_account_form.account_name.value=document.add_account_form.contact_details_business_name.value;
  }
  else {
  document.add_account_form.account_contact_details_first_name.value="";
  document.add_account_form.account_contact_details_last_name.value="";
  document.add_account_form.account_contact_details_mobile.value="";
  document.add_account_form.account_contact_details_phone.value="";
  document.add_account_form.account_contact_details_fax.value="";
  document.add_account_form.account_contact_details_email.value="";
  document.add_account_form.account_contact_details_website.value="";
  document.add_account_form.account_contact_details_address.value="";
  document.add_account_form.account_contact_details_suburb.value="";
  document.add_account_form.account_contact_details_state.value="";
  document.add_account_form.account_contact_details_postcode.value="";
  document.add_account_form.account_contact_details_business_name.value="";
 }

}

function start_carousel() {
  new Carousel('carousel-wrapper', $$('#carousel-content .slide'), $$('a.carousel-control', 'a.carousel-jumper'), {circular: true, wheel: false});
}


// Hides the 3 zoomed product page images, if they exist
function hide_zoom_standard() {
  $('zoom_image_standard_1').fade({ duration: 0.4, from: 1, to: 0 }); 
  $('zoom_image_standard_2').fade({ duration: 0.4, from: 1, to: 0 }); 
  $('zoom_image_standard_3').fade({ duration: 0.4, from: 1, to: 0 });  
}
  
  
// Flowplayer   
function playClip(clipUrl) {       
    $f(0).play(clipUrl);
}