///////////////////////////////////////////////////////////////////////////////
// VDaemon PHP Library version 2.3.0
// Copyright (C) 2002-2004 Alexander Orlov and Andrei Stepanuga
//
// VDaemon client-side validation file
//
///////////////////////////////////////////////////////////////////////////////

function VDSymError()
{
	
  return true;
}
window.onerror = VDSymError;

var vdAllForms = new Array();
var vdForm = null;
var vdDelimiter = "~";
var ValidationStatus=true;
var SetFocusToControl=null;

function VDValidateForm(formName)
{	
	if (typeof(vdAllForms[formName]) == "undefined")
		return true;	
	var isPageValid = true;	
	if(document.getElementById('ClientValidate') != null){			
		if(document.getElementById('ClientValidate').value=="false"){
			isPageValid=true;		
		}
		else{					
			isPageValid = VDValidateFormorignal(formName);
		}
	}
	else{		
		isPageValid = VDValidateFormorignal(formName);
				
	}		
	
	return isPageValid;
}

function VDValidateFormorignal(formName)
{
	vdForm = vdAllForms[formName];
	var isPageValid = true;	 
	for (var key in vdForm.validators)
    {				
        VDValidateValidator(vdForm.validators[key]);        
        isPageValid = isPageValid && vdForm.validators[key].isvalid;
    }
    vdForm.isvalid = isPageValid;    

	VDUpdateLabels();    
    VDUpdateSummaries();    
	vdForm = null;    
    return isPageValid;
}

function VDValidateValidator(validator)
{			
    validator.isvalid = true;	
    switch (validator.type)
    {
        case "required":	

            validator.isvalid = VDEvaluateRequired(validator);
            break;
        case "length":
            validator.isvalid = VDEvaluateLength(validator);
            break;	
        case "checktype":
            validator.isvalid = VDEvaluateChecktype(validator);
            break;
        case "range":
            validator.isvalid = VDEvaluateRange(validator);
            break;
        case "compare":
            validator.isvalid = VDEvaluateCompare(validator);
            break;
        case "regexp":
            validator.isvalid = VDEvaluateRegExp(validator);
            break;
        case "email":
            validator.isvalid = VDEvaluateEmail(validator);
            break;
		case "url":
            validator.isvalid = VDEvaluateUrl(validator);
            break;
		case "www":
            validator.isvalid = VDEvaluateWWW(validator);
            break;
		case "weblo":
            validator.isvalid = VDEvaluateWeblo(validator);
            break;
		case "weblofloat":
            validator.isvalid = VDEvaluateWebloFloat(validator);
            break;	
		case "required2":
            validator.isvalid = VDEvaluateWebloRequired2(validator);
            break;	
		case "domain":
            validator.isvalid = VDEvaluateDomain(validator);
            break;
		case "zip":
            validator.isvalid = VDEvaluateZip(validator);
            break;
		case "postal":
            validator.isvalid = VDEvaluatePostal(validator);
            break;
		case "login":
            validator.isvalid = VDEvaluateLogin(validator);
            break;
		case "password":
            validator.isvalid = VDEvaluatePassword(validator);
            break;	
        case "custom":
            validator.isvalid = VDEvaluateCustom(validator);
            break;
		 case "phone":
            validator.isvalid = VDEvaluatePhone(validator);
            break;	
		case "mobile":
            validator.isvalid = VDEvaluateMobile(validator);
            break;	
		case "fax":
            validator.isvalid = VDEvaluateFax(validator);
            break;	
		case "group":
            validator.isvalid = false;
            for (var i in validator.items)
            {
                VDValidateValidator(validator.items[i]);
                validator.isvalid = validator.isvalid || validator.items[i].isvalid;
            }
            break;
    }
}

function VDUpdateLabels()
{
		
    if (typeof(vdForm.labels) == "undefined")
        return;
    var i, j;
    var count;
    count=0;
    var FirstVarName="";
    for (i in vdForm.labels)
    {
        var oLabel = vdForm.labels[i];
		
        var label = document.getElementById(oLabel.id);
        if (label != null)
        {
            var isValid = true;
            for (j in oLabel.validators)
            {
               var valName = oLabel.validators[j];                
               if(count==0){				  
               		FirstVarName=oLabel.validators[j];					 
               		count=1;
               }
                if (typeof(vdForm.validators[valName]) != "undefined")
                {
					
                    isValid = isValid && vdForm.validators[valName].isvalid;
					
                }
            }
			
            label.innerHTML = "";
            if (isValid)
            {
                label.innerHTML = oLabel.oktext;
                label.className = oLabel.okclass;
            }
            else
            {
                label.innerHTML = oLabel.errtext;                               
                label.className = oLabel.errclass;
            }
        }
    } 
						
    return FirstVarName;               
}

function VDUpdateSummaries()
{
    if (typeof(vdForm.summaries) == "undefined")
        return;

    for (var i in vdForm.summaries)
    {
        var headerSep, first, pre, post, last, s;
        var oSummary = vdForm.summaries[i];
        var summary = document.getElementById(oSummary.id);
        if (summary != null)
        {
            if (vdForm.isvalid)
            {
                //summary.innerHTML = oSummary.showsummary ? "&nbsp;" : "";
                summary.innerHTML = "";
                summary.style.display = "none";
            }
            else
            {
                if (oSummary.showsummary)
                {
                    switch (oSummary.displaymode)
                    {
                        case "list":
                        default:
                            headerSep = "<br>";
                            first = "";
                            pre = "";
                            post = "<br>";
                            last = "";
                            break;
                        case "bulletlist":
                            headerSep = "";
                            first = "<ul>";
                            pre = "<li>";
                            post = "</li>";
                            last = "</ul>";
                            break;
                        case "paragraph":
                            headerSep = " ";
                            first = "";
                            pre = "";
                            post = " ";
                            last = "";
                            break;
                    }

                    s = "";
                    for (var j in vdForm.validators)
                    {
                        var val = vdForm.validators[j];
                        if (!val.isvalid && val.errmsg)
                        {
                            s += pre + val.errmsg + post;
                        }
                    }
                    if (s != "")
                    {
                        s = headerSep + first + s + last;
                    }
                    if (oSummary.headertext != "")
                    {
                        s = oSummary.headertext + s;
                    }
                    
                    summary.innerHTML = s;
                    summary.style.display = (s == "") ? "none" : "";
                    //window.scrollTo(0,0);
                }

                if (oSummary.messagebox)
                {
                    s = "";
                    if (oSummary.headertext != "")
                    {
                        s += oSummary.headertext + "\n";
                    }
                    for (var j in vdForm.validators)
                    {
                        var val = vdForm.validators[j];
                        if (!val.isvalid && val.errmsg != null)
                        {
                            switch (oSummary.displaymode)
                            {
                                case "list":
                                default:
                                    s += val.errmsg + "\n";
                                    break;
                                case "bulletlist":
                                    s += "  - " + val.errmsg + "\n";
                                    break;
                                case "paragraph":
                                    s += val.errmsg + " ";
                                    break;
                            }
                        }
                    }
                    alert(s);
                }
            }
        }
    }
}

function VDGetControlValue(formName, controlName)
{
    var control;
    control = document.forms[formName].elements[controlName];
    if (typeof(control) == "undefined")
        return "";
    
    var isArray = false;
    if (controlName.length > 2)
        isArray = controlName.substring(controlName.length - 2, controlName.length) == "[]";
    
    return VDGetControlValueRecursive(control, isArray);
 }

function VDGetControlValueRecursive(control, isArray)
{
    var result = "";
    if (typeof(control.type) == "undefined")
    {
        if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number")
        {
            for (var j = 0; j < control.length; j++)
            {
                var value = VDGetControlValueRecursive(control[j], isArray);
                if (value != "")
                {
                    if (isArray && result != "")
                        result += vdDelimiter + value;
                    else
                        result = value;
                }
            }
        }
        else if (typeof(control.tagName) == "string" && control.tagName.toLowerCase() == "option")
        {
            if (control.selected)
            {
                if (typeof(control.value) == "string")
                {
                    if (control.getAttribute("VALUE") == "")
                        result = VDTrim(control.text);
                    else
                        result = VDTrim(control.value);
                }
                else
                {
                    result = VDTrim(control.text);
                }
            }
        }
    }
    else
    {
        if (control.type == "select-multiple")
        {
            var children = control.getElementsByTagName("OPTION");
            result = VDGetControlValueRecursive(children, isArray);
        }
        else if (typeof(control.value) == "string")
        {
            if (control.type == "checkbox" || control.type == "radio")
            {
                if (control.checked)
                    result = VDTrim(control.value);
            }
            else
                result = VDTrim(control.value);
        }
    }
    return result;
}

function VDTrim(str)
{
    var match = str.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (match == null) ? "" : match[1];
}

function VDConvert(op, val)
{
    function GetFullYear(year)
    {
        return (year + 2000) - ((year < 30) ? 0 : 100);
    }
    
    var dataType = val.validtype;
    var num, cleanInput, m, exp;
    if (dataType == "integer")
    {
        exp = /^\s*[-\+]?\d+\s*$/;
        if (op.match(exp) == null) 
            return null;
        num = parseInt(op, 10);
        return (isNaN(num) ? null : num);
    }
    else if(dataType == "float")
    {
        exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\.\\d+)?\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        cleanInput = (m[1] ? m[1] : '') + (m[2] && m[2].length > 0 ? m[2] : "0") + (m[3] ? m[3] : '');
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    } 
    else if (dataType == "currency")
    {
        exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\,)*)(\\d+)(\\.\\d{1,2})?\\s*$");
        m = op.match(exp);
        if (m == null)
            return null;
        var intermed = (m[2] ? m[2] : '') + (m[5] ? m[5] : '');
        cleanInput = (m[1] ? m[1] : '') + intermed.replace(new RegExp("(\\,)", "g"), "") + (m[6] ? m[6] : '');
        num = parseFloat(cleanInput);
        return (isNaN(num) ? null : num);            
    }
    else if (dataType == "date")
    {
        var day, month, year;
        if (val.dateorder == "ymd")
        {
            exp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$");
            m = op.match(exp);
            if (m == null)
                return null;
            day = m[6];
            month = m[5];
            year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
        }
        else
        {
            exp = new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
            m = op.match(exp);
            if (m == null)
                return null;
            if (val.dateorder == "dmy")
            {
                day = m[1];
                month = m[3];
            }
            else
            {
                day = m[3];
                month = m[1];
            }
            year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
        }
        month -= 1;
        var date = new Date(year, month, day);
        return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
    }
    else
    {
        return op.toString();
    }
}

function VDCompare(operand1, operand2, operator, val)
{
    var op1, op2;
    if ((op1 = VDConvert(operand1, val)) == null)
        return false;    
    if ((op2 = VDConvert(operand2, val)) == null)
        return true;

    if (val.validtype == "string" && !val.casesensitive)
    {
        op1 = op1.toLowerCase();
        op2 = op2.toLowerCase();
    }    
    switch (operator)
    {
        case "ne":
            return (op1 != op2);
        case "g":
            return (op1 > op2);
        case "ge":
            return (op1 >= op2);
        case "l":
            return (op1 < op2);
        case "le":
            return (op1 <= op2);
        case "e":
        default:
            return (op1 == op2);            
    }
}

function VDEvaluateRequired(validator)
{	
   var value = VDGetControlValue(vdForm.name, validator.control);   					
	if (value.length < validator.minlength){
		return false;
	}
	if (validator.maxlength != -1)
		return (value.length <= validator.maxlength);
		
    return true;
}
function VDEvaluateLength(validator)
{
    var value = VDGetControlValue(vdForm.name, validator.control);
	if(value.length!=0){
		if (value.length < validator.minlength)
			return false;		
		if (value.length > validator.maxlength)
			return false;    	
	}
    return true;
}

function VDEvaluateChecktype(validator)
{
    var value = VDGetControlValue(vdForm.name, validator.control);
    if (value.length == 0)
        return true;
    
    return (VDConvert(value, validator) != null);
}

function VDEvaluateRange(validator)
{
    var value = VDGetControlValue(vdForm.name, validator.control);
    if (value.length == 0) 
        return true;
    
    return (VDCompare(value, validator.minvalue, "ge", validator) &&
            VDCompare(value, validator.maxvalue, "le", validator));
}

function VDEvaluateCompare(validator)
{
    var value = VDGetControlValue(vdForm.name, validator.control);
    if (value.length == 0) 
        return true;
    
    var compareTo = "";
    if (typeof(validator.comparevalue) != "undefined")
    {
        compareTo = validator.comparevalue;
    }
    else if (typeof(validator.comparecontrol) != "undefined")
    {
        compareTo = VDGetControlValue(vdForm.name, validator.comparecontrol);
    }
    else
        return false;

    return VDCompare(value, compareTo, validator.operator, validator);
}

function VDEvaluateRegExp(validator)
{
    var result = true;
    
    var value = VDGetControlValue(vdForm.name, validator.control);
    if (value.length > 0)
    {
        var rx;
        try
        {
            eval("rx = " + validator.clientregexp + ";");
            var matches = rx.exec(value);
            result = (matches != null);
        }
        catch(e)
        {
            result = true;
        }
    }
    
    return result;
}

function VDEvaluateEmail(validator)
{
    var value = VDGetControlValue(vdForm.name, validator.control);
    if (value.length == 0) 
        return true;        
    var rx = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)*\.\w{2,8}$/;
    var matches = rx.exec(value);
    return (matches != null);
}

function VDEvaluateCustom(validator)
{
    var value = null;
    if (typeof(validator.control) == "string")
    {
        value = VDGetControlValue(vdForm.name, validator.control);
    }
    
    var args = new Object();
    args.isvalid = true;
    args.errmsg = validator.errmsg;
    args.value = value;
    if (typeof(validator.clientfunction) == "string")
    {
        var rx = /^[a-zA-Z_]\w*$/;
        var m = rx.exec(validator.clientfunction);
        var isfunc;
        if (m != null)
        {
            eval("isfunc = typeof(" + validator.clientfunction + ") == 'function';");
            if (isfunc)
            {
                eval(validator.clientfunction + "(args);");
                args.isvalid = (args.isvalid === true);
                if (typeof(args.errmsg) == "string" && validator.type != "group")
                {
                    validator.errmsg = args.errmsg;
                }
            }
        }
    }        
    return args.isvalid;
}

function VDEvaluateUrl(validator)
 {
	var url = VDGetControlValue(vdForm.name, validator.control);		
	
 	if(url != ''){
		urlRegxp = /^(http|https|ftp|mailto|file|news|gopher|telnet|nntp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.([a-z]{2,5}(([0-9]{1,5})?\/.*)?)$/i;
		  if (urlRegxp.test(url) != true)
			return false;
		else 
			return true;
	}
	else
		return true;
}
function VDEvaluateWWW(validator)
 {
	var url = VDGetControlValue(vdForm.name, validator.control);		
	
 	if(url != ''){
		urlRegxp = /^www\.[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(( 0-9]{1,5})?\/.*)?$/i;
		  if (urlRegxp.test(url) != true)
			return false;
		else 
			return true;
	}
	else
		return true;
}
function VDEvaluateWeblo(validator)
{
	
	var url = VDGetControlValue(vdForm.name, validator.control);		
	if (url != ''){
	if(url.indexOf('weblo.com') > 0){
		return true;
	
	}
	else
	{
		return false;
	}
	}
	else
	{
		return true;
	}
}
function VDEvaluateDomain(validator)
{
	var i;
	var valid = true;	
	var domain = VDGetControlValue(vdForm.name, validator.control);
	var domains = new Array();
	domains = domain.split(/[ \t\n\r,]+/);
	Regxp = /^[a-zA-Z0-9\-]+(\.([a-zA-Z]){2,4})*$/;
	
	for (i = 0; i < domains.length; i++) {
		valid = Regxp.test(domains[i]);
		if (!valid) break;
	}
	
	return valid;
}

function VDEvaluateWebloFloat(validator)
{	
	str = VDGetControlValue(vdForm.name, validator.control);
	if (str != ""){
	if(str > 0)
		{
	    if(/^[-+]?[0-9]+(\.[0-9]+)?$/.test(str) == true){
			

			return true;
		}else{
			return false;
     	}
	 } 
	  else {
	 return false;
	  }
	}
	return true;
}
function VDEvaluateWebloRequired2(validator)
{
	
	name = VDGetControlValue(vdForm.name, validator.control);
		if (name.lenght < 0 ) {
		return false;
		}
		else
		{
		return true;
		}
	}
function VDEvaluateZip(validator)
{
	var value = VDGetControlValue(vdForm.name, validator.control);		
	
	if(value.length!=0)
	{		
		if (isNaN(value)  || value.length < 4 || value.length > 10)
		{		
			alert(!isNaN(value));
			return false;
		}				
		else
			return true;
	}
}
function VDEvaluatePostal(validator)
{
	
	var Pcode = VDGetControlValue(vdForm.name, validator.control);		
	
	if (Pcode != ""){
	if(Pcode.length < 4 || Pcode.length > 10){
	return false;
	}else{
	
	return true; 
	}
	
	}
}
function VDEvaluateLogin(validator)
{
	var i;
	var valid = true;	
	var login = VDGetControlValue(vdForm.name, validator.control);
	var str = new Array();
	str = login.split(/[ \t\n\r,]+/);
	Regxp = /^[a-zA-Z0-9\-]+(\.([a-zA-Z]){3,19})*$/;
	
	for (i = 0; i < str.length; i++) {
		valid = Regxp.test(str[i]);
		if (!valid) 
		break;
	}
	
	return valid;
}
function VDEvaluatePassword(validator)
{
	
	var Password = VDGetControlValue(vdForm.name, validator.control);		
	
	if (Password != ""){
	if(Password.length < 6 ){
	return false;
	}else{
	
	return true; 
	}
	
	}
}

var digits = "0123456789";
var phoneNumberDelimiters = "()-";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 5;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
         var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    //if all characters are number
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
        
    for (i = 0; i < s.length; i++)
    {   
        // Check  white space.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function VDEvaluateMobile(validator)
{
	var Cell = VDGetControlValue(vdForm.name, validator.control);
		
	Str1=new String(Cell);
		
	if (Str1.length!=0){
	 	if (checkInternationalPhone(Str1)==false){
		return false
	}
}	
	return true
}

function VDEvaluatePhone(validator)
{
	
 	var Phone = VDGetControlValue(vdForm.name, validator.control);
		
	 Str=new String(Phone);
		
	if (Str.length!=0 ){
		
		if (checkInternationalPhone(Str)==false){
		
		return false
	}
}
   return true
 }

function SubmitFunction(Name, Func){					
	if(Func == ""){
		return VDValidateForm(Name);	
	}

	var f = new Function(Func);
	return f() && VDValidateForm(Name);
}
