//******************************************************************************
//***                                                                        ***
//*** File       : common.js                                                 ***
//*** Author     : Edwin Poldervaart                                         ***
//*** Date       : 14-07-2006                                                ***
//*** Copyright  : (C) 2004 HawarIT BV                                       ***
//*** Email      : info@hawarIT.com                                          ***
//***                                                                        ***
//*** Description: Common Functionality                                      ***
//***                                                                        ***
//******************************************************************************


//*** Indicator for Internet Explorer ***
var gIExplorer = (navigator.appName.indexOf("Microsoft") != -1);


//********************************************************************************
//*** Local defines
//********************************************************************************
var controlRange = new Array(0,8,9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,44,45,46,112,113,114,115,116,117,118,119,120,121,122,123); //Control Key's like backspace delete etc.
var numRange  = "0123456789";
var phoneRange = '()+ /\\';
var emailRange = "@_-.";
var charRange = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var signRange = "_- ";


//********************************************************************************
//*** Element functions
//********************************************************************************
function getElement(key)
{
  //*** First check if specified key is element id.
  var elem = document.getElementById(key);
  
  if (!elem)
  {
    //*** Now check if specified key is element name.
    elem = document.getElementsByName(key);
    
    //*** Element array found, set element to first one.
    if (elem.length > 0) elem = elem[0];
  }
  
  if (!elem)
  {
    //*** Element not found, raise error message.
    alert("Could not find element '" + key + "'");
    
    return false;
  }
  
  return elem;
}

function getRadioValue(key)
{
	var elem = document.getElementsByName(key);
	var check = false;
	var passed = false;
  
	//*** Element not found.
	if (!elem)
	{
		alert("no key: " + key + " found!");
		return false;
	}
	else 
	{
		for(var i = 0; i < elem.length;i++)
		{
			if(elem[i].checked)
			{
				passed = true;
				check = true;
				return elem[i].value;
			}
		}
	}
	
	if(!check && !passed)
	{
		alert("unexpected error");
		return false;
	}
	else if(!check && passed)
	{
		alert("no selection is made");
		return false;
	}
}


function getElementAttrib(key, attrib)
{
  var elem = getElement(key);
  
  //*** Element not found.
  if (!elem) return false;
  
  //*** Return value of specified attribute.
  return elem[attrib];
}


function setElementAttrib(key, attrib, value)
{
  var elem = getElement(key);
  
  //*** Element not found.
  if (!elem) return false;
  
  //*** Set value of specified attribute.
  elem[attrib] = value;
}


//********************************************************************************
//*** String functions
//********************************************************************************
function LTrim(str, trimChar)
{
  //*** Trim specified character from the left.
  while (str.substring(0,1) == trimChar)
  {
    str = str.substring(1, str.length);
  }
  
  return str;
}


function RTrim(str, trimChar)
{
  //*** Trim specified character from the right.
  while (str.substring(str.length - 1, str.length) == trimChar)
  {
    str = str.substring(0, str.length - 1);
  }
  
  return str;
}


function Trim(str, trimChar)
{
  //*** Trim specified character from the left and right.
  str = LTrim(str, trimChar);
  str = RTrim(str, trimChar);
  
  return str;
}


//********************************************************************************
//*** Keyboard Functions
//********************************************************************************
function getASCIIKey(e)
{
  //*** IE or other event?
  //return (window.event) ? window.event.keyCode : e.which;
  return (gIExplorer) ? e.keyCode : e.which;
}


function getCharKey(e)
{
  //*** Return char.
  return String.fromCharCode(getASCIIKey(e));
}


function isAlpha(e)
{
  var key = getCharKey(e);
  
  return isValid(key, numRange + charRange, e);
}

function isValidField(e)
{
  var key = getCharKey(e);
  
  return isValid(key, numRange + charRange + signRange, e);
}

function isValidPhone(e)
{
  var key = getCharKey(e);
  
  return isValid(key, numRange + phoneRange, e);
}

function isValidEmail(e)
{
  var key = getCharKey(e);
  
  return isValid(key, numRange + charRange + emailRange, e);
}

function isNum(e)
{
	var key = getCharKey(e);
  
	return isValid(key, numRange, e);
}

function isChar(e)
{
  var key = getCharKey(e);
  
  return isValid(key, charRange, e);
}


function isValid(key, range, e)
{
  if ((key == "") || (range.indexOf(key, 0) >= 0) || isValidControl(e))
    //*** Key found!
    return true;
  else
    return false;
}

function isValidControl(e)
{
   var k = getASCIIKey(e);
   valid = false;
   //alert(k);
   for (var i = 0; i < controlRange.length ; i++)
   {
	   if(controlRange[i] == k)
	   {
		   valid = true;
	   }
   }

   return valid;
}


function CalcPrice(id,format,amount,out) {
	
	//alert(format + " > " + amount);
	var http = null;
    
	if (window.XMLHttpRequest)
	{
		http = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		http = new XMLHttpRequest();
	}

	
	http.open('get', 'php/calculator.php?format='+format+'&amount='+amount+'&id='+id);
    http.onreadystatechange = function handleResponse() {
								if(http.readyState == 4){
									var response = http.responseText;
									var update = new Array();

									if(response.indexOf('|' != -1)) {
										update = response.split('|');
										out.value = update[1];
									}
								}
							}
    http.send(null);
}
