/* $Id: nc_cottageValidation.js,v 1.4 2009/02/12 09:46:00 kiran 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();
			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(theForm.sector.value != '')
	{
		var s=theForm.sector.value;
		if(!s.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ_´:\s\,-]{1,100}$/))
		{
            document.getElementById('sector').className='highlight';
            document.getElementById('sectorimg').className='hlightimage';
			alert(invalidSector);
 			theForm.sector.focus();
        	return false;
		}
	}
	if(!checkAddContTime(theForm.address.value))
	{
		document.getElementById('address').className='highlight';
		document.getElementById('addressimg').className='hlightimage';
		alert(msgAddBlank);
		theForm.address.select();
		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.select();
			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.select();  					
  					theForm.zip.focus();
  					return false;
  				}
  			}
  			else
  			{
  				document.getElementById('zipcode').className='highlight';
  				document.getElementById('zipcodeimg').className='hlightimage';
  				alert(msgCheckZip);
  				theForm.zip.select();
  				theForm.zip.focus();
  				return false;
  			}
  		}
  	}
  	else
  	{
  		document.getElementById('zipcode').className='highlight';
  		document.getElementById('zipcodeimg').className='hlightimage';
		alert(msgMandatoryFieldEmpty);
		theForm.zip.focus();
		return false;
  	}

	if(theForm.post_office.value != '')
	{
		po=theForm.post_office.value;
		if(!po.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ-]{1,100}$/))
		{
			document.getElementById('post_office').className='highlight';
			document.getElementById('post_officeimg').className='hlightimage';
			alert(invalidPostOffice);
			theForm.post_office.select();
			theForm.post_office.focus();
			return false;
		}
	}
	else
	{
		document.getElementById('post_office').className='highlight';
		document.getElementById('post_officeimg').className='hlightimage';
		alert(msgPostOfficeBlank);
		theForm.post_office.focus();
		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;    
		}
	}
    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;
}
