/**
 * Functions for form at javascript
 *
 * This is the javascript file for forms functions and checks
 * @author David Vélez <david.velez@entersoftweb.com>
 * @version 1.0
 * @package javascript
 */

function mask(id, mtype, opc){
     var regExp = null;
     if(mtype == "numeric"){
          regExp = /\d/
          // Option 1: Align rigth
          if(opc & 1) $("#" + id).css("text-align","right");
          $("#" + id).keypress(function(e){
               e=e||window.event;
               var key=e.charCode||e.keyCode||e.which;
               if ((key>=48 && key<=57) || ((key == 46 || key < 41) && key != 32)) return true ;
               return false;
          });
     }else if(mtype == "phone"){
          regExp = /[\+\d]/
		// Option 1: Align rigth
          if(opc & 1) $("#" + id).css("text-align","right");
          $("#" + id).keypress(function(e){
               e=e||window.event;
               var key=e.charCode||e.keyCode||e.which;
               if ((key>=48 && key<=57) || key == 43 || ((key == 46 || key < 41) && key != 32)){
                    if(key == 43){
                         if(!$("#" + id).attr("value") || !($("#" + id).attr("value").match(/\+/))) return true;
                         else return false;
                    }else if(key>=48 && key<=57){
                         if(!$("#" + id).attr("value")) $("#" + id).attr("value", "+");
                         else if(!($("#" + id).attr("value").match(/\+/))) $("#" + id).attr("value", "+" + $("#" + id).attr("value"));
                         return true;
                    }else return true;
               }else return false;
          });
     }else if(mtype == "email"){
          regExp = /[\da-zA-Z-._@]/
          $("#" + id).css("text-transform","lowercase");
          $("#" + id).keypress(function(e){
               e=e||window.event;
               var key=e.charCode||e.keyCode||e.which;			
               if ((key>=48 && key<=57) || (key>=65 && key<=90) || (key>=97 && key<=122) || key < 41 || key == 45 || key == 95 || key == 46 || key == 64){
                    if(key == 64){
                         if(!($("#" + id).attr("value").match(/@/))) return true;
                         else return false;
                    }else     return true;
               }
               return false;
          });
     }
     // Initial validation
     $("#" + id).attr("value", validate($("#" + id).attr("value"), regExp));

     // Event to control
     $("#" + id).focus(function(){
          // Initial correction
          if(mtype == "phone"){
               if($("#" + id).attr("value") && !($("#" + id).attr("value").match(/\+/))) $("#" + id).attr("value", "+" + $("#" + id).attr("value"));
          }
          $(this).attr("value", validate($(this).attr("value"), regExp));
     });
     $("#" + id).blur(function(){
          // Final correction
          if(mtype == "phone"){
               if($("#" + id).attr("value") && !($("#" + id).attr("value").match(/\+/))) $("#" + id).attr("value", "+" + $("#" + id).attr("value"));
          }
          $(this).attr("value", validate($(this).attr("value"), regExp));
     });

     // Validate function
     function validate(test, regExp){
          var buffer = "";
          if(test){
               var pos=0;
               while(pos++<test.length){
                    if(test.charAt(pos-1).match(regExp)) buffer+=test.charAt(pos-1);
               }
               return buffer;
          }else return buffer;
     }
}

// Mail regular expression check
function checkMail(temail){
    remail = /.+@.+\..+/ ;
	if(temail.search("@") < 0) return 1;
	else if(temail.search(remail) < 0) return 2;
	else return 0;
}

// Check field
function checkField(id, type){
     var returnValue = false;
     // Check a standar data (if exist)
     if(type == "exist"){
          if(!$("#" + id).attr("value")){
               $("#" + id).attr("class", "input_error");
               returnValue = id;
          }else{
               $("#" + id).attr("class", "");
          }
     }
     // Check a mail type
     else if(type == "email"){
          if(!$("#" + id).attr("value") || checkMail($("#" + id).attr("value"))){
               $("#" + id).attr("class", "input_error");
               returnValue = id;
          }else{
			$("#" + id).attr("class", "");
          }
     }
     // Check a selector type
     else if(type == "selector"){
          if(parseInt($("#" + id).attr("value")) <= 0){
               $("#" + id).attr("class", "input_error");
               returnValue = id;
          }else{
               $("#" + id).attr("class", "");
          }
     }     

     return returnValue;
}
