/**
 *	Trims string
 *	@param str - string to trim
 *
 *	@return trimed string
 */
function trim(str){
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;

/**
 *	Generates Alert UI
 *      @param orbit - object name to look for or pointer to object
 *      @param msg - holds the message to display on error alert
 *	@param alertType - holds the alert type to generate (0-no alert,string format = 'm|p|b|f' [m-message,p-picture,b-border,f-focus])
 *
 */
function displayAlert(orbit,msg,alertType){   
    if(alertType!='' && alertType!=0){
        
        // checking if orbit is object pointer or object name
        if(typeof(orbit)!='object'){
            obj = document.getElementById(orbit);
            if(obj==null) return false; // if such object does not exists
        }
        else obj=orbit;
        var objName = obj.name;// retriving the objectName
        
        // @param alertTypeArray - holds the splitted alertType string
        var alertTypeArray = alertType.split('|');

        // Comensing all givven alert Types
        if('b' in inArray(alertTypeArray)) {          
                obj.style.border="1px solid #ff0000 ";
        }// end if 'b'

        if('m' in inArray(alertTypeArray)) {
            var messageAlert=document.getElementById(objName+"AlrtM");            
            if(messageAlert!=null){
                messageAlert.innerHTML=msg;
                if(messageAlert.style.display=='none') messageAlert.style.display='';
            }
            else return false;
       }// end if 'm'

       if('p' in inArray(alertTypeArray)) {
            var pictureAlert=document.getElementById(objName+"AlrtP");
            if(pictureAlert!=null){
                pictureAlert.style.display='';
            }
            else return false;
       }// end if 'p'

        if('f' in inArray(alertTypeArray)) {
                obj.focus();
        }// end if 'f'

        if('mi' in inArray(alertTypeArray)) { 
                obj.value=msg;				
                $('#'+obj.id).addClass('errorInput');
        }// end if 'f'

        return true
    }
    return false
}

/**
 *	Generates Alert UI
 *      @param orbit - object name to look for or pointer to object     
 *
 */
function clearAlert(orbit){ 
    if(orbit!=null){ 
        // checking if orbit is object pointer or object name
        if(typeof(orbit)!='object'){
            obj = document.getElementById(orbit.toString());
        }
        else obj=orbit;
        var objName = obj.name;// retriving the objectName
         
                 
        obj.style.border="1px #C9C7BA solid";        
        var messageAlert=document.getElementById(objName+"AlrtM");        
        if(messageAlert!=null){
            messageAlert.innerHTML='';
                if(messageAlert.style.display=='') messageAlert.style.display='none';
        }

        var pictureAlert=document.getElementById(objName+"AlrtP");
        if(pictureAlert!=null){
            pictureAlert.style.display='none';
        }
    }   
}

/**
 *	@description Checks if the object is empty
 *      @param orbit - object name to look for or pointer to object
 *	@param msg - holds the message to display on error alert
 *	@param alertType - holds the alert type to generate (0-no alert,string format = 'm|p|b|f' [m-message,p-picture,b-border,f-focus])
 *
 *	@return trimed string
 */
function reqFieldsByVal(orbit,msg,alertType){
    var obj; // Object holder    
    // checking if orbit is object pointer or object name
    if(typeof(orbit)!='object'){
        obj = document.getElementById(orbit);
        if(obj==null) return false; // if such object does not exists
    }
    else obj=orbit;

    var objVal = obj.value;

    objVal = trim(objVal);

    if (objVal == ""){
        displayAlert(obj,msg,alertType);
        return false;
    }
    return true;
}

function validateCredit(cardnumber,cardname,orbit,msg,alertType){
	var obj; // Object holder    
    // checking if orbit is object pointer or object name
    if(typeof(orbit)!='object'){
        obj = document.getElementById(orbit);
        if(obj==null) return false; // if such object does not exists
    }
    else obj=orbit;
	if(!checkCreditCard(cardnumber, cardname)){
		displayAlert(obj,msg,alertType);
        return false;
	}
	return true;
}

function validateID(orbit,msg,alertType){
	var obj; // Object holder    
    // checking if orbit is object pointer or object name
    if(typeof(orbit)!='object'){
        obj = document.getElementById(orbit);
        if(obj==null) return false; // if such object does not exists
    }
    else obj=orbit;

    var objVal = obj.value;

    objVal = trim(objVal);
	
	var temp = String(objVal);
	if ((temp.length != 9) || (isNaN(temp))){
		displayAlert(obj,msg,alertType);
        return false;
	}
	var mone = 0, incNum;
	for (var i=0; i < 9; i++)
	{
		incNum = Number(temp.charAt(i));
		incNum *= (i%2)+1;
		if (incNum > 9)
			incNum -= 9;
		mone += incNum;
	}
	//return (mone%10 == 0);
	
	if(!(mone%10 == 0)){
		displayAlert(obj,msg,alertType);
        return false;
	}
	return true;
}

/**
 *	Validates  Email, and generates alert by givven alertType
 *	@param orbit - object name to look for or pointer to object
 *	@param msg - holds the message to display on error alert
 *	@param alertType - holds the alert type to generate (0-no alert,string format = 'm|p|b|f' [m-message,p-picture,b-border,f-focus])
 *
 *	@return true on validation pass false visaversa.
 *	@see displayAlert()
 */
function validateEmail(orbit,msg,alertType){
    
    var obj; // Object holder

    // checking if orbit is object pointer or object name
    if(typeof(orbit)!='object'){
        obj = document.getElementById(orbit);
        if(obj==null) return false; // if such object does not exists
    }
    else obj=orbit;

    var emVal = obj.value; // collecting email

    if (emVal == '') return true;// no false found

    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|'; // invalid chars in email.
    for (i=0; i<invalidChars.length; i++) {
	   if (emVal.indexOf(invalidChars.charAt(i),0) > -1) {       
                
		displayAlert(obj,msg,alertType);
		return false;
	   }
    }
    for (i=0; i<emVal.length; i++){
       if (emVal.charCodeAt(i)>127) {
          
               displayAlert(obj,msg,alertType);
          return false;
         }
    }    
    var atPos = emVal.indexOf('@',0);
    var len = emVal.length;
    if (atPos == -1) {
       
	displayAlert(obj,msg,alertType);
	return false;
    }
    if (atPos == 0) {
       
        displayAlert(obj,msg,alertType);
        return false;
    }
    if (emVal.indexOf('@', atPos + 1) > - 1) {
        
        displayAlert(obj,msg,alertType);
        return false;
    }
    if (emVal.indexOf('.', atPos) == -1) {
        
        displayAlert(obj,msg,alertType);
        return false;
    }
    if (emVal.indexOf('.', len-1) != -1) {
       
        displayAlert(obj,msg,alertType);
        return false;
    }
    if (emVal.indexOf('@.',0) != -1) {
       
        displayAlert(obj,msg,alertType);
        return false;
    }
    if (emVal.indexOf('.@',0) != -1){
       
        displayAlert(obj,msg,alertType);
        return false;
    }
    if (emVal.indexOf('..',0) != -1){
        
        displayAlert(obj,msg,alertType);
        return false;
    }
	return true;
}


/**
 *	checks if some value in arr - [Use like:  "some in inArray(arr)"]
 *	@param arr - object name to look for or pointer to object
 *
 *	@return true if some value exists in arr false visaversa. 
 */
function inArray(arr)
{
    var o = {};
    for(var i=0;i<arr.length;i++)
    {
        o[arr[i]]='';
    }
    return o;
}

/**
 *	Alows to create number only input field
 *	@param e - event object
 *
 *	@return true value is numerical false visaversa.
 */
function numbersonly(e){
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);
	
  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==127) || (key==45) )
    return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;
  else
    return false;
}

/**
 *	Alows to create number only input field
 *	@param value - string representing number
 *
 *	@return true value is numerical false visaversa.
 */
function IsPositiveNumeric(value){
    var anum=/(^\d+$)|(^\d+\.\d+$)/
    if (anum.test(value))   return true;
    return false;
   }
