/*
 addOnloadEvent(myFunctionName);
Or to pass arguments
 addOnloadEvent(function(){ myFunctionName('myArgument') });
*/

addOnloadEvent(setupZoom);

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else 
      window.onload = fnc;
  }
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) 
{
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function disableRightClick(e)
{
  var message = "Right click disabled";
  if(!document.rightClickDisabled) // initialize
  {
    if(document.layers) 
    {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
    if (e.which==2||e.which==3)
    {
      alert(message);
      return false;
    }
  }
  else
  {
    alert(message);
    return false;
  }
}
disableRightClick();
document.oncontextmenu=new Function("return false")

function checkEnter(e) { //e is event object passed from function invocation
	var characterCode;

	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		return false;
	} else {
		return true;
	}
}

function wopen(url, name, w, h)
{
  // Fudge factors for window decoration space.
  // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 54;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
  // Just in case width and height are ignored
  win.resizeTo(w, h);
  // Just in case left and top are ignored
  win.moveTo(wleft, wtop);
  win.focus();
}

//configure status message not to show
var statusmsg=""
function hidestatus(){
	window.status=statusmsg
	return true
}

function startProcess() {
	document.getElementById('f1_upload_process').style.visibility = 'visible';
	return true;
}

function stopProcess(success) {
	var result = '';

	if (success == 1){
		document.getElementById('f1_result').innerHTML = '<span class="msg">The process is completed successfully!<\/span><br/><br/>';
	} else if (success == 2){
		document.getElementById('f1_result').innerHTML = '<span class="emsg">There was an error during the process!<\/span><br/><br/>';
	}

	document.getElementById('f1_upload_process').style.visibility = 'hidden';
	return true;
}

function isDocument(formElement, message) {
	var OK = new Array ("csv","doc","pdf","ppt","txt","xls");  // if you change this element, please change the config.php

	trimElement = trim(formElement.value);
	if (trimElement == '') {
		return true;
	}

	var ext = getExt(trimElement);
	_isDocument = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isDocument = true; // one of the file extensions found
		} 
	}

	if (!_isDocument) { 
		if (message!='') alert(message);
		formElement.focus();
	}

	return _isDocument;
}

function isImage(formElement, message) {
	var OK = new Array ("gif","jpg","jpeg","png","wbmp");  // if you change this element, please change the config.php

	trimElement = trim(formElement.value);
	if (trimElement == '') {
		return true;
	}

	var ext = getExt(trimElement);

	_isImage = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isImage = true; // one of the file extensions found
		} 
	}

	if (!_isImage) { 
		if (message!='') alert(message);
		formElement.focus();
	}

	return _isImage;
}

function isImageVal(formElement) {
	var OK = new Array ("gif","jpg","jpeg","png","wbmp");  // if you change this element, please change the config.php

	if (formElement == '') {
		return true;
	}

	var ext = getExt(formElement);

	_isImage = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isImage = true; // one of the file extensions found
		} 
	}

	return _isImage;
}

function isVideo(formElement, message) {
	var OK = new Array ("3gp","asf","asx","avi","mov","mp4","mpg","qt","rm","swf","wmv");  // if you change this element, please change the config.php

	trimElement = trim(formElement.value);
	if (trimElement == '') {
		return true;
	}

	var ext = getExt(trimElement);
	_isDocument = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isDocument = true; // one of the file extensions found
		} 
	}

	if (!_isDocument) { 
		if (message!='') alert(message);
		formElement.focus();
	}

	return _isDocument;
}

function isPhoneNumber(elementValue) {
	if (elementValue == '') return;
	var phoneNumberPattern = /^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$/;
	return phoneNumberPattern.test(elementValue);
}

function isZip(s) {
	if (s == '') return;
	// Check for correct zip code
	reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

	if (!reZip.test(s)) {
		alert("Zip Code Is Not Valid");
		return false;
	}

	return true;
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isDecimalKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 46) // Decimal Period
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isComaDecimalKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 44 || charCode == 46) // Coma or Decimal Period
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isDecPercentKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 37 || charCode == 46) // % or Decimal Period
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isQuantityKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 43 || charCode == 45) // + or - key
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isURL(s) {
	if(s.length > 0) {
		var v = new RegExp(); 
		v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
		if (!v.test(s)) { 
			return false; 
		} else {
			return true;
		}
	}
	return false;
}

function getExt(formElement) {
	var dot_pos = formElement.lastIndexOf(".");
	if(dot_pos == -1)
		return "";
	return formElement.substr(dot_pos+1).toLowerCase();
}

function validateEmail(email) {
	var splitted = email.match("^(.+)@(.+)$");

	if(splitted == null) return false;

	if(splitted[1] != null ) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user) == null) return false;
	}

	if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null) {
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		}// if
		return true;
	}

	return false;
}

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 Address")
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert("Invalid E-mail Address")
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail Address")
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail Address")
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail Address")
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail Address")
		return false
	}

	if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail Address")
		return false
	}

	return true					
}

function getURL() {
	// Get URL
	if (location.href.indexOf("https://")!= -1) 
		var prefix = "https://"; 
	else 
		var prefix = "http://"; 

	if (location.href.indexOf('//') !=-1) {
		firstpos = location.href.indexOf('//')+2;
		var tmpHref = location.href.substring(firstpos);
		if (tmpHref.indexOf('/') !=-1) {
			lastpos = tmpHref.indexOf('/');
			var strHref = prefix + tmpHref.substring(0, lastpos);
		} else {
			var strHref = location.href;
		}
	} else {
		if (location.href.indexOf('/') !=-1) {
			lastpos = location.href.indexOf('/');
			var strHref = prefix + location.href.substring(0, lastpos);
		} else {
			var strHref = prefix + location.href;
		}
	}

	return strHref;
}
function toggleBox(szDivID, iState) { // 1 visible, 0 hidden
	var obj = document.layers ? document.layers[szDivID] : document.getElementById ?  document.getElementById(szDivID).style : document.all[szDivID].style;
	obj.display = document.layers ? (iState ? "show" : "hide") : (iState ? "block" : "none");
}

