/* =========================================================================

JavaScript Source File -- Created with Brain

NAME: DOMFunctions

AUTHOR: Roobin , Travelize, also Stolen quite a bit from Adam at http://adamv.com
DATE  : 2007-03-07

COMMENT: Feel Free to add functions. The purpose of this file is to have a simple way
		of manipulate layers(other elements as well). Toggle display and so on.
		Examples: To Come....

============================================================================ */

function hideLayer(whichLayer) {

   if (document.getElementById) {
   // this is the way the standards work
   document.getElementById(whichLayer).style.visibility = "hidden";
   document.getElementById(whichLayer).style.display = "none";
   }
   else if (document.all) {
   // this is the way old msie versions work
   document.all[whichlayer].style.visibility = "hidden";
   document.all[whichlayer].style.display = "none";
   }
   else if (document.layers) {
   // this is the way nn4 works
   document.layers[whichLayer].visibility = "hidden";
   document.layers[whichLayer].display = "none";
   }
//alert(whichLayer)
}

function showLayer(whichLayer) {
   if (document.getElementById) {
   // this is the way the standards work
   document.getElementById(whichLayer).style.visibility = "visible";
   document.getElementById(whichLayer).style.display = "";
   }
   else if (document.all) {
   // this is the way old msie versions work
   document.all[whichlayer].style.visibility = "visible";
   document.all[whichlayer].style.display = "";
   }
   else if (document.layers) {
   // this is the way nn4 works
   document.layers[whichLayer].visibility = "visible";
   document.layers[whichLayer].display = "";
   }
//alert(whichLayer)
}

function toggleLayer(whichLayer) {
   if (document.getElementById) {
   // this is the way the standards work
   	if (document.getElementById(whichLayer).style.visibility == "hidden"){
   			document.getElementById(whichLayer).style.visibility = "visible";
   			document.getElementById(whichLayer).style.display = "";
   		}
   	else{
   			document.getElementById(whichLayer).style.visibility = "hidden";
   			document.getElementById(whichLayer).style.display = "none";
   	}
   }
   else if (document.all) {
   // this is the way old msie versions work
   	if(document.all[whichlayer].style.visibility == "hidden"){
   			document.all[whichlayer].style.visibility = "hidden";
   			document.all[whichlayer].style.display = "none";
   		}
   	else{
   			document.all[whichlayer].style.visibility = "visible";
   			document.all[whichlayer].style.display = "";
   		}
   }
   else if (document.layers) {
   // this is the way nn4 works
   	if(document.layers[whichLayer].visibility == "hidden"){
   			document.layers[whichLayer].visibility = "visible";
   			document.layers[whichLayer].display = "";
   		}
   	else{
   			document.layers[whichLayer].visibility = "hidden";
   			document.layers[whichLayer].display = "none";
   		}
   }	
}

function opacity(id, opacStart, opacEnd, millisec)
{
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) 
    {
        for(i = opacStart; i >= opacEnd; i--) 
        {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } 
    else if(opacStart < opacEnd)
    {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        	}
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
    object.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+opacity+")";
    //alert(opacity+'  '+id);
} 


/* DOM & DHTML helper functions */
/* Version 0.9.3, 19 Jun 2005, Adamv.com */

// Looks up the given node by ID if a string is passed in,
// otherwise returns the given object.
// This lets functions work either with a node ID to look up
// or an already retreived node.

// Don't pass in anything other than strings or nodes.
function $(o) {
	if (typeof(o) == "string")
		return document.getElementById(o)
	else
		return o;
}


/*Example Usage Roo
 function test(id){
 
 DOM.enable(id);
 DOM.before(id,'Value of the the new node','div');
 DOM.toggle(id);
 DOM.show(id);
 
 }
*/
DOM = {
	// Remove all the children of the given node
	nuke: function(id){
		var o=$(id); if (!o) return;
		while(0 < o.childNodes.length)
			o.removeChild(o.childNodes[0]);
	},

	nukebytagandclass: function(tagname,classname){	
		// first loop over elements
		var elements;
		if(tagname == '*') 
		{
			// '*' not supported by IE/Win 5.5 and below
			elements = (ie) ? document.all : document.getElementsByTagName('*');
		} 
		else 
		{
			elements = document.getElementsByTagName(tagname);
		}
		for(var i = 0; i < elements.length; i++){
			var node = elements.item(i);
			for(var j = 0; j < node.attributes.length; j++) {
				if(node.attributes.item(j).nodeName == 'class') {
					if(node.attributes.item(j).nodeValue == classname) {
							while(0 < node.childNodes.length)
								node.removeChild(node.childNodes[0]);
					}
				}
			}		
		}	
	},
	
	before: function(id, node, nodeType){
		var o=$(id); if (!o) return;
		if (typeof(node) == "string"){
			var newNode = document.createElement(nodeType || "div")
			newNode.innerHTML = node
			node = newNode
		}
		o.parentNode.insertBefore(node, o)
	},
	
	after: function(id, node, nodeType){
		var o=$(id); if (!o) return;
		if (typeof(node) == "string"){
			var newNode = document.createElement(nodeType || "div")
			newNode.innerHTML = node
			node = newNode
		}
		o.parentNode.insertBefore(node, o.nextSibling)
	},
	
	// Sets the visibility of the given node
	set_visible: function(id,visible){
		if (visible) Display.show(id); else Display.hide(id)
	},
	
	// Hides the given node by setting CSS display: none
	dohide: function(id){Display.show(id, "none")},
	// Displays the given node by clearing the CSS display property
	// This resets display: to the default value for that element type
	show: function(id, style){
		var o=$(id); if (!o) return;
		o.style.display = style || "";
	},
		
	// Toggle the visibility of the given node
	toggle: function(id){
		var o=$(id); if (!o) return;
		(o.style.display == "none") ? Display.show(o) : Display.hide(o)
	},
	
	// Enables the given node (useful for form elements.)
	enable: function(id){
		var o=$(id); if (!o) return;
		o.disabled="";
	},
		
	// Disables the given node (useful for form elements.)
	disable: function(id){
		var o=$(id); if (!o) return;
		o.disabled="disabled";
	},
	
	background: function(id, color){
		var o=$(id); if (!o) return;
		o.style.background=color
	}
}
/* QueryString functionality to js
	usage: var roo = displayItem('action');
*/
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
	for(var j=0; j < this.keyValuePairs.length; j++) {
		if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key){
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function displayItem(key){
	if(queryString(key)=='false')
	{
		//document.write("you didn't enter a ?name=value querystring item.");
		return '';
	}else{
		//document.write(queryString(key));
		return queryString(key);
	}
}


//CSS STYLE MANIPULATIONS//

// ugly workaround for missing support for selectorText in Netscape6/Mozilla
// call onLoad() or before you need to do anything you would have otherwise used
// selectorText for.
var ugly_selectorText_workaround_flag = false;
var allStyleRules;
// code developed using the following workaround (CVS v1.15) as an example.
// http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
function ugly_selectorText_workaround() {

	if((navigator.userAgent.indexOf("Gecko") == -1) ||
	   (ugly_selectorText_workaround_flag)) {
		return; // we've already been here or shouldn't be here
	}
	var styleElements = document.getElementsByTagName("style");
	
	for(var i = 0; i < styleElements.length; i++) {
		var styleText = styleElements[i].firstChild.data;
		// this should be using match(/\b[\w-.]+(?=\s*\{)/g but ?= causes an
		// error in IE5, so we include the open brace and then strip it
		allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
	}

	for(var i = 0; i < allStyleRules.length; i++) {
		// probably insufficient for people who like random gobs of 
		// whitespace in their styles
		allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
	}
	ugly_selectorText_workaround_flag = true;

}


// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

// getStyleById: given an element ID and style property
// return the current setting for that property, or null.
// args:
//  i - element id
//  p - property
function getStyleById(i, p) {
	var n = document.getElementById(i);
	var s = eval("n.style." + p);

	// try inline
	if((s != "") && (s != null)) {
		return s;
	}

	// try currentStyle
	if(n.currentStyle) {
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null)) {
			return s;
		}
	}
	
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[y] == i) {
							return z[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) ||
						   (rules[y].selectorText == i)) {
							return z[p];
						}
					}
				}
			}
		}
	}
	return null;
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;

function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}

// getStyleByClass: given an element type, a class selector and a property,
// return the value of the property for that element type.
// args:
//  t - element type
//  c - class identifier
//  p - CSS property
function getStyleByClass(t, c, p) {
	// first loop over elements, because if they've been modified they
	// will contain style data more recent than that in the stylesheet
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					var theStyle = eval('node.style.' + p);
					if((theStyle != "") && (theStyle != null)) {
						return theStyle;
					}
				}
			}
		}		
	}
	// if we got here it's because we didn't find anything
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++) {
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0) {
				// check each rule
				for(var y = 0; y < rules.length; y++) {
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if((allStyleRules[y] == c) ||
						   (allStyleRules[y] == (t + "." + c))) {
							return z[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) &&
						   ((rules[y].selectorText == c) ||
						    (rules[y].selectorText == (t + "." + c)))) {
							return z[p];
						}
					}
				}
			}
		}
	}

	return null;
}

// setStyleByTag: given an element type, style property and 
// value, and whether the property should override inline styles or
// just global stylesheet preferences, apply the style.
// args:
//  e - element type or id
//  p - property
//  v - value
//  g - boolean 0: modify global only; 1: modify all elements in document
function setStyleByTag(e, p, v, g) {
	if(g) {
		var elements = document.getElementsByTagName(e);
		for(var i = 0; i < elements.length; i++) {
			elements.item(i).style[p] = v;
		}
	} else {
		var sheets = document.styleSheets;
		if(sheets.length > 0) {
			for(var i = 0; i < sheets.length; i++) {
				var rules = sheets[i].cssRules;
				if(rules.length > 0) {
					for(var j = 0; j < rules.length; j++) {
						var s = rules[j].style;
						// selectorText broken in NS 6/Mozilla: see
						// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
						ugly_selectorText_workaround();
						if(allStyleRules) {
							if(allStyleRules[j] == e) {
								s[p] = v;
							}			
						} else {
							// use the native selectorText and style stuff
							if(((s[p] != "") && (s[p] != null)) &&
							   (rules[j].selectorText == e)) {
								s[p] = v;
							}
						}

					}
				}
			}
		}
	}
}

// getStyleByTag: given an element type and style property, return
// the property's value
// args:
//  e - element type
//  p - property
function getStyleByTag(e, p) {
	var sheets = document.styleSheets;
	if(sheets.length > 0) {
		for(var i = 0; i < sheets.length; i++) {
			var rules = sheets[i].cssRules;
			if(rules.length > 0) {
				for(var j = 0; j < rules.length; j++) {
					var s = rules[j].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules) {
						if(allStyleRules[j] == e) {
							return s[p];
						}			
					} else {
						// use the native selectorText and style stuff
						if(((s[p] != "") && (s[p] != null)) &&
						   (rules[j].selectorText == e)) {
							return s[p];
						}
					}

				}
			}
		}
	}

	// if we don't find any style sheets, return the value for the first
	// element of this type we encounter without a CLASS or STYLE attribute
	var elements = document.getElementsByTagName(e);
	var sawClassOrStyleAttribute = false;
	for(var i = 0; i < elements.length; i++) {
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if((node.attributes.item(j).nodeName == 'class') ||
			   (node.attributes.item(j).nodeName == 'style')){
			   sawClassOrStyleAttribute = true;
			}
		}
		if(! sawClassOrStyleAttribute) {
			return elements.item(i).style[p];
		}
	}
}


/*==========VALIDATION OBJECT===========================================*/

VALIDATE = {
	// Remove all the children of the given node
	isEmpty: function(s){
		return ((s == null) || (s.length == 0));
	},
	isWhitespace: function(s){
		var i;
		var bolnotempty = true;
		var whitespace = " \t\n\r";
		// Is s empty?
		if (VALIDATE.isEmpty(s)){
			return true;
			bolnotempty = false;
		}
		// Search through string's characters one by one
		// until we find a non-whitespace character.	 
		for (i = 0; i < s.length; i++)
		{
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (whitespace.indexOf(c) == -1) bolnotempty = false;
		}
		// If bolnoemty=true then field is empty
		if (bolnotempty){
			return true;
		}
		else{
			return false;
		}		
	},
	isEmail: function(str){
		var email = str;
		
		//regular expression
		var ps_pattern = /^([\w\-~.]+\@([\w\-]+\.){1,}[a-zA-Z]{2,4})$/;
		
			//TEST
			if (email == "" || ps_pattern.test(email) == false ) {
			return false;
			}
		
		return true;		
	},
	remove_number_symbols: function(str){
		str = VALIDATE.str_reg_exp_replace(str,",","");
		str = VALIDATE.str_reg_exp_replace(str,"\\\.","");	//needs to have extra backslashes
		str = VALIDATE.str_reg_exp_replace(str," ","");
		str = VALIDATE.str_reg_exp_replace(str,"\-","");
		return str;
	},
	str_reg_exp_replace:function (str, find_value, replace_value) {
		eval("var reg_expression = /"+find_value+"/g;");
		str = str.replace(reg_expression,replace_value);
		return str;
	},
	chk_number: function(numberx,start_length,end_length) {
		var num = new String(numberx);
		var length_start = new String(start_length);
		var length_end = new String(end_length);

		//default
		if (length_start == "" || isNaN(length_start)) {
		length_start = 1;
		}
		if (length_end == "" || isNaN(length_end)) {
		length_end= 100;
		}
		//remove typical values before checking
		num = VALIDATE.remove_number_symbols(num);
		
		//regular expression
		eval("var pn_pattern = /^[0-9]{"+length_start+","+length_end+"}\$/;");
		
		//TEST
		if (num == "" || pn_pattern.test(num) == false ) {
		return false;
		}
		return true;
	},
	chk_phone_number: function(phone_number,phone_length) {
		var pn = new String(phone_number);
		var plength = new String(phone_length);
		var pmin = 5;
		//default
		if (plength == "" || isNaN(plength)) {
			plength = 2;
		}
		return VALIDATE.chk_number(pn,pmin,plength);
	},
	IsNumeric: function(sText){
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
	      { 
	      Char = sText.charAt(i); 
	      if (ValidChars.indexOf(Char) == -1) 
	         {
	         IsNumber = false;
	         }
	      }
	   return IsNumber;
	   
	 },
 	ValidateForm:function(frm,allforms){
  		//alert('validate');
  		  //DOM.nuke('validaterrmsg');
		  DOM.nukebytagandclass('span','validaterrmsg')
		  var Errmsg="";
		  var bolerr=false;
		  var bolall=false;
		  if (allforms==true){
			bolall=true;  	
		  }
		  with (document)
		  {
		  	for (var f=0;f<forms.length;f++)
		  	{
		  		var fobj = document.forms[f];
		  		if(bolall || fobj.name==frm.name){ 
			  		for(var i = 0;i < fobj.elements.length;i++)
			  		{
			  			//alert(fobj.elements[i].name);
			  			var doValidate = fobj.elements[i].getAttribute("validate");
			  			if (!doValidate) continue;
			  				switch(doValidate)
				           	{
				               case "empty":
				               	//fobj.elements[i].onchange = new Function("isWhitespace(this.value,false)");
					            	if(VALIDATE.isWhitespace(fobj.elements[i].value)){
					            		//alert('is empty');
					            		bolerr=true;
					            		DOM.after(fobj.elements[i].id,'<span class="validaterrmsg" id="validaterrmsg"><br><b>Can not be empty</b></span>','span');
					            		//Errmsg+=MakeErrMsg(fobj.elements[i],'<%=t_notempty%>');
					            	}
					            	break;
				            	case "email":
				            		var vaildmail=VALIDATE.isEmail(fobj.elements[i].value);
									//alert(vaildmail);
									if (vaildmail){
									}
									else{
										//alert('not email');
					            		bolerr=true;
					            		//Errmsg+=MakeErrMsg(fobj.elements[i],'<%=er_email%>');
					            		DOM.after(fobj.elements[i].id,'<span class="validaterrmsg" id="validaterrmsg"><br><b>Not Valid Email</b></span>','span');
									}
									break;
								case "phone":
									var validphone=VALIDATE.chk_phone_number(fobj.elements[i].value,40);
									if (validphone){
									}
									else{
										//alert('not phonenumber');
					            		bolerr=true;
					            		//Errmsg+=MakeErrMsg(fobj.elements[i],'<%=er_telephone%>');																
						            	DOM.after(fobj.elements[i].id,'<span class="validaterrmsg" id="validaterrmsg"><br><b>Not Valid phone number</b></span>','span');
									}
									break;
								case "numeric":
									if(VALIDATE.isWhitespace(fobj.elements[i].value)==false){
										if (VALIDATE.IsNumeric(fobj.elements[i].value)==false){
											//Errmsg+=MakeErrMsg(fobj.elements[i],'<%=t_notnumeric%>');
											DOM.after(fobj.elements[i].id,'<span class="validaterrmsg" id="validaterrmsg"><br><b>Not Numeric</b></span>','span');
											//setStyleById(fobj.elements[i].id, "border", "2px solid red");
											bolerr=true;																
										}
									}
									break;
									            		
				            }
				    }
				}
		  			
		  	}
		  }
		  if(bolerr){
		  		//alert(Errmsg);
		  		return false;
		  	}
		  else{
		  	frm.submit();
		  	}
	} 		
}






