var ie5 = document.all && document.getElementById;
var ns6 = document.getElementById && !document.all;
var arVersion = navigator.appVersion.split("MSIE");
var ieversion = parseFloat(arVersion[1]);
var ROOT = '/cms/';
var ADMINROOT = '/cms/admin/';
var FormGo = 1;
var MsgAlert = '';

function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  if (oldonload) {
			oldonload();
		  }
		  func();
		}
	  }
	}


function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
{
   key = window.event.keyCode;
}
else if (e)
{
   key = e.which;
}
else
{
   return true;
}
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27)  || (key==46) )
{	
   return true;
}
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
{
   return true;
}
// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
{
   return false;
}
}

function submitRegistration(div, frm, msg, returnurl) 
		{
			var returnurl = returnurl;
			var msg = msg;
			var div = div;
			var frm = frm;
			
			var regForm = document.frm_Register;
			
			if(regForm.username.value=='')
			{
				MsgAlert += "Please enter your Username\n";
				FormGo = 0;
			}
			if(regForm.password1.value=='')
			{
				MsgAlert += "Please enter your Password\n";
				FormGo = 0;
			}
			if(regForm.password1.value!=regForm.password2.value)
			{
				MsgAlert += "Password Mismatch\n";
				FormGo = 0;
			}
			if(regForm.fname.value=='')
			{
				MsgAlert += "Please enter your First Name\n";
				FormGo = 0;
			}
			if(regForm.lname.value=='')
			{
				MsgAlert += "Please enter your Last Name\n";
				FormGo = 0;
			}
			if(regForm.nric.value=='')
			{
				MsgAlert += "Please enter your NRIC #\n";
				FormGo = 0;
			}
			if(regForm.email.value=='')
			{
				MsgAlert += "Please enter your Email Address\n";
				FormGo = 0;
			}
			if(regForm.email.value!='')
			{
				var str=regForm.email.value
				var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
				
				if (!filter.test(str))
				{
				MsgAlert += "Please enter your a valid Email address\n";
				FormGo = 0;
				regForm.email.focus();
				
				}	
			}
			if(regForm.contact_no.value=='')
			{
				MsgAlert += "Please enter your Contact #\n";
				FormGo = 0;
			}
			if(regForm.address.value=='')
			{
				MsgAlert += "Please enter your Address\n";
				FormGo = 0;
			}
			if(regForm.postalcode.value=='')
			{
				MsgAlert += "Please enter your Postal Code\n";
				FormGo = 0;
			}
			
			if(FormGo==0) {
				alert(MsgAlert);
				MsgAlert = '';
				FormGo = 1;
				return false;
			}
			else
			{
				
				new Ajax.Updater(div, ROOT+returnurl, {asynchronous:true, evalScripts: true, parameters:Form.serialize(frm)});
				return true;
			}
			
			
		}



function submitAddProduct(div, frm, msg, returnurl) 
		{
			var returnurl = returnurl;
			var msg = msg;
			var div = div;
			var frm = frm;
			
			var regForm = document.frm_Product;
			
			if(regForm.product_cat_id.value=='')
			{
				MsgAlert += "Please select a Category\n";
				FormGo = 0;
			}
			if(regForm.product_code.value=='')
			{
				MsgAlert += "Product code is required\n";
				FormGo = 0;
			}
			if(regForm.product_name.value=='')
			{
				MsgAlert += "Product name is required\n";
				FormGo = 0;
			}
			if(regForm.price.value=='')
			{
				MsgAlert += "Product price is required\n";
				FormGo = 0;
			}
			
			
			if(FormGo==0) {
				alert(MsgAlert);
				MsgAlert = '';
				FormGo = 1;
				return false;
			}
			else
			{
				
				new Ajax.Updater(div, ADMINROOT+returnurl, {asynchronous:true, evalScripts: true, parameters:Form.serialize(frm)});
				return true;
			}
			
			
		}


function loader(div, txt)
{
	//var loader = '<div class="preview cat'+cid+'"><ul><li><h2>Loading... <img src="/css/images/loader.gif" /></h2><p>&nbsp;</p></li></ul></div>';
	document.getElementById(div).innerHTML = '<img src="'+ROOT+'images/loader.gif" />&nbsp;<span class="loader">'+txt+' ...</span>';
}

function goTo(url)
{
	//var loader = '<div class="preview cat'+cid+'"><ul><li><h2>Loading... <img src="/css/images/loader.gif" /></h2><p>&nbsp;</p></li></ul></div>';
	document.location.replace(url);
}

function returnNormal(val)
{
	var val = val;
	document.getElementById('country').innerHTML=val;
}


function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
				
				
						
					
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
				
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	
}

function getRealLeft(el){
xPos = document.getElementById(el).offsetLeft;
tempEl = document.getElementById(el).offsetParent;
while (tempEl != null) {
xPos += tempEl.offsetLeft;
tempEl = tempEl.offsetParent;
}
return xPos;
}

function getRealTop(el){
yPos = document.getElementById(el).scrollLeft;
tempEl = document.getElementById(el).offsetParent;
while (tempEl != null) {
yPos += tempEl.offsetTop;
tempEl = tempEl.offsetParent;
}
return yPos;
}

function dispTruePos(isID){
trueX = getRealLeft(isID);
trueY = getRealTop(isID);

return 
alert('True Xpos is: '+trueX+'\nTrue Ypos is: '+trueY)
}

function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
		alert(yPos);
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
			yPos = elem.style.pixelHeight;
		} else if (document.all){
			var elem = document.all[Elem];
			yPos = elem.offsetHeight;
		}
		
		alert(yPos);
		return yPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
			xPos = elem.style.pixelWidth;
		} else if (document.all){
			var elem = document.all[Elem];
			xPos = elem.offsetWidth;
		}
		
		return xPos;
	}
}

function getElemWidth(elm){
    var elm;
    if(typeof(elm) === "object"){
      elm = elm;
    } else {
      elm = document.getElementById(elm);
    }
    return parseInt(elm.offsetWidth, 10);
  }
  
  
  function popitup(url) {
	newwindow=window.open(url,'name','height=400,width=250');
	if (window.focus) {newwindow.focus()}
	return false;
}

////////////////////////////////////////////////
// PNG HACKS


var pngImagesOver = new Array();
var pngImagesOut = new Array();
function pngImage(containerId, imagePath, width, height, alt) {
  var container = document.getElementById(containerId);
  if(container) {
    var element;
    if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)) {
      element = document.createElement('SPAN');
      element.id = containerId + 'Img';
      element.style.display = 'block';
      element.style.width = width;
      element.style.height = height;
      element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imagePath + "', sizingMethod='scale')";
    }
    else {
      element = document.createElement('IMG');
      element.id = containerId + 'Img';
      element.src = imagePath;
      element.alt = alt;
      element.width = width;
      element.height = height;
    }
    container.appendChild(element);
  }
}

function pngImageBG(containerId, imagePath, width, height, alt) {
  var container = document.getElementById(containerId);
  if(container) {
    if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)) {
      container.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imagePath + "', sizingMethod='scale')";
    }
    else {
      container.style.backgroundImage = 'url(' + imagePath + ')';
    }
  }
}

function pngMouseOverImg(imgId, imgPath) {
  var obj = document.getElementById(imgId);
  if(imgId) {
    if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)) {
      obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgPath + "', sizingMethod='scale')";
    }
    else {
      obj.src = imgPath;
    }
  }
}

function pngMouseOutImg(imgId, imgPath) {
  var obj = document.getElementById(imgId);
  if(imgId) {
    if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)) {
      obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgPath + "', sizingMethod='scale')";
    }
    else {
      obj.src = imgPath;
    }
  }
}


///////////////////////
/// MouseOVER ROW

var arrayOfRolloverClasses = new Array();
	var arrayOfClickClasses = new Array();
	var activeRow = false;
	var activeRowClickArray = new Array();
	
	function highlightTableRow()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;

		if(this!=activeRow){
			this.setAttribute('origCl',this.className);
			this.origCl = this.className;
		}
		this.className = arrayOfRolloverClasses[tableObj.id];
		
		activeRow = this;
		
	}
	
	function clickOnTableRow()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;		
		
		if(activeRowClickArray[tableObj.id] && this!=activeRowClickArray[tableObj.id]){
			activeRowClickArray[tableObj.id].className='';
		}
		this.className = arrayOfClickClasses[tableObj.id];
		
		activeRowClickArray[tableObj.id] = this;
				
	}
	
	function resetRowStyle()
	{
		var tableObj = this.parentNode;
		if(tableObj.tagName!='TABLE')tableObj = tableObj.parentNode;

		if(activeRowClickArray[tableObj.id] && this==activeRowClickArray[tableObj.id]){
			this.className = arrayOfClickClasses[tableObj.id];
			return;	
		}
		
		var origCl = this.getAttribute('origCl');
		if(!origCl)origCl = this.origCl;
		this.className=origCl;
		
	}
		
	function addTableRolloverEffect(tableId,whichClass,whichClassOnClick)
	{
		arrayOfRolloverClasses[tableId] = whichClass;
		arrayOfClickClasses[tableId] = whichClassOnClick;
		
		var tableObj = document.getElementById(tableId);
		var tBody = tableObj.getElementsByTagName('TBODY');
		if(tBody){
			var rows = tBody[0].getElementsByTagName('TR');
		}else{
			var rows = tableObj.getElementsByTagName('TR');
		}
		for(var no=0;no<rows.length;no++){
			rows[no].onmouseover = highlightTableRow;
			rows[no].onmouseout = resetRowStyle;
			
			if(whichClassOnClick){
				rows[no].onclick = clickOnTableRow;	
			}
		}
		
	}
	
	
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function _popURL(URL,width,height) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height=" + height + "');");
}
