//	$Id: nx_userValidation.js,v 1.20 2011/08/12 06:38:27 payal 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,isAdmin)
{
    //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();
            return false;
        }
        if(!checkPasswd(theForm.pass1.value))
        {
            document.getElementById('pass1').className='highlight';
            document.getElementById('pass1img').className='hlightimage';
            alert(msgPasswd);
            theForm.pass1.focus();
            return false;
        }
        else
        {
            if(theForm.pass1.value != theForm.pass2.value)
            {
                document.getElementById('pass2').className='highlight';
                document.getElementById('pass2img').className='hlightimage red_text';
                alert(msgSamePasswd);
                theForm.pass2.focus();
                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();
            return false;
        }
        else
        {
            document.getElementById('lname').className='highlight';
            document.getElementById('lnameimg').className='hlightimage';
            alert(msgFLnameBlank);
            theForm.lname.focus();
            return false;
        }
    }
    if(!checkAddContTime(theForm.address.value))
    {
        document.getElementById('address').className='highlight';
        document.getElementById('addressimg').className='hlightimage';
        alert(msgAddBlank);
        theForm.address.focus();
        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();
            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();
                    return false;
                }
            }
            else
            {
                document.getElementById('zipcode').className='highlight';
                document.getElementById('zipcodeimg').className='hlightimage';
                alert(msgCheckZip);
                theForm.zip.focus();
                return false;
            }
        }
    }
    else
    {
        document.getElementById('zipcode').className='highlight';
        document.getElementById('zipcodeimg').className='hlightimage';
        alert(msgMandatoryFieldEmpty);
        theForm.zip.focus();
        return false;
    }
    if(!checkCity(theForm.city.value))
    {
        document.getElementById('city').className='highlight';
        document.getElementById('cityimg').className='hlightimage';
        alert(msgCityBlank);
        theForm.city.focus();
        return false;
    }
    if(theForm.id_domicile.value == '')
    {
        document.getElementById('domicile').className='highlight';
        document.getElementById('domicileimg').className='hlightimage';
        alert(msgDomicile);
        theForm.id_domicile.focus();
        return false;
    }
    if(theForm.id_town && theForm.id_town.value == '')
    {
        document.getElementById('town').className='highlight';
        document.getElementById('townimg').className='hlightimage';
        alert(msgTown);
        theForm.id_town.focus();
        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) < 0)
			{
				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();
            return false;
        }
        else if(!emailInvalid(theForm.email.value))
        {
            document.getElementById('email').className='highlight';
            document.getElementById('emailimg').className='hlightimage';
            alert(msgInvalidEmail);
            theForm.email.focus();
            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();
            return false;
        }
        if(theForm.second_email && theForm.email.value == theForm.second_email.value)
        {
			document.getElementById('second_email').className='highlight';
			document.getElementById('second_emailimg').className='hlightimage';
			alert(msgSameSeconEmail);
			theForm.second_email.focus();
			return false;
        }
        if(theForm.second_email && !isblank(theForm.second_email.value) && !emailInvalid(theForm.second_email.value))
        {
            document.getElementById('second_email').className='highlight';
            document.getElementById('second_emailimg').className='hlightimage';
            alert(msgInvalidEmail);
            theForm.second_email.focus();
            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();
                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();
            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();
            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();
        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();
            return false;
        }
    }
    if(isAdmin != 'Y' && theForm.birth_year && theForm.birth_year.value == '')
    {
        document.getElementById('birth_year').className='highlight';
        document.getElementById('birth_yearimg').className='hlightimage';
        alert(msgAge);
        theForm.birth_year.focus();
        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);
            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();
        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();
        return false;
    }
    return true;
}

function charcount()
{
	var lan = ((document.getElementById('cont_time_ele').value).replace(/\n/g,"")).length;

	if(lan > 80)
	{
		alert('You can not enter more then 80 charcter');
		document.getElementById('cont_time').className='highlight';
		document.getElementById('cnt_timeimg').className='hlightimage';
		return false;
	}

    var lanInto = ((document.getElementById('intro_ele').value).replace(/\n/g,"")).length;
    if(lanInto > 500)
    {
        alert('You can not enter more then 500 charcter');
        document.getElementById('introduction').className='highlight';
        document.getElementById('introimg').className='hlightimage';
        return false;
    }
	return true;
}
function confirmReset(thisForm)
{
	if(confirm(msgConfirmReset))
		return true;

	return false;
}
function showPreview(sdsUrl,user)
{
	for(var i=0;i < document.frmEditDealer.id_template.length;i++)
	{
		if(document.frmEditDealer.id_template[i].checked)
			openImg(sdsUrl+'/list.php?id_user='+user+'&id_template='+document.frmEditDealer.id_template[i].value,800,800,'');
	}
}

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('Enter the new password');
					theForm.newpass1.focus();
					return false;
				}

				if(isblank(theForm.newpass2.value))
				{
					document.getElementById('idNewpass2').className='highlight';
					document.getElementById('newpass2img').className='hlightimage';
					alert('Enter the confirm password');
					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 showPriceHistory(v)
{
	if(v == false)
		alert(msgPriceHistory);
}

/** To Select userid from radio button, when registering through facebook */
function suggestUser()
{
    var selectUser = document.reg.suggest_user;
    for (i=0; i < selectUser.length; i++)
    {
        if (selectUser[i].checked == true)
            document.reg.loginid.value = selectUser[i].value;
    }
    jQuery('#logidimg').html('');
}

function checkExistAndSuggestUser(obj)
{
    var loginVal = obj.value;
    var nm = obj.name;

    var msg = checkLogin(loginVal);

    if(msg != 1 && loginVal != '')
    {
        jQuery('#logidimg').html('');
        jQuery('#suggesNettiUser').html('');
        jQuery('#suggesNettiUser').hide();
        jQuery('#logid').attr('class','highlight');
        jQuery('#logidimg').attr('class','hlightimage red_text');
        alert(msg);
        obj.focus();
        return false;
    }

    if(loginVal != '')
    {
        jQuery('#loadingImg').css('display','inline');
        var pars='ajx=1&nm='+nm+'&val='+loginVal;

        jQuery.post('addUser.php',pars,function(str){

            if(str != '')
            {
                jQuery('#suggesNettiUser').html(str);
                jQuery('#suggesNettiUser').css('display','block');
                jQuery('#logidimg').html(msgUserExist);
            }
            else
            {
                jQuery('#suggesNettiUser').hide();
                jQuery('#logidimg').html('');
            }
            jQuery('#loadingImg').hide();
        });
        return false;
    }
    return false;
}

function matchConfirmPass(p1,p2)
{
    if(p1.value != p2.value)
        jQuery('#pass2img').html(msgSamePasswd);
    else
        jQuery('#pass2img').html('');
}

function setPrimaryEmail()
{
	var email = jQuery('#second_email_ele').val();
	jQuery('#second_email_ele').val(jQuery('#email_ele').val());
	jQuery('#email_ele').val(email);
	jQuery('#showResetButton').html(msgToSaveChanges);
	jQuery('#showResetButton').addClass('red_text bold');
}

function removeSecondEmail()
{
	jQuery('#second_email_ele').val('');
	jQuery('#set_primary').css('display','none');
	jQuery('#remove_email').css('display','none');
	jQuery('#showResetButton').html(msgToSaveChanges);
	jQuery('#showResetButton').addClass('red_text bold');
}

function checkBlankSecondEmail()
{
	if(jQuery('#second_email_ele').val() == '' && secondEmailExist != '')
	{
		jQuery('#showResetButton').html(msgToSaveChanges);
		jQuery('#showResetButton').addClass('red_text bold');
	}
}
