/*-----------------------------------*/
/* Requires: Prototype 1.5 or higher */
/*-----------------------------------*/


//////////////////////////////////////////////////
//////////////// VALIDATE STRING /////////////////
//////////////////////////////////////////////////

function validateString(field, len, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateString`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO';}
	if(len==''||len==undefined){len=3;}
	if(msg==''||msg==undefined){msg='Invalid text. Please check it';}

	if(allowNull=='NO'){
		if(field.value.length < len || field.value == null || /^\s+$/.test(field.value)){
			field.addClassName('errorInput');
			field.activate();
			alert(msg);
			return false;
		}else{
			field.removeClassName('errorInput');
			return true;
		}	
	}
}

//////////////////////////////////////////////////
//////////////// VALIDATE NUMERIC /////////////////
//////////////////////////////////////////////////

function validateNumeric(field, len, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateNumeric`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO';}
	if(msg==''||msg==undefined){msg='Just numbers allowed. Please check it';}
	if(len==''||len==undefined){len = 0}
		
	if(allowNull=='NO'){
		var regex = /^\d+$/;
		if(field.value.length < len){
			field.addClassName('errorInput');
			field.activate();
			alert('You must type at least '+ len +' numbers');
			return false;
		}else{
			if(regex.test(field.value)==false){
				field.addClassName('errorInput');
				field.activate();
				alert(msg);
				return false;
			}else{
				field.removeClassName('errorInput');
				return true;
			}

		}
		
	}
}


//////////////////////////////////////////////////
//////////////// VALIDATE NUMERIC /////////////////
//////////////////////////////////////////////////

function validateFloat(field, len, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateNumeric`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO';}
	if(msg==''||msg==undefined){msg='Just numbers allowed. Please check it';}
	if(len==''||len==undefined){len = 0}
		
	if(allowNull=='NO'){
		var regex = /^[-+]?\d{1,2}(\.\d{1,2})?$/;
		if(field.value.length < len){
			field.addClassName('errorInput');
			field.activate();
			alert('You must type at least '+ len +' numbers');
			return false;
		}else{
			if(regex.test(field.value)==false){
				field.addClassName('errorInput');
				field.activate();
				alert(msg);
				return false;
			}else{
				field.removeClassName('errorInput');
				return true;
			}

		}
		
	}
}


//////////////////////////////////////////////////
//////////////// VALIDATE PATH ///////////////////
//////////////////////////////////////////////////

function validatePath(field, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validatePath`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO';}
	if(msg==''||msg==undefined){msg='Invalid file/folder path. Please check it';}

	if(allowNull=='NO'){
		if (field.value.length < 1){
			field.addClassName('errorInput');
			field.activate();
			alert('Please pick the file/folder');
			return false;
		}
		var regex = /^(.*?\/|.*?\\)?([^\./|^\.\\]+)(?:\.([^\\]*)|)$/;
		var path = field.value;
		if(regex.test(path) == false){
			field.addClassName('errorInput');
			field.activate();
			alert(msg);
			return false;
		}else{
			field.removeClassName('errorInput');
			return true;
		}
	}
}


//////////////////////////////////////////////////
////////////// VALIDATE DATE FORMAT //////////////
//////////////////////////////////////////////////

function validateDateFormat(field, format, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateDateFormat`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO';}
    if(format==''||format==undefined){format='YYYY-MM-DD'}
	if(msg==''||msg==undefined){msg='Invalid date format. Please use '+format;}

    switch(format){
        case 'YYYY-MM-DD':
            regex = /(19|20)\d\d([- \/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])/;
            break;
        case 'MM-DD-YYYY':
            regex = /(0[1-9]|1[012])([- \/.])(0[1-9]|[12][0-9]|3[01])\2(19|20)\d\d/;
            break;
        case 'DD-MM-YYYY':
            regex = /(0[1-9]|[12][0-9]|3[01])([- \/.])(0[1-9]|1[012])\2(19|20)\d\d/;
            break;
    }
    var dateStr = field.value;

    if(allowNull=='NO'){
		if (field.value.length < 1){
			field.addClassName('errorInput');
			field.activate();
			alert('Please fill in the date field');
			return false;
		}
		if(regex.test(dateStr) == false){
			field.addClassName('errorInput');
			field.activate();
			alert(msg);
			return false;
		}else{
			field.removeClassName('errorInput');
			return true;
		}	
	}
}


//////////////////////////////////////////////////
///////////// VALIDATE TIME FORMAT ///////////////
//////////////////////////////////////////////////

function validateTimeFormat(field, format, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateTimeFormat`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO';}
    if(format==''||format==undefined){format='24'}
	if(msg==''||msg==undefined){msg='Invalid time format. Please use '+format+ 'Hr. format';}

    switch(format){
        case '12':
            regex = /([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$/;
            break;
        case '24':
            regex = /([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])/;
            break;
    }
	if(allowNull=='NO'){
		if (field.value.length < 1){
			field.addClassName('errorInput');
			field.activate();
			alert('Please fill in the time field');
			return false;
		}
		var timeStr = field.value;
		if(regex.test(timeStr) == false){
			field.addClassName('errorInput');
			field.activate();
			alert(msg);
			return false;
		}else{
			field.removeClassName('errorInput');
			return true;
		}	
	}
}


//////////////////////////////////////////////////
////////////// VALIDATE IP FORMAT ////////////////
//////////////////////////////////////////////////
/**
 * Validate IP formatting from 0.0.0.0 to 255.255.255.255
 */
function validateIPFormat(field, msg, allowNull, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateTimeFormat`');} /* DEBUG */
	}
	if(allowNull==undefined){allowNull='NO'}
	if(msg==''||msg==undefined){msg='Invalid IP format. Please use XXX.XXX.XXX.XXX';}

    var regex = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;

    if(allowNull=='NO'){
		if (field.value.length < 1){
			field.addClassName('errorInput');
			field.activate();
			alert('Please fill in the IP Address field');
			return false;
		}
		var timeStr = field.value;
		if(regex.test(timeStr) == false){
			field.addClassName('errorInput');
			field.activate();
			alert(msg);
			return false;
		}else{
			field.removeClassName('errorInput');
			return true;
		}
	}else{
        return true;
    }
}


//////////////////////////////////////////////////
////////////// VALIDATE SAME VALUE ///////////////
//////////////////////////////////////////////////

function validateSameValue(fieldA, fieldB, msg, debug){
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(fieldA==undefined){alert('** Hey Developer: No `fieldA` reference passed!! ** \n Function: `validateSameValue`');} /* DEBUG */ 
		if(fieldB==undefined){alert('** Hey Developer: No `fieldB` reference passed!! ** \n Function: `validateSameValue`');} /* DEBUG */
	}
	if(msg==''||msg==undefined){msg='Both fields must match. Please check it';}

	if(fieldA.value != fieldB.value){
		fieldA.addClassName('errorInput');
		fieldB.addClassName('errorInput');
	    fieldB.activate();
	    alert(msg);		  
        return false;	
	}else{
		fieldA.removeClassName('errorInput');
	    fieldB.removeClassName('errorInput');
	    return true;
	}
}


//////////////////////////////////////////////////
//////////////// VALIDATE MIN SIZE ///////////////
//////////////////////////////////////////////////

function validateMinSize(field, minSize, msg, allowNull, debug) {
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateMinSize`');} /* DEBUG */ 
	}
	if(allowNull==undefined){allowNull='NO';}
	if(minSize==''){minSize=3;}
	if(msg==''||msg==undefined){msg='Invalid text. Please check it';}
	
	if(allowNull=='NO'){
		if (field.value.length < minSize){
			field.addClassName('errorInput');
			field.activate();
			alert(msg);
			return false;
		}else{
			field.removeClassName('errorInput');
			return true;
		}
	}
}


//////////////////////////////////////////////////
///////////////// VALIDATE COMBO /////////////////
//////////////////////////////////////////////////

function validateCombo(field, msg, debug) {
	if(debug){
		alert('-------------------\n'+'*DEBUG MODE*\n'+'-------------------');
		if(field==undefined){alert('** Hey Developer: No field reference passed!! ** \n Function: `validateCombos`');} /* DEBUG */
	}
	if(msg==''||msg==undefined){msg='Please select an option';}

	if ((field.value=='(-)') || (field.value == '')) {
		field.addClassName('requiredInput');
		field.activate();
		alert(msg);
		return false;
	}else{
	    field.removeClassName('requiredInput');
	    return true;
	}
}

//////////////////////////////////////////////////
///////////////// VALIDATE EMAIL /////////////////
//////////////////////////////////////////////////

function validate_email(field, alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
			alert(alerttxt);
			field.activate();
			return false;
		}else{
			return true;
		}
	}
}
