﻿/* handy functions for html form */

// Get form element value.
function gv(id) {
	var o = document.getElementById(id);
	if ( o == null ) {
		alert("Invalid Id: " + id);
		return "";
	}
	return o.value;	
}

// Set form element value.
function sv(id, v) {
	var o = document.getElementById(id);
	if ( o == null ) {
		alert("Invalid Id: " + id);
		return "";
	}
	o.value = v;
}

function radio_index(name) {
	var o =	document.getElementsByName(name);
	if ( o == null ) {
		alert("Invalid Id: " + id);
		return "";
	}
	for(i=0; i<o.length;i++) {
		if ( o[i].checked ) return i;
	}
	return -1;
}

function is_alphanumeric(alphane) {
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++) {
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if((hh >= 48 && hh <=57) || (hh >=65 && hh<=90) || (hh >=97 && hh <=122) || hh == 95) {		  
		}
		else {
			return false;
		}
	}
	return true;
}

function is_email(x) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	return false;
}

/* input date format: yyyy-mm-dd */
function is_date(d)
{
	if ( ! ( /^\d{1,4}-\d{1,2}-\d{1,2}$/.test(d) ) ) return false;

	var monthLength = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

	var vv = d.split("-");
	var day = parseInt(vv[2]);
	var month = parseInt(vv[1]);
	var year = parseInt(vv[0]);
	if (!day || !month || !year)return false;

	// check for year.
	if ( year < 0 || year > 9999 ) return false;
	// check for month.
	if ( month < 1 || month > 12 ) return false;
	// check for day.
	if ( day < 1 || day > 31 ) return false;
	if (year/4 == parseInt(year/4)) monthLength[2] = 29;
	if (day > monthLength[month]) return false;
	return true;
}



// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

/* ============================= */

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function nav_pulldown(list){
var w=open("","horoscopes","width=700,height=500");
w.document.location.href=list.options[list.selectedIndex].value;
}

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_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 MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function setCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start !=-1)
		{ 
			c_start =c_start + c_name.length +1; 
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end ==-1) c_end =document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
			} 
	  }
	return null;
}



function highlight_error(id) {
	var highlight_color = "#FFFF99";
	document.getElementById(id).style.backgroundColor= highlight_color;

}



function undo_highlight_error(id) {

	document.getElementById(id).style.backgroundColor= "#FFFFFF";

}

function checkLen(id, min, max) {
	//pass id's name only eg. "name_ch" , not object

	var len = trim(document.getElementById(id).value).length;
	
	if ( min != null ) {

		if ( len < min ) {
			highlight_error(id);
			return false;
		
		}else {
			undo_highlight_error(id);
		}
	}

	
	if ( max != null ) {

		if ( len > max ) {
			highlight_error(id);
			return false;

		}

		else {
			undo_highlight_error(id);

		}

	}
	return true;
}

function is_integer(id) {

	var v;

	v = document.getElementById(id).value;

	if (v == "")

	{
		undo_highlight_error(id);
		return true;
	}
	else if ( isNaN(v) )
	{
		highlight_error(id);
		return false;
	}
	else
	{
		undo_highlight_error(id);
		return true;
	}

}

function checkRadioChkBox(name) {

	var radio_chk = false;

	var r;

	var id = 'id_'+name ;

	r = document.getElementsByName(name);

	for (i = 0; i < r.length; i++)

	{

		if (r[i].checked){

			radio_chk = true;

			break;

		}

	}



	if (! radio_chk) { 

		//document.getElementById('id_'+name).style.backgroundColor= highlight_color;

		highlight_error(id);

		return false;

	}

	else { 

		undo_highlight_error(id);

		return true;

	}

}

//name="checkbox_name[]"
function checkChkBoxCount(name,min,max) {

	var radio_chk = false;

	var r,count=0;

	var id = 'id_'+name ;

	r = document.getElementsByName(name);

	for (i = 0; i < r.length; i++)

	{

		if (r[i].checked){

			count++;

		}

	}

	if ( min != null ) {

		if ( count < min ) {
			highlight_error(id);
			return false;
		
		}else {
			undo_highlight_error(id);
		}
	}

	
	if ( max != null ) {

		if ( count > max ) {
			highlight_error(id);
			return false;

		}

		else {
			undo_highlight_error(id);

		}

	}
	return true;

}