// JavaScript Document
/*
	Created By  : Jansan John
	E-Mail      : jans4u@gmail.com
	Description : General Javascript Functions
*/
//--------------------------------------------------------------------------------------------------//

function keyDownHappen(e) {
	if(!e) var e = window.event;
	e.stopPropogation=true;
	return false;
}
//--------------------------------------------------------------------------------------------------//
/*
Return the id of the attribute.
*/
function $(id){return document.getElementById(id);}
//--------------------------------------------------------------------------------------------------//
/*
Return the array elements corressponding to the name of the attribute.
*/
function $N(name){return document.getElementsByName(name);}
//--------------------------------------------------------------------------------------------------//
function $NV(name)
{
	var elementsArray = document.getElementsByName(name);
	var elementsArraySize = elementsArray.length;
	var elementValues = new Array();
	
	for(var i=0; i<elementsArraySize; i++)
	{
		elementValues[i] = elementsArray[i].value;
		//elementValues.push(elementsArray[i].value);
	}
	return elementValues;
}
//--------------------------------------------------------------------------------------------------//
/*
Return the value of the attribute.
*/
function $V(id){return document.getElementById(id).value;}
//--------------------------------------------------------------------------------------------------//
// system error hiding
//window.onerror = SymError;
function SymError()
{
  return true;
}

//--------------------------------------------------------------------------------------------------//
/*
check the string is an integer or not.
*/
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {  
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

//--------------------------------------------------------------------------------------------------//
/*
strip the chars in the 'bag' from the passing string
*/
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {  
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

//--------------------------------------------------------------------------------------------------//
/*
strip the chars not in the 'bag' from the passing string
*/
function stripCharsNotInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {  
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) >= 0) returnString += c;
    }
    return returnString;
}

//--------------------------------------------------------------------------------------------------//

function restrictCharacters(type,id) {
	var str = document.getElementById(id).value;
	document.getElementById(id).value = "";
	
	if(type == 'integer') {
		var filteredString = stripCharsNotInBag(str,'1234567890');
		if(isInteger(filteredString)) {
			document.getElementById(id).value = filteredString;
		}
	}
}



function numonly(myfield, e, dot)
{
	var key;
	var keychar;
	
	if (window.event) {
	   key = window.event.keyCode;
	} else if (e){
	   key = e.which;
	} else {
	   return true;
	}
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){
	   return true;
	} else if ((("0123456789.").indexOf(keychar) > -1)) {
	   return true;
	} else {
	   return false;
	}
}



function substr_count(string,substring,start,length)
{
	 var c = 0;
	 if(start) { string = string.substr(start); }
	 if(length) { string = string.substr(0,length); }
	 for (var i=0;i<string.length;i++)
	 {
	  if(substring == string.substr(i,substring.length))
	  c++;
	 }
	 return c;
}

//--------------------------------------------------------------------------------------------------//
// concate the passing argumens
// USAGE : myConcat(',','1','2','3','4')
function myConcat(separator) {
  var result = "";
  // iterate through non-separator arguments
  for (var i = 1; i < arguments.length; i++) {
    result += arguments[i] + separator;
  }
  return result;
}






