//This function is used to select and unselect all the check box of the given name.
//fmobj id	= 	the form oblect
//chekname	=	The checkbox name to be select and unselect.
function chk(frm)
{	
	var email_chk=/(^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$)/i;
	var msg='Please enter valid data in the following fields\n--------------------------------------------------------------------';
	//Fields marked (*) are mandatory\n';
	var foc=-1;
	if(typeof fields != "undefined"){
		for(i=0; i<fields.length; i++){
			var field = eval('frm.'+fields[i]);
			var field_value = getFieldValue(field);
			if(empty(field_value)){
				if(!field.disabled) { 
					if(foc==-1)foc=fields[i];
					msg+='\n'+msgs[i];
				}
			}
		}
	}
	if(typeof emails != "undefined"){
		for(j=0; j<emails.length; j++){
			var field_value = eval('frm.'+emails[j]+'.value');
			if(!email_chk.test(field_value) && !empty(field_value) ){
				if(foc==-1)foc=emails[j];
				msg+='\n'+email_msgs[j];
			}
		}
	}
	if(typeof nums != "undefined")
	{

		for(k=0; k<nums.length; k++)
		{
			var field_value = eval('frm.'+nums[k]+'.value');
			if (typeof num_zero!='undefined')
			{
					if ((num_zero==0))
					{
						if(!empty(field_value) && (isNaN(field_value) || (field_value <= 0))){
							if(foc==-1)foc=nums[k];
							msg+='\n'+nums_msgs[k];
						}
					}
					else
					{
						if(!empty(field_value) && (isNaN(field_value)))
						{
							if(foc==-1)foc=nums[k];
							msg+='\n'+nums_msgs[k];
						}
					}
			}
			else
			{
					if(!empty(field_value) && (isNaN(field_value) || (field_value <= 0))){
							if(foc==-1)foc=nums[k];
							msg+='\n'+nums_msgs[k];
						}

			}
		}
	}
	if(foc!=-1)
	{
		alert(msg);
		var fieldType = eval('frm.' + foc + '.type');
		if(fieldType && fieldType!='hidden' && fieldType!='radio' && fieldType!='checkbox')
		{
			eval('frm.'+foc+'.focus()')
		}		
		return(false);
	} else {
		return(true);
	}
}

function empty(str)
{
	var empty_chk=/(^[\ ]*$)/i;
	if(!empty_chk.test(str))return false;
	return(true);
}

function getFieldValue(field)
{
   switch(field.type)
   {
      case "text" :
      case "textarea" :
      case "password" :
      case "hidden" :
         return field.value;
      case "select-one" :
         var i = field.selectedIndex;
         if (i == -1)   return "";
         else   return field.options[i].value ;
		 /*return (field.options[i].value == "") ? field.options[i].text : field.options[i].value;*/
      case "select-multiple" :
         var allChecked = new Array();
         for(i = 0; i < field.options.length; i++)
            if(field.options[i].selected)
               allChecked[allChecked.length] = (field.options[i].value == "") ? field.options[i].text : field.options[i].value;
         return allChecked;
      case "button":
      case "reset":
      case "submit":
         return "";
      case "radio" :
      case "checkbox" :
         if (field.checked) { return field.value; } else { return ""; }
      default :
         if(field[0].type == "radio")
         {
            for (i = 0; i < field.length; i++)
               if (field[i].checked)
                  return field[i].value;

            return "";
         }
         else if(field[0].type == "checkbox")
         {
            var allChecked = new Array();
            for(i = 0; i < field.length; i++)
               if(field[i].checked)
                  allChecked[allChecked.length] = field[i].value;
            return allChecked;
         }
         break;
   }
   
   return "";
}
//This function can be used with listing pages like category listing in admin side for Mass Update.
function CheckCheckAll(fmobj,chekname) {
	
var TotalBoxes = 0;
var TotalOn = 0;
for (var i=0;i<fmobj.elements.length;i++)
	{
	var e = fmobj.elements[i];
	if(fmobj.select_all.checked==true)
		{
			if(e.name==chekname)
			{e.checked=true;}
		}
	else
		{
				if(e.name==chekname)
				{e.checked=false;}
		}
	}
}
function IsNumeric(sText)

{
   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 to display image of real size

function popUp(url) {
	//alert(url);
window.open(url, "name_of_window","width=800,height=650,left=20,top=20,scrollbars=yes,menubar=no, resizable=no,location=no,toolbar=no");
}
function emailCheck(email)
{
	var email_chk=/(^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$)/i;
	if(email_chk.test(email))
	{
		return true;	
	}
	else
	{
		return false;
	}
}
function lcaseCheck(e) {
	key = getKeyCodeVal(e);
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
	else if ((key > 96 && key < 123) || (key > 47 && key < 58))
		return true;
	else
		return false;
}
function getKeyCodeVal(e) {
 if (window.event)
    return window.event.keyCode;
 else if (e)
    return e.which;
 else
    return null;
}
function trim(str) {     if(!str || typeof str != 'string')         return null;     return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' '); }


/**
*
*  URL encode / decode
*
**/

var UrlUtfEnDec = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
