//The following variables r used to show chart
var CanDo = document.getElementById ? true : false;
var DocAllMode = document.all ? true : false;
var scrollX = 0;
var scrollY = 0;
var cX = 0; 
var cY = 0; 

function getNodeValue(xml, nodename)
{
	try
	{  
		var value = xml.getElementsByTagName(nodename)[0].firstChild.nodeValue;
		return URLDecode(value);
	}   
	catch (e)
	{
		return "";  
	}
}

function getRadioValue(obj)
{
	var rvalue = '';
	if(!obj) return rvalue;
	var rlength = obj.length; //single item radio button returns undefined on checking value
	if(rlength == undefined) 
	{
		rvalue = obj.value;
	}
	else
	{
		for (var i=0; i < rlength; i++)
	   	{
	   		if (obj[i].checked)
	      	{
	      		rvalue = obj[i].value;
	      	}
	   	}
	}
   return rvalue;
}

function selectValueInRadioButtons(radioObj, newValue) 
{
	if(!radioObj) return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) 
	{
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) 
	{
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) 
		{
			radioObj[i].checked = true;
		}
	}
}

function selectValueInList(list, value)
{
	for (i=0; i<list.options.length; i++)
	{
		if (list.options[i].value == value)
		{
			list.selectedIndex = i;
			return;
		}
	}
}

function isValueInList(list, value)
{
	for (i=0; i<list.options.length; i++)
	{
		if (list.options[i].value == value)
		{
			return true;
		}
	}
	return false;
}

function isTextInList(list, text)
{
	for (i=0; i<list.options.length; i++)
	{
		if (list.options[i].text == text)
		{
			return true;
		}
	}
	return false;
}

function deleteAllInList(list)
{
	var count = list.options.length;
	for (i=0; i<count; i++)
	{
		list.options[0] = null;
	}
	return false;
}

function trim(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function isTextAreaMaxLength(obj, maxlen)
{
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	if (obj.getAttribute && obj.value.length>mlength)
	{
		obj.value=obj.value.substring(0,mlength);
		return true;
	}
	else 
		return false;
}

function isValidPhone(phone)
{
	if (phone != '')
	{
		if (isNaN(parseFloat(phone)))
		{
			window.alert('Phone number should have digits only');
			return false;
		}
		else if (phone.charAt(0) == '0')
		{
			window.alert('Phone number should not have leading zeros');
			return false;
		}
		else 
			return true;
	}
	return true;
}

function isValidEmail(str)
{
	if (trim(str)=='')
	{
		return false;
	}
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1)
	{
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{		  	  
   	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{			  
 	    return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
   		 return false;
	}

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
   {
	     return false;
   }

   if (str.indexOf(dot,(lat+2))==-1)
   {
	     return false;
   }

  if (str.indexOf(" ")!=-1)
  {
       return false;
  }

  return true;					
}

function optionstostring(list)
{
		var str = '';
		for (i=0;i<list.options.length;i++)
		{
			str = str + list.options[i].value + 'itanyplacedelimiter';
		}
		return str;
}

function texttostring(list)
{
		var str = '';
		for (i=0;i<list.options.length;i++)
		{
			str = str + list.options[i].text + 'itanyplacedelimiter';
		}
		return str;
}

function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

function UpdateCursorPosition(e) {
	cX = e.pageX; cY = e.pageY;
}

function UpdateCursorPositionDocAll(e) {
	cX = event.clientX; cY = event.clientY;
}

if(DocAllMode) { 
	document.onmousemove = UpdateCursorPositionDocAll; 
}
else { 
	document.onmousemove = UpdateCursorPosition; 
}

function ScrollAmount() {
	if(self.pageYOffset) {
   		scrollX = self.pageXOffset;
   		scrollY = self.pageYOffset;
   	}
	else if(document.documentElement && document.documentElement.scrollTop) {
   		scrollX = document.documentElement.scrollLeft;
	   scrollY = document.documentElement.scrollTop;
   	}
	else if(document.body) {
   		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
   	}
}

function AssignPosition(d) {
	if(DocAllMode) { ScrollAmount(); }
	d.style.left = (cX+scrollX+10) + "px";
	d.style.top = (cY+scrollY+10) + "px";
}

function HideContent(d) {
	if(! CanDo) { return; }
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
	if(! CanDo) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
}

function ReverseContentDisplay(d) {
	if(! CanDo) { return; }
	var dd = document.getElementById(d);
	if(dd.style.display == "none") {
	   dd.style.display = "block";
	   }
	else { dd.style.display = "none"; }
}

function URLEncode( plaintext )
{
	//convert to string
	plaintext = plaintext + '';
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function isNumeric(sText)
{
   if(sText.length==0) { return false; }
   
   var ValidChars = "0123456789.-+";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function validateCurrentDate(startdate, type)
{
	var currentTime = new Date()
	window.alert
	var month = currentTime.getMonth() + 1
	if (month<10)
		month='0'+ month ; 
	var day = currentTime.getDate()
	if (day<10)
		day='0'+ day ; 
	var year = currentTime.getFullYear()
	var cd =    month + "/" + day + "/" + year;	
	var sd =startdate.split(' ')[0].split('-');
	var std = sd[1] + "/" + sd[2] + "/" + sd[0];	
	if(std < cd)
	{
		window.alert('Start date should be greater or equal to current date');
		return false;
	}
	
}

function checkMimeType(mimetype) {
	var mt = mimetype;
	if (mimetype=="audio/mp3")
	{
		var isWin = navigator.userAgent.toLowerCase().indexOf("windows") !=-1;
		if (isWin)
			mt = "application/x-mplayer2";
	}
	return mt;
}
