//	$Id: nx_userValidation.js,v 1.4 2010/06/15 08:49:51 kiran Exp $

function checkPassword(theForm)
{
    if(document.getElementById('idOldpass'))
    {
        if(isblank(theForm.oldpass.value))
        {
            document.getElementById('idOldpass').className='highlight';
            document.getElementById('oldpassimg').className='hlightimage';
            alert(msgEnterPasswd);
            theForm.oldpass.focus();
            return false;
        }
        else
        {
            if(theForm.newpass1.value || theForm.newpass2.value)
            {
                if(isblank(theForm.newpass1.value))
                {
                    document.getElementById('idNewpass1').className='highlight';
                    document.getElementById('newpass1img').className='hlightimage';
                    alert(msgEnterPass);
                    theForm.newpass1.focus();
                    return false;
                }

                if(isblank(theForm.newpass2.value))
                {
                    document.getElementById('idNewpass2').className='highlight';
                    document.getElementById('newpass2img').className='hlightimage';
                    alert(msgEnterConfPass);
                    theForm.newpass2.focus();
                    return false;
                }

                if(!checkPasswd(theForm.newpass1.value))
                {
                    document.getElementById('idNewpass1').className='highlight';
                    document.getElementById('newpass1img').className='hlightimage';
                    alert(msgPasswd);
                    theForm.newpass1.focus();
                    return false;
                }

                if(!checkPasswd(theForm.newpass2.value))
                {
                    document.getElementById('idNewpass2').className='highlight';
                    document.getElementById('newpass2img').className='hlightimage';
                    alert(msgPasswd);
                    theForm.newpass2.focus();
                    return false;
                }

                if(theForm.newpass1.value != theForm.newpass2.value)
                {
                    document.getElementById('idNewpass1').className='highlight';
                    document.getElementById('newpass1img').className='hlightimage';
                    document.getElementById('idNewpass2').className='highlight';
                    document.getElementById('newpass2img').className='hlightimage';
                    alert(msgSamePasswd);
                    theForm.newpass1.value='';
                    theForm.newpass2.value='';
                    theForm.newpass1.focus();
                    return false;
                }
            }
        }
    }
}

function checkAll(theForm,newreg,dealer)
{
    var enablePageTracker=false;
    if(typeof(siteCode) != 'undefined' && siteCode == 'NA')
        enablePageTracker=true;

    //Newreg value is Y then check password and login id validation value
    if(newreg  == "Y")
    {
        var msg=checkLogin(theForm.loginid.value);

        if(msg != 1)
        {
            document.getElementById('logid').className='highlight';
            document.getElementById('logidimg').className='hlightimage';
            alert(msg);
            theForm.loginid.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','loginid');} catch(err) {}

            return false;
        }
        if(!checkPasswd(theForm.pass1.value))
        {
            document.getElementById('pass1').className='highlight';
            document.getElementById('pass1img').className='hlightimage';
            alert(msgPasswd);
            theForm.pass1.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','pass1');} catch(err) {}

            return false;
        }
        else
        {
            if(theForm.pass1.value != theForm.pass2.value)
            {
                document.getElementById('pass2').className='highlight';
                document.getElementById('pass2img').className='hlightimage';
                alert(msgSamePasswd);
                theForm.pass2.focus();

                if(enablePageTracker)
                    try {pageTracker._trackEvent('Virheet','Rekisteröinti','pass2');} catch(err) {}

                return false;
            }
        }
    }
    if(!checkName(theForm.fname.value) || !checkName(theForm.lname.value))
    {
        if(!checkName(theForm.fname.value))
        {
            document.getElementById('fname').className='highlight';
            document.getElementById('fnameimg').className='hlightimage';
            alert(msgFLnameBlank);
            theForm.fname.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','fname');} catch(err) {}

            return false;
        }
        else
        {
            document.getElementById('lname').className='highlight';
            document.getElementById('lnameimg').className='hlightimage';
            alert(msgFLnameBlank);
            theForm.lname.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','lname');} catch(err) {}

            return false;
        }
    }
    if(!checkAddContTime(theForm.address.value))
    {
        document.getElementById('address').className='highlight';
        document.getElementById('addressimg').className='hlightimage';
        alert(msgAddBlank);
        theForm.address.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','address');} catch(err) {}

        return false;
    }
    if(!(isblank(theForm.zip.value)))
    {
        if(!(checkZip(theForm.zip.value)))
        {
            document.getElementById('zipcode').className='highlight';
            document.getElementById('zipcodeimg').className='hlightimage';
            alert(msgZip);
            theForm.zip.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','zip');} catch(err) {}

            return false;
        }
        else if(theForm.id_domicile.value != 15)
        {
            if(theForm.zip.value.match(/^[0-9]{5}$/))
            {
                if(theForm.zip.value <= 0)
                {
                    document.getElementById('zipcode').className='highlight';
                    document.getElementById('zipcodeimg').className='hlightimage';
                    alert(msgCheckZip);
                    theForm.zip.focus();

                    if(enablePageTracker)
                        try {pageTracker._trackEvent('Virheet','Rekisteröinti','zip');} catch(err) {}

                    return false;
                }
            }
            else
            {
                document.getElementById('zipcode').className='highlight';
                document.getElementById('zipcodeimg').className='hlightimage';
                alert(msgCheckZip);
                theForm.zip.focus();

                if(enablePageTracker)
                    try {pageTracker._trackEvent('Virheet','Rekisteröinti','zip');} catch(err) {}

                return false;
            }
        }
    }
    else
    {
        document.getElementById('zipcode').className='highlight';
        document.getElementById('zipcodeimg').className='hlightimage';
        alert(msgMandatoryFieldEmpty);
        theForm.zip.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','zip');} catch(err) {}

        return false;
    }
    if(!checkCity(theForm.city.value))
    {
        document.getElementById('city').className='highlight';
        document.getElementById('cityimg').className='hlightimage';
        alert(msgCityBlank);
        theForm.city.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','city');} catch(err) {}

        return false;
    }
    if(theForm.id_domicile.value == '')
    {
        document.getElementById('domicile').className='highlight';
        document.getElementById('domicileimg').className='hlightimage';
        alert(msgDomicile);
        theForm.id_domicile.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','id_domicile');} catch(err) {}

        return false;
    }    
    //If dealer Y then check dealer validation of ADD/EDIT dealer.
    if(dealer == "Y")
    {
		if(theForm.delivery_cost && !isblank(theForm.delivery_cost.value))
		{
			if(!(theForm.delivery_cost.value.match(/^[0-9]{1,10}$/)) || parseInt(theForm.delivery_cost.value) < 1)
			{
				document.getElementById('delivery_cost').className='highlight';
				document.getElementById('delivery_costimg').className='hlightimage';
				alert(msgInvalidDelCost);
				theForm.delivery_cost.select();
				theForm.delivery_cost.focus();
				return false;
			}
		}
        if(isblank(theForm.company.value))
        {
            document.getElementById('company').className='highlight';
            document.getElementById('companyimg').className='hlightimage';
            alert(msgMandatoryFieldEmpty);
            theForm.company.focus();
            return false;
        }
        if(theForm.url && (theForm.url.value.substring(0,7) != 'http://' || isblank(theForm.url.value)))
        {
            document.getElementById('url').className='highlight';
            document.getElementById('urlimg').className='hlightimage';
            alert(msgUrlFormat);
            theForm.url.focus();
            return false;
        }
        if(theForm.simple_url)
        {
            if(theForm.simple_url.value == '')
            {
                document.getElementById('simple_url').className='highlight';
                document.getElementById('simple_urlimg').className='hlightimage';
                alert(msgBlankSimpeUrl);
                theForm.simple_url.select();
                theForm.simple_url.focus();
                return false;
            }
            if(!(theForm.simple_url.value.match(/^([a-zA-Z0-9-_~])*$/)))
            {
                document.getElementById('simple_url').className='highlight';
                document.getElementById('simple_urlimg').className='hlightimage';
                alert(msgSpecialChar);
                theForm.simple_url.select();
                theForm.simple_url.focus();
                return false;
            }
        }
        if(newreg == "Y" && !(theForm.id_sales_person.selectedIndex > 0))
        {
            document.getElementById('id_sales_person').className='highlight';
            document.getElementById('id_sales_personimg').className='hlightimage';
            alert(msgSalesPerson);
            theForm.id_sales_person.focus();
            return false;
        }
        if(newreg == "Y" && !(theForm.id_hoover_type.selectedIndex > 0))
        {
            document.getElementById('id_hoovertype').className='highlight';
            document.getElementById('id_hoovertypeimg').className='hlightimage';
            alert(msgHooverType);
            theForm.id_hoover_type.focus();
            return false;
        }
        //For dealer check email only if it is entered
        if(!(isblank(theForm.email.value)))
        {
            if(!emailInvalid(theForm.email.value))
            {
                document.getElementById('email').className='highlight';
                document.getElementById('emailimg').className='hlightimage';
                alert(msgInvalidEmail);
                theForm.email.focus();
                return false;
            }
        }
        if(theForm.email_for_lead_xml && !(isblank(theForm.email_for_lead_xml.value)))
        {
            var mEmail=theForm.email_for_lead_xml.value;

            strMail=mEmail.split(',');

            for(var m=0; m < strMail.length; m++)
            {
                if(!emailInvalid(strMail[m]))
                {
                    document.getElementById('email_for_lead_xml').className='highlight';
                    document.getElementById('email_for_lead_xmlimg').className='hlightimage';
                    alert(msgInvalidEmail);
                    theForm.email_for_lead_xml.focus();
                    return false;
                }
            }
        }
        if(theForm.email_for_leads && !(isblank(theForm.email_for_leads.value)))
        {
            var mEmail=theForm.email_for_leads.value;

            strMail=mEmail.split(',');

            for(var m=0; m < strMail.length; m++)
            {
                if(!emailInvalid(strMail[m]))
                {
                    document.getElementById('email_for_leads').className='highlight';
                    document.getElementById('email_for_leadsimg').className='hlightimage';
                    alert(msgInvalidEmail);
                    theForm.email_for_leads.focus();
                    return false;
                }
            }
        }
        if(newreg == 'Y' && document.reg.dlrlmtopt[1].checked && (isblank(theForm.total_limit.value) || theForm.total_limit.value < 0))
        {
            document.getElementById('total_limit').className='highlight';
            document.getElementById('total_limitimg').className='hlightimage';
            alert(msgMinLimit);
            theForm.total_limit.select();
            theForm.total_limit.focus();
            return false;
        }
    }
    else
    {
        if(isblank(theForm.email.value))
        {
            document.getElementById('email').className='highlight';
            document.getElementById('emailimg').className='hlightimage';
            alert(msgBlankEmail);
            theForm.email.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','email');} catch(err) {}

            return false;
        }
        else if(!emailInvalid(theForm.email.value))
        {
            document.getElementById('email').className='highlight';
            document.getElementById('emailimg').className='hlightimage';
            alert(msgInvalidEmail);
            theForm.email.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','email');} catch(err) {}

            return false;
        }
        if(theForm.conf_email && (theForm.email_ele.value != theForm.conf_email.value))
        {
            document.getElementById('conf_email').className='highlight';
            document.getElementById('conf_emailimg').className='hlightimage';
            alert(msgSameConfEmail);
            theForm.conf_email.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','conf_email');} catch(err) {}

            return false;
        }
        if(isblank(theForm.mobile.value) && !theForm.company)
        {
            if(!(check(theForm.mobile.value)) || theForm.mobile.value.match(/^[0\s+-]{0,}$/))
            {
                document.getElementById('mobile').className='highlight';
                document.getElementById('mobileimg').className='hlightimage';
                alert(msgMobileFormat);
                theForm.mobile.focus();

                if(enablePageTracker)
                    try {pageTracker._trackEvent('Virheet','Rekisteröinti','mobile');} catch(err) {}

                return false;
            }
        }
    }
    if(theForm.phone && !(isblank(theForm.phone.value)))
    {
        if(!(check(theForm.phone.value)) || theForm.phone.value.match(/^[0\s+-]{0,}$/))
        {
            document.getElementById('mobile').className='highlight';
            document.getElementById('mobileimg').className='hlightimage';
            alert(msgPhoneFormat);
            theForm.phone.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','phone');} catch(err) {}

            return false;
        }
    }
    if(!(isblank(theForm.mobile.value)))
    {
        if(!(check(theForm.mobile.value)) || theForm.mobile.value.match(/^[0\s+-]{0,}$/))
        {
            document.getElementById('mobile').className='highlight';
            document.getElementById('mobileimg').className='hlightimage';
            alert(msgMobileFormat);
            theForm.mobile.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','mobile');} catch(err) {}

            return false;
        }
    }
    if(theForm.phone && isblank(theForm.phone.value) && isblank(theForm.mobile.value) && theForm.company)
    {
        document.getElementById('mobile').className='highlight';
        document.getElementById('mobileimg').className='hlightimage';
        alert(msgOneCont);
        theForm.phone.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','mobile');} catch(err) {}

        return false;
    }
    if(theForm.fax && !(isblank(theForm.fax.value)))
    {
        if(!(check(theForm.fax.value)))
        {
            document.getElementById('fax').className='highlight';
            document.getElementById('faximg').className='hlightimage';
            alert(msgFaxFormat);
            theForm.fax.focus();

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','fax');} catch(err) {}

            return false;
        }
    }
    if(theForm.id_age && theForm.id_age.value == '')
    {
        document.getElementById('id_age').className='highlight';
        document.getElementById('id_ageimg').className='hlightimage';
        alert(msgAge);
        theForm.id_age.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','id_age');} catch(err) {}

        return false;
    }
    if(!(isblank(theForm.cont_time.value)))
    {
        if(!(checkAddContTime(theForm.cont_time.value)))
        {
            document.getElementById('cont_time').className='highlight';
            document.getElementById('cnt_timeimg').className='hlightimage';
            alert(msgContTime);
            theForm.cont_time.focus();
			return false;
		}
	}
    if(theForm.notify_via_mobile && theForm.notify_via_mobile.checked)
    {
        if(isblank(theForm.mobile.value))
        {
            document.getElementById('phonemobile').className='highlight';
            document.getElementById('phonemobileimg').className='hlightimage';
            alert(msgMobileSms);
            theForm.mobile.focus();
            return false;
        }
    }
    if(newreg == "Y" && dealer == "N" && theForm.allow_save)
    {
        if(theForm.allow_save.checked == false)
        {
            document.getElementById('allow_save').className='highlight';
            document.getElementById('allow_saveimg').className='hlightimage';
            alert(msgAllowSaveInDb);

            if(enablePageTracker)
                try {pageTracker._trackEvent('Virheet','Rekisteröinti','allow_save');} catch(err) {}

            return false;
        }
    }
    if(theForm.user_code && theForm.user_code.value == '')
    {
        document.getElementById('user_code').className='highlight';
        document.getElementById('user_codeimg').className='hlightimage';
        alert(magWordMatch);
        theForm.user_code.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','user_code');} catch(err) {}

        return false;
    }
    else if(theForm.user_code && (theForm.user_code.value).length > 20)
    {
        document.getElementById('user_code').className='highlight';
        document.getElementById('user_codeimg').className='hlightimage';
        alert(magWordMatch);
        theForm.user_code.focus();

        if(enablePageTracker)
            try {pageTracker._trackEvent('Virheet','Rekisteröinti','user_code');} catch(err) {}

        return false;
    }
    return true;
}