// DOM FUNCTIONS



function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') d[n] = null;
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) purge(d.childNodes[i]);
    }
}

 
//STRING FUNCTIONS

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

var xmlNodeText = function(text, tag){
    var m = text.match(new RegExp("<(" + tag + ")(?:\\s?[^>]*)>((?:\\s|\\S)*)<\/\\1>"));
    if (m && m[2]) return m[2];
    else return null;
}    

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function getFileExtension(filename) { 
    f = filename.toLowerCase(); 
    f = f.split("?")[0];
    f = f.split("?")[0];
    f = f.split("?")[0];
    f = f.split("?")[0];
    if( f.length == 0 ) return ""; 
    var dot = f.lastIndexOf("."); 
    if( dot == -1 ) return ""; 
    var extension = f.substr(dot+1,f.length); 
    return extension; 
} 

// USE INSTEAD OF INNERHTML=
function replaceHtml(el, html) {
	var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	/* Since we just removed the old element from the DOM, return a reference
	to the new element, which can be used to restore variable references. */
	return newEl;
};

//VALIDATION FUNCTIONS BLOCK

function validateAuMobile(str_phone){
    var re=new RegExp(/^04\d{8}$/);
    if(!re.exec(str_phone)){
         return false;
    }
    else
        return true;

}

function validateEmail(str_email){
    var re=new RegExp(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/);
    if(!re.exec(str_email)){
         return false;
    }
    else
        return true;

}

function validatePassword(str_password){
    var re=new RegExp(/^[a-zA-Z0-9]{5,10}$/);
    if(!re.exec(str_password)){
         return false;
    }
    else
        return true;

}

//--------------------------



var DJgallery = {

	holderID : "#imageGallery",
	autorun : false,
	height : 310,
	width : 450,
	imagePath : "images/gallery/",
	imageArray : ["photo1.jpg","photo2.jpg","photo3.jpg"],

	imageCount : 0,
	currentPos : 0,
	useNumberTwo : true,

	init : function(){

	    this.currentPos = 0;
	    this.imageCount = this.imageArray.length;

		//draw placeholder elements
        var html = "<img src='" + this.imagePath+this.imageArray[0] + "' id='DJGalleryImage1' height='" + this.height + "' width='" + this.width + "'><img src='" + this.imagePath+this.imageArray[0] + "' id='DJGalleryImage2' height='" + this.height + "' width='" + this.width + "'>";
        html = html + "<div id='DJGalleryControls'><p>Photo <span id='DJGalleryCurrentPos'>1</span> of " + this.imageCount + "</p>&nbsp;<img src='/global/images/galleryPrev.png' alt='&laquo;' id='DJGalleryBackButton' />&nbsp;<img alt='&raquo;' id='DJGalleryNextButton'  src='/global/images/galleryNext.png' /></div>";

        jQuery(this.holderID).html(html);
        
		jQuery("#DJGalleryImage2").css("display", "block");
		jQuery("#DJGalleryImage2").css("opacity", "0");
		jQuery("#imageGallery").css("height", this.height+"px");
		jQuery("#imageGallery").css("width", this.width+"px");
		jQuery("#DJGalleryBackButton").click(function(){DJgallery.prev();});
		jQuery("#DJGalleryNextButton").click(function(){DJgallery.next();});
		

		jQuery("#DJGalleryImage1").click(function(){DJgallery.next();});
		jQuery("#DJGalleryImage2").click(function(){DJgallery.next();});
		
	}
	,

	go : function(move){
		this.currentPos += move;
		if(this.currentPos == this.imageCount)this.currentPos = 0;
		if(this.currentPos == -1 )this.currentPos = this.imageCount-1;

		//animate
		var currentImage = "#DJGalleryImage1";
		var newImage = "#DJGalleryImage2";
		if(!this.useNumberTwo){
			currentImage = "#DJGalleryImage2";
			newImage = "#DJGalleryImage1";
		}

        jQuery(newImage).css("opacity", "0");

		//swap z-index
		
		jQuery(currentImage).css("z-index", "400");
		jQuery(newImage).css("z-index", "500");
		jQuery("#DJGalleryControls").css("z-index", "600");

		jQuery(newImage).attr("src", this.imagePath+this.imageArray[this.currentPos]);
		jQuery("#DJGalleryCurrentPos").html(this.currentPos+1);

		//swap current/new
		this.useNumberTwo = !this.useNumberTwo;

		//fade in new
		jQuery(newImage).animate({
		    opacity: 1
		}, 250, null, function() {
			//hide current
			jQuery(currentImage).css("opacity", "0");
		});
	},

	next : function(){
		this.go(1);
	},
	prev : function(){
		this.go(-1);
	}
}

// 'clicks' the specified button on return press
function clickButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        if (evt.keyCode == 13) {
        bt.click();
        return false;
        }
    }
}

