/* ===============================================================
 * Form Validation
 *  
 *  Abstract       :  
 *
 *  Filename       : 
 *  Creation Date  : 
 *  Author         : Carl C. Balshaw
 *  
 *  Code Status    : Draft Development
 *  
 *  Revisions      :
 *  14 Oct 2009  - first version
 * 
 * ---------------------------------------------------------------
 *  Notes:
 *    This is a....
 *
 *
 * ---------------------------------------------------------------
 * (c) Copyright 2009, Rivington Computer Services Ltd.
 * =============================================================== */

function Max(a,b) {
	return (a>b?a:b);
}

function GetElementHeightByTag(tag)
{
	var result=0;
	var e=document.getElementsByTagName(tag);
	if (e==null) {
		return 0;
	}
	
	for(var i=0;i<e.length;i++) {
		result=(e[i].offsetHeight>result?e[i].offsetHeight:result)
	}
	
	return result;
}

function GetElementHeightById(id)
{
	var e=document.getElementById(id);
	return (e==null?0:e.offsetHeight);
}

function SetMinHeightbyID(pID,pHeight){
	h=GetElementHeightById(pID);
	if (pHeight>h) SetHeight(pID,pHeight);
}

function SetHeight(pID,pHeight) {
	
	var e=document.getElementById(pID);
	//lPaddingT = getStyle(pID,"PaddingTop");
	//lPaddingT = getStyle(pID,"PaddingTop");
	lPadding = getStyle(pID,"padding");
	//lPadding=lPaddingT+lPaddingB;
	lHeight=pHeight-lPadding;
	//alert(pHeight+" = "+lHeight+" + "+lPadding);
	e.style.height =pHeight+"px";
}

function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

/*
 when setting height 
*/

function SetHeightFromIDArray(id,ids,padding) {
	
	if (padding===undefined) padding=0;
	
	s="SetHeightFromIDArray for "+id+"\n";
	
	h=GetElementHeightById(id);
	m=h;
	s=s+id+".height= "+m+"\n";
	
	for(var a=0;a<ids.length;a++){
		e=GetElementHeightById(ids[a]);
		s=s+ids[a]+".height= "+e+"\n";
		m=Max(m,e);
	}
	
	s=s+"Max height= "+m+"\n";
	
	m=m-padding;
	//alert(s);
	if (m>h) {
		SetMinHeightbyID(id,m);
		
	}

}

/* ===============================================================
 * Read From Server
 *
 *
 * =============================================================== */

function ajaxGetBannerData(pURL,pParams) {
	
		if ( par === undefined ) {
			parentvalue=0;
		} else {
			parentvalue=$(par).val();
		}
		
		$(ctrl).html('<option>...please wait...</option>');
		
		$.getJSON(url,{parent: parentvalue},function(myOptions){
			var shtml='';
			for (myOption in myOptions) {
				shtml=shtml+'<option value="'+myOption+'">'+myOptions[myOption]+'</option>';
			}
			$(ctrl).html(shtml);
		})
}
/* ===============================================================
 * Dynamic Data
 *
 *
 * =============================================================== */

function ajaxReadOptionData(ctrl,url,par) {
	
		if ( par === undefined ) {
			parentvalue=0;
		} else {
			parentvalue=$(par).val();
		}
		
		$(ctrl).html('<option>...please wait...</option>');
		
		$.getJSON(url,{parent: parentvalue},function(myOptions){
			var shtml='';
			for (myOption in myOptions) {
				shtml=shtml+'<option value="'+myOption+'">'+myOptions[myOption]+'</option>';
			}
			$(ctrl).html(shtml);
		})
}

/* ===============================================================
 * Image Preloader
 *
 *
 * =============================================================== */

var PreloaderCount = 0;
var Preloader = new Array();

function PreloadImage(ImageSource) {
	
	PreloaderCount++;
	Preloader[PreloaderCount] = new Image(16,16);
	Preloader[PreloaderCount].src = ImageSource;
	
}

function PreloadImages(PhotoArray) {
	
	for(var a=0;a<PhotoArray.length;a++){
		PreloadImage(PhotoArray[a]);
	}

}


/* ===============================================================
 * Image Fader
 *
 *
 * =============================================================== */

function SetOpacity(objImage,intOpacity)
{
  objImage.style.filter = 'alpha(opacity=' + intOpacity + ')';
  objImage.style.MozOpacity = intOpacity/100;
  objImage.style.opacity = intOpacity/100;
}

function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
  var element=document.getElementById(id);
  var opacity = element.style.opacity * 100;
  var msNow = (new Date()).getTime();
  opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity<0) 
    SetOpacity(element,0)
  else if (opacity>100)
    SetOpacity(element,100)
  else
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
  }
}

function getElementChild(pParent)
{
	var lResult;
	var lChild=pParent.firstChild;
	while (lChild!=pParent.lastChild) {
		if (lChild.nodeType==1) lResult=lChild;
		lChild=lChild.nextSibling;
	}
	return lResult;
}

function FadeInImage(id,img)
{
  var a=document.getElementById(id);
  var b=a.parentNode;
  
  if (b.nodeName=="A") {
  	b=b.parentNode;
  }

  b.style.backgroundImage = 'url(' + a.src + ')';
  b.style.backgroundRepeat = 'no-repeat';
  
  SetOpacity(a,0);
  a.src = img;
  
  var t0 = (new Date()).getTime();
  if (a.timer) clearTimeout(a.timer);
  ChangeOpacity(id,500,t0,0,100);
}


/* ===============================================================
 * Slideshow
 *
 *
 * =============================================================== */
function Slideshow(imageID,PhotoArray) {
	this.Photos  = PhotoArray;
	this.ImageID = imageID;
	this.CurrentImage=0;
	this.NumberOfImages=PhotoArray.length;
	
	for(var a=0;a<PhotoArray.length;a++){
		PreloadImage(PhotoArray[a]);
	}
	
	window.setInterval(function () {
		
		mySlideshow.CurrentImage++;
		
		if (mySlideshow.CurrentImage>=mySlideshow.NumberOfImages) {
			mySlideshow.CurrentImage = 0;
		}
		
		FadeInImage(mySlideshow.ImageID,mySlideshow.Photos[mySlideshow.CurrentImage]);

	},2500);
}


/* ===============================================================
 * Form Validation
 *
 *
 * =============================================================== */

gFrmRes = true;

function ValidateForm(pForm) {
	
	gFrmRes = true;
	gFrmMsg = "";
	
	Validation(pForm);
	
	if (gFrmRes==false) {
		alert("You must enter...\n"+gFrmMsg);
		return(false);
	} else {
		return(true);
	}
}
function Required(pField,pTitle) {

	with (pField) {
		if (value==null||value=="") {
			gFrmRes=false;
			gFrmMsg=gFrmMsg+"* "+pTitle+"\n";
		}
	}
}
