/**
 * @package pearltea
 * @copyright Copyright 2009 StockStore Team
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: frmCheck.js - create by tankai 2007-10-19 
 */
function numbersonly(e){
	var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
	var actualkey=String.fromCharCode(unicode)

	//alert(actualkey);
	if(actualkey=='.' || actualkey=='-' || actualkey=='/')
		return true;
	if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
		if (unicode<48||unicode>57) //if not a number
		return false //disable key press
	}
	
}

function field_check(name, rule, field){
  	if(rule.indexOf("cpwd")>-1){
		strTmp = rule.split("#");
		if(strTmp[0].inc("cpwd","/") == true && field.value != $(strTmp[1]).value){
			return "O "+name+" deve conferir com seu " +name.split(" Confirmation")[0];
		}
	}	
	if(rule.inc("eml","/")==true && !/(\,|^)([\w+._]+@\w+\.(\w+\.){0,3}\w{2,4})/.test(field.value.replace(/-|\//g,"")) && !isNone(field.value)){
		return "Seu " + name + " está correto? Nosso sistema não pode entender seu endereço de e-mail.";
	}
	if(rule.inc("cpfj","/")==true && !cnpj_cpf(field.value) && !isNone(field.value)) {
			//alert(cpfj(field));
			return "Informação faltando, "+name+".";		
	}
	if(rule.inc("tel","/")==true && !/(^[0-9+-]{3,30}$)/.test(field.value) && !isNone(field.value)){
		return "Seu "+name+" está correto? Nosso sistema não pode entener seu formato de telefone.";
	}	
	size = rule.sub("min","/");
	if(size > 0 ){			
		if(field.value.trim().length < size && field.value.trim().length>0){
			return "Sua "+ name +" está correta? Nosso sistema aceita um mínimo de "+size+" charactéres.";
		}
	}		
	size = rule.sub("max","/");		
	if(size > 0 ){
		if(field.value.trim().length > size){
			return "Seu "+ name +" está correto? Nosso sistema aceita um máximo de "+size+" charactéres."
		}
	}	
	if(rule.inc("nnull","/") == true && isNone(field.value)){
		return "Informação faltando, "+name+".";		
	}	
	if(rule.inc("ischeck","/") == true && ! field.checked ){
		return name;
	}
	if(rule.inc("isselect","/") == true && field.value == '-1' ){
		return 'Escolha "' + name + '"';
	}	
	return "";
};

/*#表单验证#*/
function fmChk(fm){
	var name , rule , tmp , msgStr , size;
	if(fm == null || fm.tagName != "FORM"){
		alert("", null , "error");
	    return;
	}	
	for(i=0;i<fm.length;i++){		
		var msgStr = chkInput(fm[i]);				
		if(msgStr != "success"){
			efocu(fm[i]);
			msg(msgStr);
			return false;
		}
	}
	return true;
};

function chkInput(obj){
	var name , rule , msgStr;
	name=obj.getAttribute("chkName");
	rule=obj.getAttribute("chkRule");
	if(isNone(rule) || isNone(name))  return "success";	
	msgStr = field_check(name, rule, obj);
	
	if(msgStr != ""){
		return msgStr;
	}
	else{
		return "success";
	}
};

function msg(key){
	alert(key);
};

function trim1(str) {
	var specials = ['/', '.', '-'];
    return str.replace(new RegExp('(\\' + specials.join('|\\') + ')', 'g'), '');

}
 
/*function cnpj_cpf verifica qual das funcoes tem que chamar cpf ou cnpj*/

   function cnpj_cpf(documento1){

		if ((documento1.length < 11) || (documento1.length > 18)) 
		{ 
			return false; 
		} 
		//return valida_cpf(documento1);
        return (valida_cnpj(documento1) || valida_cpf(documento1));
   }

   function valida_cnpj(documento1){

         var i;
		var str = new String(documento1); 
		 str =  trim1(str);
		var c1 = str.substr(0,12);
         var dv = str.substr(12,2);
         var d1 = 0;
		var result = true;
         for (i = 0; i < 12; i++){
            d1 += c1.charAt(11-i)*(2+(i % 8));
         }

         if (d1 == 0){
            result = false;
         }
            d1 = 11 - (d1 % 11);

         if (d1 > 9) d1 = 0;

            if (dv.charAt(0) != d1){
               result = false;
            }

         d1 *= 2;
         for (i = 0; i < 12; i++){
            d1 += c1.charAt(11-i)*(2+((i+1) % 8));
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

            if (dv.charAt(1) != d1){
               result = false;
            }

		return result;	
   }


   function valida_cpf(documento1){

         var i;
		 var result = true;
		var str = new String(documento1); 
		 str =  trim1(str);
		var c1 = str.substr(0,9);
 	        var dv = str.substr(9,2);
         var d1 = 0;
		
         for (i = 0; i < 9; i++){
            d1 += c1.charAt(i)*(10-i);
         }

         if (d1 == 0){
            result = false;
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

         if (dv.charAt(0) != d1){
           result = false;
         }

         d1 *= 2;
         for (i = 0; i < 9; i++){
            d1 += c1.charAt(i)*(11-i);
         }

         d1 = 11 - (d1 % 11);
         if (d1 > 9) d1 = 0;

         if (dv.charAt(1) != d1){
            result = false;
         }

		return result;
   }


function initForm(el, func){	
	var img_path = baseURL + "includes/templates/debug_5/images/checkout";
	(new Image(10,10)).src = img_path + "/ico_F.gif";
	var error_bg = "#FEDFDF", blur_bg = "" , focus_bg = "#FFFEE1";
	var formId = $(el);
	if(formId == null || formId.tagName != "FORM"){
		alert("", null , "error");
	    return;
	}
	var addImage = '<img src="'+ img_path + "/ico_T.gif" +'" width="10" height="10" style="display:none;" class="pad_l"/>';
	var addDiv = '<div style="display:none;" class="red line_120"></div>';
	var elArr = formId.elements;
	var elLen = elArr.length;
	for(i=0; i<elLen; i++) {
		//insert the img and div
		var addHtml = elArr[i].getAttribute("chkRule");
		if(addHtml){
			insHtm(elArr[i],addImage);
			insHtm(elArr[i],addDiv);
			
			//add the mouse style have check;
			elArr[i].onfocus = function(){
					if((this.tagName === "INPUT" && (this.type === "text" || this.type === "password"))||this.tagName === "TEXTAREA")
					{this.style.backgroundColor = focus_bg;}
			}
			elArr[i].onblur = function(){
				this.style.backgroundColor = blur_bg;	
				var out_img = this.parentNode.getElementsByTagName("img")[0];
				var out_text = this.parentNode.getElementsByTagName("div")[0];
				var msg = chkInput(this);
				if(msg === "success") {
					out_img.src = img_path + "/ico_T.gif";
					out_img.style.display="inline";
					out_text.style.display="none";
				}else{
					out_img.src = img_path + "/ico_F.gif";						
					out_img.style.display="inline";
					out_text.innerHTML = msg;
					out_text.style.display="block";
					if((this.tagName === "INPUT" && (this.type === "text" || this.type === "password"))||this.tagName === "TEXTAREA"){
						this.style.backgroundColor = error_bg;
					}
				}
				if(func != null) {try{eval(func);}catch(e){}}						
			}
		}else{
				//add the mouse style not check;
			if((elArr[i].tagName === "INPUT" && (elArr[i].type === "text" || elArr[i].type === "password"))||elArr[i].tagName === "TEXTAREA"){
				elArr[i].onfocus = function(){this.style.backgroundColor = focus_bg;}
				elArr[i].onblur = function(){this.style.backgroundColor = blur_bg; if(func != null) {try{eval(func);}catch(e){}}}
			}
		}
	}
}