// var emailvalidate = "Please enter email address";


/*This javascript file contais all the default validation functions which are used for validation in entire system.*/
var _inputFocus='';
var fadeOutSec = 5000;
//validateMsgShow function is useful to display validation message.
//Arguments. 1.id of field, 2.validation message. 3.If the array for focus is passed then the id will be pushed in it otherwise focussed on the field itself.
function validateMsgShow(id,msg){
	// $("#"+id).focus();
	$("#error_"+id).show().fadeOut(fadeOutSec);
	$("#error_"+id).html(msg);
}

function matchPasswordValidate(newpwd,confpwd,msg){
	if($.trim($("#"+newpwd).val()) != $.trim($("#"+confpwd).val())){
		validateMsgShow(confpwd,msg);
		return 1;
	}else{
		return 0;
	}
}

function requiredValidateByLength(id,msg){
	if($('#'+id).val() != '' && $('#'+id).val() != null)
	{
		var idLength = $('#'+id).val().length;
		if(idLength == '0' || idLength == 0){
			validateMsgShow(id,msg);
			return 1;
		}else{
			return 0;
		}
	}else{
		validateMsgShow(id,msg);
		return 1;
	}
}

function requiredValidateCheckedByName(id,msg){
	var chekedchkbox = $('[name="'+id+'[]"]:checked').length;
	if(chekedchkbox == '0' || chekedchkbox == 0){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}

function requiredValidateNumber(id,msg){
	if($('#'+id).val() == '' || $('#'+id).val() <= '0' || $('#'+id).val() <= 0){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}

function checkCaptchaValidation(length,id,msg)
{
	if(length == 0)
	{
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}

function checkAmountzero(id,msg){
	if($('#'+id).val() != '' && ($('#'+id).val() == '0' || $('#'+id).val() == 0)){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}

/*function compareDiscountPrice(realval,newval,chk='g',msg)
{
	if(chk == 'g')
	{
		if($("#"+newval).val() > $("#"+realval).val()){
			validateMsgShow(newval,msg);
			return 1;
		}else{
			return 0;
		}
	}else if(chk == 'l')
	{
		if(($("#"+realval) >= 0 && $("#"+newval) >= 0) && ($("#"+newval).val() < $("#"+realval).val())){
			validateMsgShow(newval,msg);
			return 1;
		}else{
			return 0;
		}
	}else if(chk == 'e')
	{
		if(($("#"+realval) >= 0 && $("#"+newval) >= 0) && ($("#"+newval).val() == $("#"+realval).val())){
			validateMsgShow(newval,msg);
			return 1;
		}else{
			return 0;
		}
	}
}*/

//required_validate() is useful in required field validation. Arguments => 1. Id of field, 2. validation message.
function requiredValidate(id,msg){
	if($.trim($("#"+id).val()) == ''){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}

function requiredValidateDOB(id,msg){

	var dob = $("#"+id).val();
    var pattern = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
	
	if (dob == null || dob == "" || !pattern.test(dob)) {
       	validateMsgShow(id,msg);
		return 1;
    }
    else {
        return 0;
    }
	
}

function requiredValidateMobileLength(id,msg){
	if($.trim($("#"+id).val()).length  != 8 ){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}

function validateMsgHide(id){
	$("#error_"+id).hide();
}

function validateYouTubeUrl(url, msg,link)
{

    if (url != undefined || url != '') {
        var regExp = /^(https?\:\/\/)?((www\.)?youtube\.com|youtu\.?be)\/.+$/;
        var match = url.match(regExp);
        if (match) {
        	return 0;

        }
       else {

        	validateMsgShow(url,msg);
        	return 1;
        }
    }
}



function requiredValidateckdior(id,msg){
	var messageLength = CKEDITOR.instances[id].getData().replace(/<[^>]*>/gi, '').length;
	if(!messageLength){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}
function requiredPasswordCheck(idO,idN,msg){
	if($("#"+idO).val() == idN){
		validateMsgShow(idO,msg);
		return 1;
	}else{
		return 0;
	}
}

//regexValidate() is useful in required field and regular expression validation. Arguments => 1. Id of field, 2. Required field validation message. 3. Regular expression validation message. 4. Type of validation. Example: email, password. 5.regex_only -> If regex_only=yes then only regular express validation will be checked otherwise required as well as regular expression both validation will be checked.
function regexValidate(id,msg1,msg2,type,regexOnly){
	if(type=='email'){
		var regex =  /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	}else if(type=='password'){
		var  regex =/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$/;
	}else if(type=='special'){
		var  regex =/^\s*[a-zA-Z0-9-_,\s]+\s*$/;
	}else if(type=="positive integer"){
		var regex =/^\+?(0|[1-9]\d*)$/;
	}else if(type=="amount"){
		var regex =/^([0-9]+(?:[\.][0-9]*)?|\.[0-9]+)$/;
	}else if(type=="decimal"){
		var regex = /^\d{0,1}(\.\d{0,4})?$/;
	}else if(type=="numpoint"){
		var regex = /^\d{0,4}(\.\d{0,4})?$/;
	}else if(type=="postalcode"){
		var regex = /^\d{6}([ \-]\d{4})?$/;
	}else if(type=="longitude"){
		var regex = /^(\-?\d+(\.\d+)?)$/;
	}else if(type=="website"){
		var regex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;
	}else if(type=="phonum"){
		var regex = /^\+(?:[0-9] ?){6,14}[0-9]$/;
	}else if(type=="img"){
		var regex = /(?:jpg|jpeg|png|bmp|tif|JPG|JPEG|PNG|BMP|TIF)$/;
	}else if(type=="pdf"){
		var regex = /(?:pdf|PDF|DOC|doc|DOCX|docx)$/;
	}else if(type=="chkSpace"){
		var regex = /^[a-zA-Z .]+$/;
	}else if(type=="alphanum"){
		var regex = /^[a-zA-Z0-9]+$/;
	}else if(type=="space"){
		var regex = /^[s.match(/\w/]+$/;
	}else if(type=="alphaSpacenum"){
		var regex = /^[a-zA-Z0-9 ]+$/;
	}else if(type=="imgdoc"){
		var regex = /(?:jpg|jpeg|png|bmp|tif|JPG|JPEG|PNG|BMP|TIF|pdf|PDF|DOC|doc|DOCX|docx)$/;
	}

	if(requiredValidate(id,msg1) && (regexOnly != 'yes')){
		return 1;
	}else if($("#"+id).val()!='' && regex.test($("#"+id).val()) ==  false){
		validateMsgShow(id,msg2);
		return 1;
	}else{
		return 0;
	}
}



/* function for allow only number keys and one decimal */
/* onkeypress="return isFloat(event, id of input);" */
function isFloat(event,id){

	if((event.which < 46 || event.which > 59) && event.which!=13 && event.which!=8 && event.which!=0) {
        event.preventDefault();
    } // prevent if not number/dot


    if($("#"+id).val().indexOf('.') != -1) {

    	var num_val = $("#"+id).val();

    	var split_val = num_val.split('.');

    	if(split_val[1].length >= 2){
    		event.preventDefault();
    	}

    }

    if(event.which == 46 && $("#"+id).val().indexOf('.') != -1) {
        event.preventDefault();
    } // prevent if already dot
}

function notZeroNumber(id,msg){
			
	if($("#"+id).val()==0){
		validateMsgShow(id,msg);
		return 1;
	}else{
		return 0;
	}
}



/* function for allow number keys and some special characters (speciallu use for contact number validation) */
/* onkeypress="return onlyNumbersandSpecialChar(event);" */

function onlyNumbersandSpecialChar(evt) {

	var e = window.event || evt;
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57 || charCode > 107 || charCode > 219 || charCode > 221) && charCode != 40 && charCode != 32 && charCode != 41 && (charCode < 43 || charCode > 46) && charCode != 37 && charCode != 39) {

		if (window.event) //IE
			window.event.returnValue = false;
		else //Firefox
			e.preventDefault();
		}

	return true;
}
