/* $Id: nc_cottageValidation.js,v 1.8 2011/11/07 05:37:51 nimisha Exp $ */

/*Function to check form validation when submit.*/
function checkAll(theForm,isAdmin)
{
	if(theForm.berths.value != '')
	{
		var jsBerths=theForm.berths.value.replace(",",".");

		if(isNaN(jsBerths) || Math.ceil(jsBerths) < 1)
		{
			document.getElementById('berths').className='highlight';
			document.getElementById('berthsimg').className='hlightimage';
			alert(msgInvalidberths);
			theForm.berths.select();
			theForm.berths.focus();sector
			return false;
		}
	}
	else
	{
		document.getElementById('berths').className='highlight';
		document.getElementById('berthsimg').className='hlightimage';
		alert(msgBerthBlank);
		theForm.berths.focus();
		return false;
	}
	if(theForm.room.value != '')
	{
		var jsRoom=theForm.room.value.replace(",",".");

		if(isNaN(jsRoom) || Math.ceil(jsRoom) < 1)
		{
			document.getElementById('room').className='highlight';
			document.getElementById('roomimg').className='hlightimage';
			alert(msgInvalidRoom);
			theForm.room.select();
			theForm.room.focus();
			return false;
		}
	}
	else
	{
		document.getElementById('room').className='highlight';
		document.getElementById('roomimg').className='hlightimage';
		alert(msgRoomBlank);
		theForm.room.focus();
		return false;
	}
	if(theForm.area.value != '')
	{
		var jsArea=theForm.area.value.replace(",",".");

		if(isNaN(jsArea) || Math.ceil(jsArea) < 1)
		{
			document.getElementById('area').className='highlight';
			document.getElementById('areaimg').className='hlightimage';
			alert(msgInvalidArea);
			theForm.area.select();
			theForm.area.focus();
			return false;
		}
	}
	else
	{
		document.getElementById('area').className='highlight';
		document.getElementById('areaimg').className='hlightimage';
		alert(msgAreaBlank);
		theForm.area.focus();
		return false;
	}
	if(theForm.mfg_year.value == '')
	{
		document.getElementById('mfg_year').className='highlight';
		document.getElementById('mfg_yearimg').className='hlightimage';
		alert(msgInvalidYear);
		theForm.mfg_year.focus();
		return false;
	}
	if(theForm.id_domicile.value == '')
	{
		document.getElementById('domicile').className='highlight';
		document.getElementById('domicileimg').className='hlightimage';
		alert(msgInvalidDomicile);
		theForm.id_domicile.focus();
		return false;
	}
	if(theForm.id_domicile.value != '' && theForm.id_domicile.value != '15')
	{
		if(theForm.id_town.value == '')
		{
			document.getElementById('town').className='highlight';
			document.getElementById('townimg').className='hlightimage';
			alert(msgInvalidTown);
			theForm.id_town.focus();
			return false
		}
	}

	if(!checkStringValue(theForm,'sector',invalidSector))
        return false;
	if(!checkAddressValue(theForm,'address',true))
		return false;
	if(!checkZipCode(theForm,'zip',true))
	    return false;
	if(!checkPostOffice(theForm,'post_office',true))
        return false;

	var u=theForm.cottage_home.value.replace("http://","");
	if(u != '')
	{
		u=theForm.cottage_home.value;
		if(!urlInvalid(u))
		{
			document.getElementById('cottage_home').className='highlight';
			document.getElementById('cottage_homeimg').className='hlightimage';
			alert(msgUrlFormat);
			theForm.cottage_home.select();
			theForm.cottage_home.focus();
			return false;
		}
	}
	/*both loop for check distance cottage from*/
	var arry_id = new Array();
	for(var i=1; i <= theForm.totDistance.value; i++)
	{
		arry_id[i]='d_'+i;
	}
	var dv;
	for(var i=1; i <= theForm.totDistance.value; i++)
	{
		dv=theForm.elements[arry_id[i]].value.replace(",",".");
		if(dv != '' && (isNaN(dv) || Math.ceil(dv) <= 0))
		{
			alert(msgInvalidDist);
			theForm.elements[arry_id[i]].focus();
			return false;
		}

	}

	var arry_dist = new Array();
	var arry_dist_val = new Array();
	for(var i=1; i <= 4; i++)
	{
		arry_dist[i]='distance_'+i;
		arry_dist_val[i]='value_'+i;
	}
	var distval;
	for(var i=1; i <= 4; i++)
	{
		dist=theForm.elements[arry_dist[i]].value;
		distval=theForm.elements[arry_dist_val[i]].value.replace(",",".");
		if((dist != '' && distval == '') || (dist == '' && distval != ''))
		{
			alert(msgBothfill);
			if(dist == '')
				theForm.elements[arry_dist[i]].focus();
			else
				theForm.elements[arry_dist_val[i]].focus();

			return false;
		}
		if(dist != '' && (!dist.match(/^[0-9a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ_´:\s\,\.-]{1,100}$/) || !isNaN(dist)))
		{
			alert(msgInvalidName);
			theForm.elements[arry_dist[i]].focus();
			return false;
		}
		if(distval != '' && (isNaN(distval) || Math.ceil(distval) <= 0))
		{
			alert(msgInvalidDist);
			theForm.elements[arry_dist_val[i]].focus();
			return false;
		}
	}

	if(isAdmin != 'Y')
	{
		if(isMaxWord(theForm.note.value," ",40) == 0)
		{
			document.getElementById('note').className='highlight';
			document.getElementById('noteimg').className='hlightimage';
			alert(msgLngDescWord);
			theForm.note.focus();
			return false;
		}
	}

   if(!checkStringValue(theForm,'invoice_name',msgString))
       return false;
   if(!checkAddressValue(theForm,'bill_add',false))
       return false;
   if(!checkZipCode(theForm,'bill_zip',false))
       return false;
   if(!checkPostOffice(theForm,'bill_postoffice',false))
       return false;

   return true;
}

//Function used when inserting sold price at marking cottage sold
function soldCottage(theForm)
{
	s=theForm.sold_price.value;

	if(s != "")
	{
		if(!(s.match(/^[0-9]{1,10}$/)))
		{
			alert(msgInvalidPrice);
			theForm.sold_price.focus();
			return false;
		}
		if (parseInt(s) < 1 )
		{
			alert(msgLessThan1Price);
			theForm.sold_price.focus();
			return false;
		}
		false;
	}
}

//This function check for word length in the string entered in the given textbox(input field)
function txtValid(theTxtbox,maxWordLen)
{
	//If any word in engine model field is longer than specified width characters long
	if(isLong(theTxtbox.value," ",maxWordLen) == 0)
	{
		alert(msgLngWordFPart + maxWordLen + msgLngWordSPart);
		theTxtbox.focus();
		return false;
	}
	else
		return true;
}

//This function works like the function 'txtValid', but used when there is a array of textBox(input field) elements
function txtErrayValid(theFrm,txtName,txtId,maxWordLen)
{
	for(i=0; i<theFrm.elements.length; i++)
	{
		var eleName=theFrm.elements[i].name;

		if(eleName.match(txtName) != null)
		{
			if(eleName.match(txtId) != null)
			{
				if(isLong(theFrm.elements[i].value," ",maxWordLen) == 0)
				{
					alert(msgLngWordFPart + maxWordLen + msgLngWordSPart);
					theFrm.elements[i].focus();
					return false;
					break;
				}
				else
				{
					return true;
					break;
				}
			}
		}
	}
	return false;
}

//VirtueInfo:20050525:Function to check mendatory fiels and whether field is numeric or not for admin side
function checkIsNumeric(theForm)
{
	if(theForm.cottageTxt.value != '')
	{
		var jscottageTxt=theForm.cottageTxt.value.replace(",",".");

		if(isNaN(jscottageTxt) || Math.ceil(jscottageTxt) < 1)
		{
			alert(msgInvalidberths);
			theForm.cottageTxt.focus();
			return false;
		}
	}
}

//VirtueInfo::20050525::Amit:: Function to check the text area maximum length and display alert message.
function textAreaLenLimit(v1,limit){
	var len=v1.value.length;
	var val=v1.value;
	var separator = '\n';
		var cntVal=0;
		var lineArray = val.split(separator);
		for (var i=0; i < lineArray.length; i++)
    	{
    		cntVal=cntVal+lineArray[i].length;
    	}
    	if(cntVal>limit)
		{
    		alert(msgMax+" "+limit+" "+msgChar);
 			v1.focus();
    		return false;
		}
}

//VirtueInfo::20050525::Jatin:: Function to check the text area maximum length without alert message.
function setTextAreaLength(fieldName,len)
{
	tmpVar=fieldName.value;
	fldLen=tmpVar.length;
	if(fldLen == len)
		return false;
}

function checkCottagePrice(theForm)
{
    if(theForm.price_day.value == '' && theForm.price_weekend.value == '' && theForm.price_week.value == '')
    {
        document.getElementById('price_day').className='highlight';
        document.getElementById('price_dayimg').className='hlightimage';
        alert(msgAtleastOnePrice);
        theForm.price_day.focus();
        return false;
    }
    var pday,pwed,pwk;
    pday=theForm.price_day.value.replace(",",".");
    pwed=theForm.price_weekend.value.replace(",",".");
    pwk=theForm.price_week.value.replace(",",".");
    if(pday != '')
    {
        if(theForm.price_day.value.match(/^[0-9]{1,6}$/))
        {
            if(pday < 1)
            {
                document.getElementById('price_day').className='highlight';
                document.getElementById('price_dayimg').className='hlightimage';
                alert(msgInvalidPrice);
                theForm.price_day.select();
                theForm.price_day.focus();
                return false;
            }
        }
        else
        {
            document.getElementById('price_day').className='highlight';
            document.getElementById('price_dayimg').className='hlightimage';
            alert(msgInvalidPrice);
            theForm.price_day.select();
            theForm.price_day.focus();
            return false;
        }
    }
    if(pwed != '')
    {
        if(theForm.price_weekend.value.match(/^[0-9]{1,6}$/))
        {
            if(pwed < 1)
            {
                document.getElementById('price_weekend').className='highlight';
                document.getElementById('price_weekendimg').className='hlightimage';
                alert(msgInvalidPrice);
                theForm.price_weekend.select();
                theForm.price_weekend.focus();
                return false;
            }
        }
        else
        {
            document.getElementById('price_weekend').className='highlight';
            document.getElementById('price_weekendimg').className='hlightimage';
            alert(msgInvalidPrice);
            theForm.price_weekend.select();
            theForm.price_weekend.focus();
            return false;
        }
    }
    if(pwk != '')
    {
        if(theForm.price_week.value.match(/^[0-9]{1,6}$/))
        {
            if(pwk < 1)
            {
                document.getElementById('price_week').className='highlight';
                document.getElementById('price_weekimg').className='hlightimage'
                alert(msgInvalidPrice);
                theForm.price_week.select();
                theForm.price_week.focus();
                return false;
            }
        }
        else
        {
            document.getElementById('price_week').className='highlight';
            document.getElementById('price_weekimg').className='hlightimage'
            alert(msgInvalidPrice);
            theForm.price_week.select();
            theForm.price_week.focus();
            return false;
        }
    }

    return true;
}

function checkPostOffice(frmObj,objName,blank)
{
    var obj=idiv(objName+'_ele');
    var po=obj.value;
    if(po != '')
    {
        if(!po.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ-]{1,100}$/))
        {
            document.getElementById(objName).className='highlight';
            document.getElementById(objName+'img').className='hlightimage';
            alert(invalidPostOffice);
            obj.select();
            obj.focus();
            return false;
        }
    }
    else if(blank)
    {
        document.getElementById(objName).className='highlight';
        document.getElementById(objName+'img').className='hlightimage';
        alert(msgPostOfficeBlank);
        obj.focus();
        return false;
    }

    return true;
}

function checkZipCode(frmObj,objName,blank)
{
    var obj=idiv(objName+'_ele');
    var zip=obj.value;

    if(!isblank(zip))
    {
        if(!(checkZip(zip)))
        {
            document.getElementById(objName).className='highlight';
            document.getElementById(objName+'img').className='hlightimage';
            alert(msgZip);
            obj.select();
            obj.focus();
            return false;
        }
        else if(frmObj.id_domicile.value != 15)
        {
            if(zip.match(/^[0-9]{5}$/))
            {
                if(zip <= 0)
                {
                    document.getElementById(objName).className='highlight';
                    document.getElementById(objName+'img').className='hlightimage';
                    alert(msgCheckZip);
                    obj.select();
                    obj.focus();
                    return false;
                }
            }
            else
            {
                document.getElementById(objName).className='highlight';
                document.getElementById(objName+'img').className='hlightimage';
                alert(msgCheckZip);
                obj.select();
                obj.focus();
                return false;
            }
        }
    }
    else if(blank)
    {
        document.getElementById(objName).className='highlight';
        document.getElementById(objName+'img').className='hlightimage';
        alert(msgMandatoryFieldEmpty);
        obj.focus();
        return false;
    }

    return true;
}

function checkStringValue(frmObj,objName,msg)
{
    var obj=idiv(objName+'_ele');
    var s=obj.value;

    if(s != '')
    {
        if(!s.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ_´:\s\,-]{1,100}$/))
        {
            document.getElementById(objName).className='highlight';
            document.getElementById(objName+'img').className='hlightimage';
            alert(msg);
            obj.focus();
            return false;
        }
    }

    return true;
}

function checkAddressValue(frmObj,objName,blank)
{
    var obj=idiv(objName+'_ele');
    var a=obj.value;

    if(a != '')
    {
        if(!checkAddContTime(a))
        {
            document.getElementById(objName).className='highlight';
            document.getElementById(objName+'img').className='hlightimage';
            alert(msgAddBlank);
            obj.select();
            obj.focus();
            return false;
        }
    }
    else if(blank)
    {
        document.getElementById(objName).className='highlight';
        document.getElementById(objName+'img').className='hlightimage';
        alert(msgAddBlank);
        obj.focus();
        return false;
    }

    return true;
}

function backToAttachImg(vLink)
{
    document.cottagepost.action=vLink;
    document.cottagepost.backto.value=1;
    document.cottagepost.submit();
    return false;
}

function submitForm() {
    document.cottagepost.submit();
}

function moveFormValue()
{
    document.cottagepost.invoice_name.value=document.saveForm.invoice_name.value;
    document.cottagepost.bill_add.value=document.saveForm.bill_add.value;
    document.cottagepost.bill_zip.value=document.saveForm.bill_zip.value;
    document.cottagepost.bill_postoffice.value=document.saveForm.bill_postoffice.value;
}
function modalAvailForm(frmObj,url,errorDiv,respDiv)
{
    var pars=jQuery(frmObj).serialize();
    jQuery('#avail_loadingImg').css('display','inline');
    jQuery('#saveAvail').attr('disabled',true);

    jQuery.post(url,pars,function(str){
        strArr=str.split('##$$split$$##');
        if(str.substring(0,5) == "error")
        {
            jQuery('#'+errorDiv).html('<div class="message margin_bottom1">' + strArr[0].substring(5) + '</div>');
        }
        else
        {
            jQuery('#'+errorDiv).html('');
            jQuery('#'+respDiv).html(strArr[0]);

            if(jQuery('#priceListInfo').length>0)
                jQuery('#priceListInfo').html(strArr[1]);
            if(jQuery('#priceListReser').length>0)
                jQuery('#priceListReser').html(strArr[1]);
            if(jQuery('#priceListSearch').length>0)
                jQuery('#priceListSearch').html(strArr[1]);
        }
        jQuery('#saveAvail').attr('disabled',false);
        jQuery('#avail_loadingImg').hide();

    });
    return false;
}

function showHideAvail()
{
    jQuery('#availability').toggle();
}

function manageCottageAvail(id_avail,cmd,hashVal,idCottage)
{
    var hashVal = (hashVal == null || hashVal == '') ? hashStr : hashVal;

    jQuery('#avail_loadingImg').css('display','inline');
    submitPage = 'http://'+location.hostname+'/manageCottageAvailability.php?availId='+id_avail+'&cmd='+cmd+'&hash='+hashVal+'&id_cottage='+idCottage;
    jQuery.get(submitPage,'',function(str){
        strArr=str.split('##$$split$$##');
        if(str.substring(0,5) == "error")
        {
            jQuery('#msgAvailError').html('<div class="message margin_bottom1">'+ strArr[0].substring(5)+'</div>');
        }
        else
        {
            jQuery('#msgAvailError').html('');
            jQuery('#ajxAvailForm').html(strArr[0]);

            if(jQuery('#priceListInfo').length>0)
                jQuery('#priceListInfo').html(strArr[1]);
            if(jQuery('#priceListReser').length>0)
                jQuery('#priceListReser').html(strArr[1]);
            if(jQuery('#priceListSearch').length>0)
                jQuery('#priceListSearch').html(strArr[1]);
        }
        jQuery('#avail_loadingImg').hide();
    });
    return false;
}



function modalPriceForm(frmObj)
{
    var pars=jQuery(frmObj).serialize();

    jQuery('#price_loadingImg').css('display','inline');
    jQuery('#savePrice').attr('disabled',true);

    jQuery.post('http://'+location.hostname+'/manageCottagePrice.php',pars,function(str){
        responseArr=str.split('##$$split$$##');

        if(str.substring(0,5) == "error")
        {
            jQuery('#msgPriceError').html('<div class="message margin_bottom1">'+ responseArr[0].substring(5)+'</div>');
        }
        else
        {
            jQuery('#msgPriceError').html('');
            document.getElementById('ajxPriceFrm').innerHTML=responseArr[0];

            if(jQuery('#priceListInfo'))
                jQuery('#priceListInfo').html(responseArr[1]);
            if(jQuery('#priceListReser'))
                jQuery('#priceListReser').html(responseArr[1]);
            if(jQuery('#priceListSearch'))
                jQuery('#priceListSearch').html(responseArr[1]);

            jQuery('#cottage_price').hide();
        }
        jQuery('#savePrice').attr('disabled',false);
        jQuery('#price_loadingImg').hide();
    });
    return false;
}

function showHidePrice(divName)
{
    jQuery('#'+divName).toggle();
}

function manageCottagePrice(id_price,cmd,hashVal,idCottage)
{
    var hashVal = (hashVal == null || hashVal == '') ? hashStr : hashVal;

    jQuery('#price_loadingImg').css('display','inline');
    submitPage='http://'+location.hostname+'/manageCottagePrice.php?priceId='+id_price+'&cmd='+cmd+'&hash='+hashVal+'&id_cottage='+idCottage;

    jQuery.get(submitPage,'',function(str){
        strArr=str.split('##$$split$$##');

        if(str.substring(0,5) == "error")
            jQuery('#msgPriceError').html('<div class="message margin_bottom1">'+ strArr[0].substring(5)+'</div>');
        else
        {
            jQuery('#msgPriceError').html('');
            document.getElementById('ajxPriceFrm').innerHTML=strArr[0];

//            if(jQuery('#priceListInfo'))
//                jQuery('#priceListInfo').html(strArr[0]);
//            if(jQuery('#priceListReser'))
//                jQuery('#priceListReser').html(strArr[0]);
//            if(jQuery('#priceListSearch'))
//                jQuery('#priceListSearch').html(strArr[0]);
        }
        jQuery('#price_loadingImg').hide();
    });


    return false;
}

function showAllPriceHistory(hashVal,idCottage)
{
    var hashVal = (hashVal == null || hashVal == '') ? hashStr : hashVal;

    jQuery('#price_loadingImg').css('display','inline');
    submitPage='http://'+location.hostname+'/manageCottagePrice.php?show_old=1&hash='+hashVal+'&id_cottage='+idCottage;

    jQuery.get(submitPage,'',function(str){
        if(str.substring(0,5) == "error")
            jQuery('#msgPriceError').html('<div class="message margin_bottom1">'+ response.responseText.substring(5)+'</div>');
        else
            jQuery('#ajxCottagePrice').html(str);
        jQuery('#price_loadingImg').hide();
    });
    return false;
}
function CheckfillInfo(f)
{
    var is_owner=f.isOwner.value;

    if(!f.begin_date)
    {
        alert(msgBeginDateBlank);
        return false;
    }
    if(!(f.end_date && trim(f.end_date.value) != '' && trim(f.end_date.value) != 0)) {
        alert(msgEndDateBlank);
        return false;
    }

    if(f.fname && trim(f.fname.value) != '')
    {
        if(!checkFullName(f.fname.value))
        {
            alert(msgFNameBlank);
            f.fname.focus();
            return false;
        }
    }
    else if(f.fname)
    {
        alert(msgFNameBlank);
        f.fname.focus();
        return false;
    }

    if(is_owner == 0)
    {
        if(f.persons[f.persons.selectedIndex].value == 0)
        {
            alert(msgNoOfPersonEmpty);
            f.persons.focus();
            return false;
        }

        if(f.surname && trim(f.surname.value) != '')
        {
            if(!checkFullName(f.surname.value))
            {
                alert(msgLNameBlank);
                f.surname.focus();
                return false;
            }
        }
        else if(f.surname)
        {
            alert(msgLNameBlank);
            f.surname.focus();
            return false;
        }

        if(f.city && trim(f.city.value) != '')
        {
            if(!checkCity(f.city.value))
            {
                alert(msgCityBlank);
                f.city.focus();
                return false;
            }
        }
        else if(f.city)
        {
            alert(msgCityBlank);
            f.city.focus();
            return false;
        }

        if(f.phone && trim(f.phone.value) != '')
        {
            if(!(check(f.phone.value)))
            {
                alert(msgPhoneFormat);
                f.phone.focus();
                return false;
            }
        }
        else if(f.phone)
        {
            alert(msgPhoneBlank);
            f.phone.focus();
            return false;
        }

        if(f.email && trim(f.email.value) != '')
        {
            if(!emailInvalid(f.email.value))
            {
                alert(msgInvalidEmail);
                f.email.focus();
                return false;
            }
        }
        else if(f.email)
        {
            alert(msgEmailBlank);
            f.email.focus();
            return false;
        }
    }
    else
    {
        if(f.surname && trim(f.surname.value) != '')
        {
            if(!checkFullName(f.surname.value))
            {
                alert(msgSurNameInvalid);
                f.surname.focus();
                return false;
            }
        }

        if(f.city && trim(f.city.value) != '')
        {
            if(!checkCity(f.city.value))
            {
                alert(msgCityBlank);
                f.city.focus();
                return false;
            }
        }

        if(f.phone && trim(f.phone.value) != '')
        {
            if(!(check(f.phone.value)))
            {
                alert(msgPhoneFormat);
                f.phone.focus();
                return false;
            }
        }

        if(f.email && trim(f.email.value) != '')
        {
            if(!emailInvalid(f.email.value))
            {
                alert(msgInvalidEmail);
                f.email.focus();
                return false;
            }
        }
    }

    return true;
}

function submitReservation(frmObj,url,container)
{
    var pars=jQuery(frmObj).serialize();

    if(jQuery('#reserve').length>0)
        jQuery('#reserve').attr('disabled',true);

    jQuery('#loadingImg_reservation').css('display','inline');

    pars = pars.replace(/%E2%82%AC/g, '%26euro%3B');
    jQuery.post(url,pars,function(str){
        if(str.substring(0,5) == "error")
            jQuery('#msgReserError').html('<table width="100%" border="0" cellspacing="3" cellpadding="0"><tr ><td height="27" class="message">'+str.substring(5)+'</td></tr></table>');
        else
            jQuery('#'+container).html(str);

        if(jQuery('#reserve').length>0)
            jQuery('#reserve').attr('disabled',false);

        jQuery('#loadingImg_reservation').hide();
        jQuery('#search_result').html('');
    });
    return false;
}

