﻿/* 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);
}

var chip;

function pagestart()
{
 checkbrOK(); 
 chip=new Chip("popup",385,333);

 if(brOK) 
   { 
   		movechip("popup");
   }
}

function pagestop() {
	if(brOK) {
		stopme('popup'); 
	}
}

var brOK=false;
var mie=false;
var aver=parseInt(navigator.appVersion.substring(0,1));
var aname=navigator.appName;

function checkbrOK()
{if(aname.indexOf("Internet Explorer")!=-1)
   {if(aver>=4) brOK=navigator.javaEnabled();
    mie=true;
   }
 if(aname.indexOf("Netscape")!=-1)  
   {if(aver>=4) brOK=navigator.javaEnabled();}
}

var vmin=2;
var vmax=5;
var vr=2;
var timer1;

function Chip(chipname,width,height)
{this.named=chipname;
 this.vx=vmin+vmax*Math.random();
 this.vy=vmin+vmax*Math.random();
 this.w=width;
 this.h=height;
 this.xx=0;
 this.yy=0;
 this.timer1=null;
}

function movechip(chipname)
{
 if(brOK)
  {	
    pageX=window.document.body.scrollLeft;
     pageW=window.document.body.offsetWidth-8;
     pageY=window.document.body.scrollTop;
     pageH=window.document.body.offsetHeight;

   chip.xx=chip.xx+chip.vx;
   chip.yy=chip.yy+chip.vy;
   
   chip.vx+=vr*(Math.random()-0.5);
   chip.vy+=vr*(Math.random()-0.5);
   if(chip.vx>(vmax+vmin))  chip.vx=(vmax+vmin)*2-chip.vx;
   if(chip.vx<(-vmax-vmin)) chip.vx=(-vmax-vmin)*2-chip.vx;
   if(chip.vy>(vmax+vmin))  chip.vy=(vmax+vmin)*2-chip.vy;
   if(chip.vy<(-vmax-vmin)) chip.vy=(-vmax-vmin)*2-chip.vy;


   if(chip.xx<=pageX)
     {chip.xx=pageX;
      chip.vx=vmin+vmax*Math.random();
     }
   if(chip.xx>=pageX+pageW-chip.w)
     {chip.xx=pageX+pageW-chip.w;
      chip.vx=-vmin-vmax*Math.random();
     }
   if(chip.yy<=pageY)
     {chip.yy=pageY;
      chip.vy=vmin+vmax*Math.random();
     }
   if(chip.yy>=pageY+pageH-chip.h)
     {chip.yy=pageY+pageH-chip.h;
      chip.vy=-vmin-vmax*Math.random();
     }

	var mychip = document.getElementById(chipname);

  mychip.style.pixelLeft = chip.xx;
  mychip.style.pixelTop =chip.yy; 
  
   chip.timer1=setTimeout("movechip('"+chip.named+"')",100);
  }
}


function stopme(chipname)
{if(brOK)
  {//alert(chipname)
   eval("chip="+chipname);
   if(chip.timer1!=null)
    {clearTimeout(chip.timer1)}
  }
}



// swf is passwed without extension.
function createflash(width,height,swf) {	AC_FL_RunContent('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width',width,'height',height,'src',swf,'quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie',swf)
}

function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

// swf is passwed without extension.
function createflash(width,height,swf) {	AC_FL_RunContent('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width',width,'height',height,'src',swf,'quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie',swf)
}

function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}

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 = 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;
	}

}