// Main JavaScript document for www.alternatedwellings.com.au

// Author:      Mark Rall
// Contact:     contact@markrall.com.au


// desc         Main ADL namespace and initialisation.
// note         This function should be called just before the closing </body> tag,
//              which then acts as an on-DOM-load callback.

function ADL() {
		ADL.setBrowserOsSelector ();
		ADL.randomImage(['hp-splash-01', 'hp-splash-02', 'hp-splash-03', 'hp-splash-04']);
		ADL.customDiv ();
};


// CSS Browser Selector.  Inspired by http://rafael.adm.br/css_browser_selector/,
// licensed under Creative Commons.
ADL.setBrowserOsSelector = function () {

  function is (t) {
    return (ua.indexOf(t) != -1);

  }

  var ua = navigator.userAgent.toLowerCase();

  var browser = (!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua)) ?
    ('ie ie'+RegExp.$1) :
    is('gecko/')          ? 'gecko'                 :
    is('opera/9')         ? 'opera opera9'          :
    /opera (\d)/.test(ua) ? 'opera opera'+RegExp.$1:
    is('konqueror')       ? 'konqueror'             :
    is('applewebkit/')    ? 'webkit safari'         :
    is('mozilla/')        ? 'gecko'                 : '';

  var os = (is('x11') || is('linux')) ? ' linux' :
    is('mac') ? ' mac' :
    is('win') ? ' win' : '';

  YAHOO.util.Dom.addClass('html', browser + os);

}

// banner rotation functionality

ADL.randomNumber = function (limit) {
  return Math.floor(Math.random()*limit);
}

ADL.simplePreload = function () { 
  var args = ADL.simplePreload.arguments;
  document.imageArray = new Array (args.length);
  for (var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}

ADL.randomImage = function (imgArr) {
  if(document.getElementById('home')) {
		  ADL.simplePreload (imgArr);
		
		  var imgSrc, r;
    r = ADL.randomNumber(imgArr.length);
    imgSrc = imgArr[r];
				
				YAHOO.util.Dom.get('randomBanner').src = 'assets/images/' + imgSrc + '.jpg';
				
		} else {
			 return null;

		}
		
}


// new slide image functionality
ADL.showslide = function (slide) {
		var newSlide = slide;
		
		if (newSlide) {
  		document.getElementById ('rotating-banner').src = 'assets/images/' + newSlide + '.jpg';
		
		}

}

// show/hide container functionality
var previousElement = null;

function showdiv(divObj) {
  var currentElement = document.getElementById(divObj);

	 if (currentElement) {
		  if ((currentElement.style.display == "none" || currentElement.style.display == "")) {
			   currentElement.style.display = "block";
		    } else {
			   if (currentElement.style.display = "block") { currentElement.style.display = "none"; }
		  }
		if (previousElement && currentElement != previousElement) { previousElement.style.display = "none"; }
		previousElement = currentElement;
	} else { previousElement = null; }
}

function fold(toSrc) {
	 if (toSrc.parentElement) {
		  divObj = toSrc.parentElement.parentElement;
	   } else {
		  divObj = toSrc.parentNode.parentNode;
	 }

	 divObj.style.display = "none";
	 previousElement = null;

}

ADL.customDiv = function () {
	whichDiv = null;
	// work out which div to display if given a hash -
	// no hash  = default div or all divs, change line below
	whichDiv = document.location.hash ? document.location.hash : '';
	whichDiv = whichDiv.replace('#','');
	// defaulting to div1
	if (whichDiv && document.getElementById(whichDiv) ) {
		if (previousElement) { previousElement.style.display = "none"; }
		document.getElementById(whichDiv).style.display="block";
		previousElement = document.getElementById(whichDiv);
	}
}


// Quotation form functionality
function submitForms() {
if (isname() && isname2() && ispostal() && isdelivery() && ValidateEmail() && ishome() && iswork())
if (confirm("\n You are about to e-mail your submission. \n\nOK to submit.    Cancel to abort."))
{
alert("\n Thank you for your enquiry!");
return true;
}
else
{
alert("\n You have chosen to abort the submission.");
return false
}
else 
return false;
}

function isname() {
if (document.form1.first_name.value == "")
{
alert ("\n The First name field is blank. \n\n Please enter your Firstname.")
document.form1.first_name.focus();
return false;
}
return true;
}

function isname2() {
if (document.form1.surname.value == "")
{
alert ("\n The Surname field is blank. \n\n Please enter your Surname.")
document.form1.surname.focus();
return false;
}
return true;
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function ValidateEmail(){
	var emailID=document.form1.email

	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }

function ispostal() {
if (document.form1.postal.value == "")
{
alert ("\n The Postal Address field is blank. \n\n Please enter your Postal Address.")
document.form1.postal.focus();
return false;
}
return true;
}

function isdelivery() {
if (document.form1.delivery.value == "")
{
alert ("\n The Delivery Address field is blank. \n\n Please enter your Delivery Address.")
document.form1.delivery.focus();
return false;
}
return true;
}

function ishome() {
if (document.form1.home_phone.value == "")
{
alert ("\n The Home Phone field is blank. \n\n Please enter your Home Phone.")
document.form1.home_phone.focus();
return false;
}
return true;
}

function iswork() {
if (document.form1.work_phone.value == "")
{
alert ("\n The Work Phone field is blank. \n\n Please enter your Work Phone.")
document.form1.work_phone.focus();
return false;
}
return true;
}


