// Infinisea Modules Javascript Code

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0 Modified by Peter
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
	if (val) { nm=val.name; if ((val=val.value)!="") {
	  if (test.indexOf('isEmail')!=-1) { p=val.search(/^[^@]+@[^@]+.[a-z]{2,}$/i);
		if (p<0 || p==(val.length-1) || val.indexOf(' ')!=-1) errors+='- '+nm+' must contain an e-mail address.\n';
	  } else if (test!='R') { num = parseFloat(val);
		if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
		if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
		  min=test.substring(8,p); max=test.substring(p+1);
		  if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
	} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}				function mailinglist_subscribe_validate() {
							if(document.getElementById('mailinglist_first_name') && document.getElementById('mailinglist_last_name')) mailinglist_concat_name();
				if (document.getElementById("mailinglist_name").value == '' || document.getElementById("mailinglist_name").value == ' ' || document.getElementById("mailinglist_name").value == 'Name') {
					alert('You must enter your name');
				} else				if (document.getElementById("mailinglist_email").value == '' || document.getElementById("mailinglist_email").value == 'Email') {
					alert('You must enter your email address');
				} else {return true;
					}
					return false;				
			}
			function javascriptformsubmit(name,email,title,group,phone) {
				AjaxRequest.get(
					{
					'url' : 'http://www.infinisea.co.nz/modules/display.php?modulename=Mailinglist&mailinglist_subscribe=yes&mailinglist_title='+title+'&mailinglist_phone='+phone+'&mailinglist_name='+name+'&mailinglist_email='+email+'&mailinglist_group='+group
						,'onSuccess':function(req){
							if(req.responseText == 'success') {
								document.getElementById("successtext").innerHTML = '<div class="">Thank you for subscribing to our Newsletter</div>';
								document.getElementById("mailinglist_form").style.display = "none";
								_gaq.push(['_trackPageview', '/newsletter-sign-up.php']);
							} else {
								document.getElementById("successtext").innerHTML = req.responseText;
							}
						} 
					}
				);
				return false;
			}
			function mailinglist_unsubscribe_validate() {				if (document.getElementById("mailinglist_email").value == '' || document.getElementById("mailinglist_email").value == 'Email') {
					alert('You must enter your email address');
				} else {
					return true;
				}
				return false;			}
			function javascriptunsubscribeformsubmit(email) {
				AjaxRequest.get(
					{
					'url' : 'http://www.infinisea.co.nz/modules/display.php?modulename=Mailinglist&mailinglist_unsubscribe=yes&mailinglist_email='+email	
						,'onSuccess':function(req){
							if(req.responseText == 'success') {
								document.getElementById("unsubscribesuccesstext").innerHTML = '<div class="">You have been unsubscribed from our mailing list.</div>';
								document.getElementById("mailinglist_form").style.display = "none";
							} else {
								document.getElementById("unsubscribesuccesstext").innerHTML = req.responseText;
							}
						} 
					}
				);
				return false;
			}
			function mailinglist_concat_name() {
				document.getElementById('mailinglist_name').value = document.getElementById('mailinglist_first_name').value + ' ' + document.getElementById('mailinglist_last_name').value;
			}function news_confirmItemDelete(id, heading) {
				if(confirm('Are you sure you want to delete the "' + heading + '" news item?')) window.location.href="http://www.infinisea.co.nz/page112.php?action=news_delete&id=" + id;
			}			function search_check(url) {
				var search = document.getElementById("id_search_box").value;
				if(search != '' && search != 'Search...') {
					if(!document.getElementById("id_search_form")) {
						window.location.href = url + '?search=' + search;
					} else {
						document.getElementById("id_search_form").submit();
					}
				}
			}

			function search_enter(e, url) {
				if(window.event) { // IE
					keynum = e.keyCode;
				} else if(e.which) { // Netscape/Firefox/Opera
					keynum = e.which;
				}
				if(keynum == 13) search_check(url);
			}
function changePrice(id,value) {
	newid = id.split('-');
	document.getElementById('PriceNew'+value).innerHTML = newid[1];
}function shoppingcart_checkPriceOption(option, form) {
	var price_checked = false;
	var loop = form.elements.length;
	
	for(i = 0; i < loop; i++) {
		if(form.elements[i].name == "priceid[]") {
			if(form.elements[i].type == "checkbox") {
				if(form.elements[i].checked == true) {
					price_checked = true;
				}
			} else if(form.elements[i].type == "select-one") {
				price_checked = true;
			}
		} else if(form.elements[i].name == "option_size_qty[]") {
			if(form.elements[i].value != '') price_checked = true;
		}
	}
		
	if(!price_checked) {
		if(option == 0) {
			alert("You must have at least one price option selected");
		} else {
			alert("There is no price option selected to add to your cart");
		}
		return false;
	} else {
		return true;
	}
}

function shoppingcart_addToBasket(form) {
	AjaxRequest.submit(
		form
		,{
			'url' : 'http://www.infinisea.co.nz/modules/display.php?modulename=Shoppingcart&ajax=true'
			,'onSuccess' : function (req) {
					var arrResponse = req.responseText.split('|');
					if(document.getElementById('Shoppingcart_cart')) document.getElementById('Shoppingcart_cart').innerHTML = arrResponse[0];
					if(document.getElementById('shoppingcart_ajax_response_' + arrResponse[1]) && arrResponse[2]) {
						document.getElementById('shoppingcart_ajax_response_' + arrResponse[1]).innerHTML = arrResponse[2];
						document.getElementById('shoppingcart_ajax_response_' + arrResponse[1]).style.display = 'block';
					}
				}
			,'onError' : function (req) {form.submit()}
		}
	);
}
function shoppingcart_deleteItem(page, criteria, boolAjax) {
	if(!boolAjax) {
		window.location.href = page + '?' + criteria;
	} else {
		AjaxRequest.get(
			{
			  'url' : 'modules/display.php?modulename=Shoppingcart&ajax=true&' + criteria
			  ,'onSuccess':function(req){
				var arrResponse = req.responseText.split('|');
				if(document.getElementById('Shoppingcart_cart')) document.getElementById('Shoppingcart_cart').innerHTML = arrResponse[0];
			  }
			}
		);
	}
}function Users_passwordType(boolHide) {
	var username = document.getElementById('password_text');
	var password = document.getElementById('password_pass');
	if(password.value != '' || boolHide) {
		password.style.display = 'inline';
		username.style.display = 'none';
		if(boolHide) password.focus();
	} else {
		password.style.display = 'none';
		username.style.display = 'inline';
		username.value = 'Password';
	}
}
