/* $Id: nc_common.js,v 1.33 2012/01/17 06:27:42 hetal Exp $ */

var _su='http://'+location.hostname;

function AjaxUpdater(updateDiv,url,extraAction)
{
    jQuery.get(url,'',function(data){
        if(updateDiv)
            jQuery('#'+updateDiv).html(data);
        if(extraAction)
            eval(extraAction);
    });
    return false;
}

function idiv(i)
{
    return document.getElementById(i);
}

function loadDefEArea(v1)
{
    if(!trim(v1.value))
    {
        if(trim(v1.name) == 'enterArea')
            v1.value=enterArea;
    }
}

function check_search()
{
    if(document.search.veh_id_reg_no.value=="")
    {
        alert(msgBlankSearchCottage);
        document.search.veh_id_reg_no.focus();
        return false;
    }
    else
    {
        //If cottage id is there
        if(document.search.veh_id_reg_no.value)
        {
            if(document.search.veh_id_reg_no.value.match(/^[0-9]{1,}$/))
            {
                //Strips blank spaces from left and right
                var lre=/^\s*/;
                var rre=/\s*$/;

                document.search.veh_id_reg_no.value=document.search.veh_id_reg_no.value.replace(lre,"");
                document.search.veh_id_reg_no.value=document.search.veh_id_reg_no.value.replace(rre,"");

                self.location.href= _su + '/view.php?id_cottage='+document.search.veh_id_reg_no.value;
            }
            else
            {
                alert(msgNumeric);
                document.search.veh_id_reg_no.focus();
                return false;
            }
        }
    }
    return false;
}
/** Function to increase the hit counter of navigation_ad table */
function navigationHitCounter(id,urlNo)
{
    AjaxUpdater('',_su+'/navigationHitCounter.php?id='+id+'&urlNo='+urlNo);
}
//Function to change year list box options
function yearChange(v1,v2,curval)
{
    var tot=v1.selectedIndex+1;

    if(tot == 1)
        tot=v1.options.length-1;

    if(curval=='' && v2.selectedIndex > 0)
        curval=v2.options[v2.selectedIndex].value;

    v2.length=1;

    for(i=1,j=1; i<tot; j++,i++)
    {
        temp=new Option(v1.options[i].value,v1.options[i].value);
        eval("v2.options[j]=temp");

        if(curval == v1.options[i].value)
            v2.selectedIndex=j;
    }
}

/** Function to change the price,births list box options. */
function lengthPowerPriceChange(v1,v2,curval,pricePerCmb)
{
 	var stopVal;

 	if(pricePerCmb != '')
    {
 		if(pricePerCmb.value == 'price_weekend')
 			stopVal=1001;
 		if(pricePerCmb.value == 'price_week')
 			stopVal=3001;
	 	if(pricePerCmb.value == 'price_day')
 			stopVal=601;
 	}
 	if(v1.name == 'berthsFrom' || v1.name == 'map_berthsFrom')
 		stopVal=31;

    if(v1.selectedIndex < 1)
        var st=1;
    else
        var st=v1.selectedIndex;

    if(curval == '' && v2.selectedIndex > 0)
        curval=v2.options[v2.selectedIndex].value;

    var tot=v1.length;
    v2.length=1;

    if(v1.value < stopVal)
    {
		for(i=st,j=1; i < tot; j++,i++)
        {
	  		var lstDispVal=v1.options[i].value;
    		var str_pr=lstDispVal.toString();
			var toLng=str_pr.length;

			if(toLng >= 3)
            {
				str_pr1=str_pr.substr(0,toLng - 3);
				str_pr2=str_pr.substr(toLng - 3,toLng);
				str_make=str_pr1+' '+str_pr2+' '+'€';
			}
            else
                str_make=lstDispVal+' '+'€';

    		lstDispVal=str_make;

    		if(v1.name == 'berthsFrom' || v1.name == 'map_berthsFrom')
                temp=new Option(v1.options[i].value,v1.options[i].value);
            else
                temp=new Option(lstDispVal,v1.options[i].value);

			eval('v2.options[j]=temp');

	  		if(curval == v1.options[i].value)
                v2.selectedIndex=j;
		}
		var lstDispVal=stopVal-1;
		var str_pr=lstDispVal.toString();
		var toLng=str_pr.length;

		if(toLng >= 3)
        {
			str_pr1=str_pr.substr(0,toLng - 3);
			str_pr2=str_pr.substr(toLng - 3,toLng);
			str_make=str_pr1+' '+str_pr2+' '+'€';
		}
        else
			str_make=lstDispVal+' '+'€';

		if(v1.name == 'berthsFrom' || v1.name == 'map_berthsFrom')
			lstDispVal=stopVal-1;
        else
			lstDispVal=str_make;

		temp=new Option(capOver+' '+lstDispVal,stopVal);
		eval('v2.options[j]=temp');

		var lstMaxVal=v2[v2.length-1].value;

		if(curval == lstMaxVal)
            v2.selectedIndex=j;

		document.srch.lstMaxVal.value=lstMaxVal;
	}
    else
    {
		for(i=st,j=1; i < tot; j++,i++)
    	{
    		var lstDispVal=v1.options[i].value;
        	var str_pr=lstDispVal.toString();
			var toLng=str_pr.length;

			if(toLng >= 3)
            {
				str_pr1=str_pr.substr(0,toLng - 3);
				str_pr2=str_pr.substr(toLng - 3,toLng);
				str_make=str_pr1+' '+str_pr2+' '+'€';
			}
            else
				str_make=lstDispVal+' '+'€';

        	lstDispVal=str_make;

        	if(v1.name == 'berthsFrom' || v1.name == 'map_berthsFrom')
        		temp=new Option(v1.options[i].value,v1.options[i].value);
        	else
        		temp=new Option(lstDispVal,v1.options[i].value);

        	eval('v2.options[j]=temp');

        	if(curval == v1.options[i].value)
                v2.selectedIndex=j;
    	}
	}
}

//Function to open new pop up window with variable width and height
function openImg(img_nm,w,h,nm)
{
    var myfeatures="directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",left=0,top=0";

    if(nm == "")
        nm="new";

    var newImg=open(img_nm,nm,myfeatures);
    newImg.focus();
}

//Function opens window of desired size by giving proper alert window
function doLogin(msgVar,width,height)
{
    openImg(_su + '/msgWin.php?opt='+msgVar,width,height,'newuser');
}

//Function to submit favorite page by adding/removing cottages
function submitFav(thisV)
{
    frmObj=document.fav;
    var i,flag='N',checked_value,img_id='';

    for(i=1; i < document.fav.length; i++)
    {
        if(document.fav.elements[i].type == 'checkbox')
        {
            if(document.fav.elements[i].checked)
            {
                flag='Y';
                continue;
            }
        }
    }
    if(flag != 'Y')
    {
        alert(msgSelectAtLeastOne);
        return false;
    }

    if(thisV == 'remove')
        document.fav.fav_opt.value='Rem';
    else
        document.fav.fav_opt.value='Fav';

    /*  AJAX code starts */

	if(thisV != 'remove')
	{
        jQuery('#loadingImg').css('display', 'inline');
        jQuery('#loadingImg2').css('display', 'inline');

        var pars=jQuery(frmObj).serialize();
        var sub_url = document.fav.action;

        jQuery.post(sub_url,pars,function(str){
            arr=str.split('#');
            if(arr[0] != '0')
            {
                jQuery('#favorite_cnt').html('('+arr[0]+')');
                jQuery('#favorite_cnt_end').html('('+arr[0]+')');
            }
            if(arr[1] != '0')
            {
                jQuery('#common_messageX').html(arr[1]+' '+msgAddFav);
                jQuery('#Add_To_Fav_End').html(arr[1]+' '+msgAddFav);
            }
            else
            {
                jQuery('#common_messageX').html(msgRptAddFav);
                jQuery('#Add_To_Fav_End').html(msgRptAddFav);
            }
            jQuery('#common_messageX').css('display', 'block');
            jQuery('#loadingImg').hide();
            jQuery('#Add_To_Fav_End').css('display', 'block');
            jQuery('#loadingImg2').hide();

        });

        //  Remove checked items
        for(i=1; i < document.fav.length; i++)
        {
            if(document.fav.elements[i].type == 'checkbox')
            {
                if(document.fav.elements[i].checked)
                {
                    document.fav.elements[i].checked=false;

                    if(thisV != 'remove')
                    {
                        checked_value=document.fav.elements[i].value;
                        changeClass(checked_value,'listing');
                    }
                    continue;
                }
            }
        }

        return false;
	}
    else
        document.fav.submit();
}

/* Argument 'label' can be the entity name you want to delete,
	for exa., user,dealer,cottage etc. and 'nm' can be the user name,
	dealer name or cottage id etc.
*/
function newConfirmDel(ref,label,nm)
{
	if(confirm(msgNewDeleteConfirm+" "+label+", '" + nm + "'?"))
		window.location.replace(ref);
}

//Function to return nothing, this is used sometimes to display tool tip on plain text by making trick
function returnVoid() {}

function monthcheck(mId,stCnt,endCnt)
{
    var chk=document.getElementById(mId);
    var mCnt=0;

    if(chk && chk.checked)
    {
        for(mCnt=stCnt; mCnt<=endCnt; mCnt++)
        {
            document.getElementById('week_'+mCnt).checked=true;
        }
    }
    else if(chk && !chk.checked)
    {
        for(mCnt=stCnt; mCnt<=endCnt; mCnt++)
        {
            document.getElementById('week_'+mCnt).checked=false;
        }
    }
}

/** function to change available date combo. */
function dateChange(v1,v2,curval)
{
    if(v1.selectedIndex < 1)
        var st=1;
    else
        var st=v1.selectedIndex;

    if(curval=='' && v2.selectedIndex > 0)
        curval=v2.options[v2.selectedIndex].value;

    var tot=v1.length;
    v2.length=1;

    for(i=st,j=1; i < tot; j++,i++)
    {
        temp=new Option(v1.options[i].value,v1.options[i].value);
        eval("v2.options[j]=temp");

        if(curval == v1.options[i].value)
            v2.selectedIndex=j;
    }
}

/** function to set the focus on distance text box when clieck on check box */
function set_focus(v1)
{
	if(v1 != '')
	{
		if(document.cottagepost.elements["acc_conn["+v1+"]"])
		{
			var obj=eval(document.cottagepost.elements["acc_conn["+v1+"]"]);
			obj.focus();
		}
	}
}

/** function to validation of the numeric value of text box. */
function number_check(v,v1)
{
  	var val=v.value.replace(',','.');
	if(val > 0)
	{
		var obj=eval(document.cottagepost.elements["acc["+v1+"]"]);
		obj.checked=true;
		return true;
  	}
  	if(isNaN(val))
	{
		var obj=eval(document.cottagepost.elements["acc["+v1+"]"]);
		obj.checked=false;
  		alert(msgNumeric);
		v.value='';
		v.focus();
		return false;
    }
}

/** function to change price combo in simple search.
    v1 price period combo name(day,week_end,week),v2 name price from combo,v3 name price to combo.
*/
function newPriceCombo(v1,v2,v3,curv1,curv2,cap1,cap2)
{
	var start,end,inc,totOptions;
	var optArray=new Array();

	if(v1.value == 'price_week')
    {
		end=3000;
		var optArray=new Array('100','200','300','400','500','600','700','800','900','1000','1500','2000','2500','3000');
	}
	if(v1.value == 'price_day')
    {
		start=0;
		end=600;
		inc=20;
	}
	if(v1.value == 'price_weekend')
    {
		end=1000;
		var optArray=new Array('50','150','200','250','300','400','500','600','700','800','900','1000');
	}
	v2.length=0;
	v3.length=0;

	temp=new Option(cap1,'');
    eval('v2.options[0]=temp');

    temp=new Option(cap2,'');
    eval('v3.options[0]=temp');

    if(curv1 == '' && v2.selectedIndex > 0)
        curv1=v2.options[v2.selectedIndex].value;

    if(curv2 == '' && v3.selectedIndex > 0)
        curv2=v3.options[v3.selectedIndex].value;

    /** create price per day combo value here. */
    if(v1.value == 'price_day')
    {
    	var totOptions=(parseInt(end)/parseInt(inc));

		for(i=0;i<=totOptions;i++)
        {
			j=i+1;
			var val=(parseInt(i)*parseInt(inc));
			var str_pr=val.toString();
			var toLng=str_pr.length;

			if(toLng > 3)
            {
				str_pr1=str_pr.substr(0,toLng - 3);
				str_pr2=str_pr.substr(toLng - 3,toLng);
				str_make=str_pr1+' '+str_pr2+' '+'€';
			}
            else
                str_make=val+' '+'€';

			val_euro=str_make;

			temp=new Option(val_euro,val);
       		eval('v2.options[j]=temp');

      		if(curv1 == v2.options[j].value)
                v2.selectedIndex=j;

 			temp1=new Option(val_euro,val);
       		eval('v3.options[j]=temp1');

	       	if(curv2 == v3.options[j].value)
                v3.selectedIndex=j;

        	if(val == end)
            {
				aboveVal=val+1;
				temp=new Option(capOver+' '+val_euro,aboveVal);
       			eval('v3.options[j+1]=temp');

      			if(curv2 == v3.options[j+1].value)
                    v3.selectedIndex=j+1;
			}
 		}
	}
 	/**  Create price per week and weekend combo value here. */
 	if(v1.value == 'price_weekend' || v1.value == 'price_week' || v2.name=='berthsFrom' || v2.name=='map_berthsFrom')
    {
 		var totOptions=optArray.length;

 		for(i=0; i < totOptions; i++)
        {
 			j=i+1;
			var val=optArray[i];
			var str_pr=val.toString();
			var toLng=str_pr.length;

			if(toLng > 3)
            {
				str_pr1=str_pr.substr(0,toLng -3);
				str_pr2=str_pr.substr(toLng - 3,toLng);
				str_make=str_pr1+' '+str_pr2+' '+'€';
			}
            else
                str_make=val+' '+'€';

			val_euro=str_make;
 			temp=new Option(val_euro,val);
       		eval('v2.options[j]=temp');

      		if(curv1 == v2.options[j].value)
                v2.selectedIndex=j;

            temp1=new Option(val_euro,val);
       		eval('v3.options[j]=temp1');

	       	if(curv2 == v3.options[j].value)
    	        v3.selectedIndex=j;

        	if(val == end)
            {
				aboveVal=(parseInt(val)+parseInt(1));
				temp=new Option(capOver+' '+val_euro,aboveVal);
       			eval('v3.options[j+1]=temp');

      			if(curv2 == v3.options[j+1].value)
                    v3.selectedIndex=j+1;
			}
 		}
 	}
}

/** Function for confirmation of when cottage deletes. */
function confirmDelCottage(ref)
{
    if(confirm(msgDeleteCottageConfirm))
        window.location.replace(ref);
}

/** Function for combo's last maximum value. */
function lastMaxCmbval(v1,frm,hidVar)
{
	var lstval=v1[v1.length-1].value;
	var obj=eval('document.'+frm.name+'.'+hidVar.name+'.value'+'='+lstval);
}

/** Function for form's all element should clear. */
function clearFormData(frm)
{
	var obj=eval('document.'+frm.name+'.elements');

	for(i=0; i < obj.length; i++)
    {
		if(obj[i].type == 'select-one')
			obj[i].selectedIndex=0;
		if(obj[i].type == 'checkbox' || obj[i].type == 'radio')
            obj[i].checked=false;
		if(obj[i].type == 'text')
			obj[i].value='';
	}
}
/** Function to change berths value from combo in simple search. */
function berthsCmbVal(v1,v2,curv1,curv2)
{
	var end=30;
	var optArray=new Array('1','2','3','4','5','6','7','8','9','10','12','14','16','18','20','25','30');
	var totOptions=optArray.length;

	for(i=0;i<totOptions;i++)
    {
		j=i+1;
		var val=optArray[i];
		temp=new Option(val,val);
   		eval('v1.options[j]=temp');

   		if(curv1 == v1.options[j].value)
        	v1.selectedIndex=j;

        temp1=new Option(val,val);
       	eval('v2.options[j]=temp1');

	    if(curv2 == v2.options[j].value)
            v2.selectedIndex=j;

    	if(val == end)
        {
			aboveVal=(parseInt(val)+parseInt(1));
			temp=new Option(capOver+' '+val,aboveVal);
       		eval('v2.options[j+1]=temp');

      		if(curv2 == v2.options[j+1].value)
           		v2.selectedIndex=j+1;
		}
 	}
}
/** Function to check the given value is string or not. */
function str_check(v1)
{
  	if(v1 && !isNaN(v1.value))
	{
  		alert(msgString);
  		v1.value='';
		v1.focus();
    }
}

function confirmReset(thisForm)
{
    if(confirm(msgConfirmReset))
        return true;

    return false;
}

/** Function for domicile and town list in advanced search. */
function addAny(v1,v2)
{
	if(v1.options.length == 0)
	{
		selOpt=new Option("","");
		eval("v1.options[0]=selOpt");
	}
	if(v2.options.length == 0)
	{
		selOpt=new Option("","");
		eval("v2.options[0]=selOpt");
	}
}
// Removes leading whitespaces
function LTrim(value)
{
	var re=/\s*((\S+\s*)*)/;
	return value.replace(re,'$1');
}
// Removes ending whitespaces
function RTrim(value)
{
	var re=/((\s*\S+)*)\s*/;
	return value.replace(re,'$1');
}
// Removes leading and ending whitespaces
function trim(value)
{
	return LTrim(RTrim(value));
}

function checkPage(theForm,pageVal)
{
	if(theForm.gotopage.value.match(/^[0-9]{1,3}$/))
	{
		if(theForm.gotopage.value != 0)
		{
			if(theForm.gotopage.value > pageVal)
			{
				alert(msgPageNotAvailable);
				return false;
			}
			else
				return true;
		}
		else
		{
			alert(notValidPageNumber);
			return false;
		}
	}
	else
	{
		alert(notPageVal);
		return false;
	}
}

//	Functions for changing background color of updated row(s) when posting question or answers for Boats.

function whiteBG()
{
	var elements = document.getElementsByTagName("tr");
	for(var i = 0;i < elements.length;i++)
	{
		if(elements[i].id == "TR_BG" && elements[i].className == "bgyellow" )
		{
			elements[i].className = "bgwhite";
		}
	}
	setTimeout("yellowBG();",500);
}
function yellowBG()
{
	var elements = document.getElementsByTagName("tr");
	for(var i = 0;i < elements.length;i++)
	{
		if(elements[i].id == "TR_BG" && elements[i].className == "bgwhite")
		{
			elements[i].className = "bgyellow";
		}
	}
	setTimeout("oddevenBG();",500);
}
//		Function for doing normal background.
function oddevenBG()
{
	var elements = document.getElementsByTagName("tr");
	for(var i = 0;i < elements.length;i++)
	{
		if(elements[i].id == "TR_BG")
		{
			if(i % 2 == 0)
				elements[i].className = "even";
			else
				elements[i].className = "odd";
		}
	}
}

//		Simple dealer que-ans list through AJAX
function sd_ajaxQue(formObj,var_O)
{
    if(document.getElementById('nc_id_user').value == "")
        return false;

    if(checkQue(formObj,var_O) != false )
    {
        var pars=jQuery(formObj).serialize();
        if(formObj.replyToQue.value == 0)
        {
            formObj.que.disabled=true;
            formObj.save.disabled=true;
            jQuery('#loaderimg').css('display','inline');
        }
        /* replacing € with &euro; */
        pars = 'ajxQueAns=1&'+pars.replace(/%E2%82%AC/g,'%26euro%3B');
        jQuery.post(_su+'/simple_dealer/sd_questionAnswer.php?ajxQueAns=1&',pars,function(data){
            jQuery('#submitXQA').html(data);
            setTimeout("whiteBG();",1000);
            jQuery('#loaderimg').hide();
            initResizeQues('que');
        });
        return false;
    }
    else
        return false;
}
function sd_ajaxLoadImage(id)
{
    var qa_id = id;

    if(document.getElementById('nc_id_user').value == "")
        return false;

    if(checkQue(document.frmqueans) != false )
    {
        var answer = document.frmqueans.replyToQue.value;
        var pars=jQuery(document.frmqueans).serialize();

        if(answer == 1)
        {
            var loader_img_id='loaderimg'+qa_id;
            var ans_id='ans'+qa_id;
            var anssave_id='anssave'+qa_id;

            jQuery('#'+ans_id).attr('disabled',true);
            jQuery('#'+anssave_id).attr('disabled',true);
            jQuery('#'+loader_img_id).css('display','inline');
        }

        /* replacing € with &euro; */
        pars = 'ajxQueAns=1&'+pars.replace(/%E2%82%AC/g,'%26euro%3B');
        jQuery.post(_su+'/simple_dealer/sd_questionAnswer.php?ajxQueAns=1&',pars,function(data){
            jQuery('#submitXQA').html(data);
            setTimeout("whiteBG();",1000);
        });
        return false;
    }
    else
        return false;
}

function domTownModal()
{
    pageURL=_su + '/modalSelectDomTown.php';

    if(document.srch.selTown.value)
        pageURL+='?selTown='+document.srch.selTown.value;

    jQuery.fn.colorbox({
        href:pageURL
    });
}

function checkFaBbl()
{
    var aT="";

    if(document.bblsrch.id_sel_town.length > 0)
    {
        for(a=0; a<document.bblsrch.id_sel_town.length-1; a++)
        {
            if(document.bblsrch.id_sel_town[a].value != "")
                aT += document.bblsrch.id_sel_town[a].value+",";
        }
        if(document.bblsrch.id_sel_town[a].value != "")
            aT += document.bblsrch.id_sel_town[a].value;

        document.bblsrch.selTownBubble.value=aT;
        document.srch.selTown.value=aT;
    }
    callAjax(document.srch);
    return true;
}

//		Fn. for Login modal
function loginmodal(opt,rPage)
{
	//  D => Simple Dealer , N => From View Page , P => Post Ad, RP => any given redirect page rPage
    url=_su+'/modalLogin.php?mainOnly=1';
    if(opt == 'D')
        pageURL = url+'&sd=1&fwdUrl='+location.href.replace('#','_hash_');
    else if(opt == 'N')
    {
        var srchHash=location.href.search("#");
        if(srchHash > 0)
            pageURL = url+'&sd=0&fwdUrl='+location.href.replace('#','_hash_');
        else
            pageURL = url+'&sd=0&fwdUrl='+location.href;
    }
    else if(opt == 'P')
        pageURL = url+'&postad=1';
    else if(opt == 'RP')
        pageURL = url+'&redirectPage='+rPage;
    else if(opt == 'POP')
        pageURL = url + '&fwdUrl='+rPage+'&opt='+opt;
    else if(opt=='LA') //login authentication
        pageURL=url+'&redirectPage='+rPage+'&opt='+opt;
    else if(opt != 'P' && location.href.search('advSearch') == -1)
    {
        document.queAsk.que.value = '';
        idiv('que').blur();
    }

    if (jQuery('#contact').length>0)
        jQuery('#contact').hide();

    jQuery.fn.colorbox({
        href:pageURL,onComplete:function(){jQuery('#loginid').focus();}
    });
}

function modalForm(frmObj)
{
	var flag='N';
	var page_from = frmObj.page_from.value;
	var login_page;

    if(frmObj.postad.value == 1)
        var returnURL=_su + '/postAd.php';
    else if(frmObj.redirectPage && frmObj.redirectPage.value != '')
		var returnURL=frmObj.redirectPage.value;
	else
        var returnURL=location.href;

	if(page_from.search('simple_dealer') == -1)
		login_page=_su + '/login.php?ajxLogin=1';
	else
		login_page='../login.php?ajxLogin=1';

	for(i=0; i < frmObj.elements.length;i++)
	{
		if(frmObj.elements[i].type == 'text')
		{
			if(!isblank(frmObj.elements[i].value))
				flag='Y';
		}
	}
	if(frmObj.passwd.value.split(' ').join('').length == 0)
		flag='N';

	if(flag == 'N')
	{
		alert(msgMandet);
		return false;
	}

	if(flag == 'Y')
	{
	    jQuery("input[name=login]").attr('disabled',true);
        jQuery("input[name=btnClose]").attr('disabled',true);
        jQuery("#loadingImgModalLogin").show();
        var pars=jQuery(frmObj).serialize();
        jQuery.post(login_page + ((login_page.indexOf('?') > 0) ? '&' : '?'),pars,function(data){
            if(frmObj.postad.value == 1 && data == "success")
                window.location.href = _su+"/postAd.php";
            else if(data == "success")
            {
                if(frmObj.back.value.match(/verifyEmail.php/))
                	window.location.href = frmObj.back.value;
                else if(frmObj.back.value != '' && frmObj.opt.value == 'POP')
                    window.location.href = frmObj.back.value+'#qa';
                else if(frmObj.back.value != '' && frmObj.opt.value == 'LA')
                    window.location.href = frmObj.redirectPage.value;
                else if(_sCoDe != 'NF' && frmObj.back.value != '' && frmObj.back.value.indexOf('advSearch.php') != '-1')
                {
                    saveAdvSearchData();
                }
                else
                    window.location.reload();
            }
            else if(data == "toHomePage")
                location.href='/';
            else
                jQuery('#msgLoginError').html('<table width="100%" border="0" cellspacing="3" cellpadding="0"><tr><td height="27" class="message">' + data + '</td></tr></table>');
            jQuery("input[name=login]").attr('disabled',false);
            jQuery("input[name=btnClose]").attr('disabled',false);
            jQuery("#loadingImgModalLogin").hide();
        });
	}
	return false;
}

//		Check send link form
function checkFL()
{
    if(isblank(document.sendlink.name.value))
    {
        alert(msgFullFLname);
        document.sendlink.name.focus();
        return false;
    }
    if(!document.sendlink.name.value.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´_\s-]{1,50}$/))
    {
        alert(msgFullFLname);
        document.sendlink.name.focus();
        document.sendlink.name.select();
        return false;
    }

    if(!emailInvalid(document.sendlink.userEmail.value))
    {
        alert(msgInvalidEmail);
        document.sendlink.userEmail.focus();
        return false;
    }
    if(isblank(document.sendlink.email.value))
    {
        alert(msgInvalidEmail);
        document.sendlink.email.focus();
        return false;
    }
    s = document.sendlink.email.value;
    removeSpace = s.replace(/ /g,"");
    str1=removeSpace.split(",");

    if(str1.length > 10)
    {
        alert(msgMoreThan10Mail);
        document.sendlink.email.focus();
        return false;
    }
    else
    {
        for(var m=0; m<str1.length; m++)
        {
            if(!emailInvalid(str1[m]))
            {
                alert(msgInvalidEmail);
                document.sendlink.email.focus();
                return false;
            }
        }
    }
    return true;
}
/** Function to change the video status. */
function changeVideoStatus(idAd,videoStatus,idReporter)
{
    var reporterSuffix;

    if(confirm(msgChangeStatus))
    {
        if(idReporter == '')
        {
            reporterSuffix='';
            idReporter='';
        }
        else
            reporterSuffix='_'+idReporter;

        var pars='id_cottage='+idAd+'&video_status='+videoStatus+'&id_reporter='+idReporter+'&ajxifrm=A';
        jQuery.post(_su+'/extra/admin/changeVideoStatus.php',pars,function(str){
            jQuery('#vStatus'+idAd+reporterSuffix).html(str);
        });

    }
    else
        return;
}
/** Function for confirmation when video ad is delete. */
function confirmVideoDelete(ref)
{
    if(confirm(msgDeleteVideoConfirm))
        window.location.replace(ref);
}
/**
Function to play the video at given id of element.
id_element=>id of element where to display the video.
videoUrl=>viewVideo.php file.
*/
function playVideo(id_element,videoUrl)
{
    AjaxUpdater(id_element,videoUrl+((videoUrl.indexOf('?') > 0) ? '&' : '?')+'ajxifrm=A');
}
/** call Ajax function for search result counter on change elements value. */
function callAjax(frmObj)
{
   /** for write hidden parameter selected model & selected acc and validate inspected motmonth & motyear */
   var ajaxFName=_su + '/ajaxSearchCounter.php?ajxifrm=A&site=NC';
   var value='';
   if(frmObj.name == 'adv')
   {
       checkFa();
       value = ajaxFName+'&frm=ADV&'+ cleanFrmUrl(frmObj);
       jQuery('#loadingImg_src_chngCntAdv').css('display','inline');
   }
   else if(frmObj.name == 'srch')
   {
       value = ajaxFName+'&frm=SIMP&'+ cleanFrmUrl(frmObj);
       jQuery('#loadingImg_src_chngCnt').css('display','inline');
   }
   var ajaxFname=value.replace('/#/ig','@');

   jQuery.get(ajaxFname,'',function(data){
       showResponse(data);
       if(frmObj.name == 'adv')
           jQuery('#loadingImg_src_chngCntAdv').hide();
       else if(frmObj.name == 'srch')
           jQuery('#loadingImg_src_chngCnt').hide();
   });
   return false;
}
/** This function is return whole url with append of selected search criteria*/
function cleanFrmUrl(qs)
{
   var len =qs.elements.length;
   var u='';

   u=u.replace(/\s/g,'-').toLowerCase();

   for(var i = 0; i < len; i++)
   {
       var e=qs.elements[i];

       if(e.name == 'search')
          continue;

       if(e.type == 'select-multiple')
       {
           var si=new Array();
           var count=0;
           var k;
           while (e.selectedIndex != -1)
           {
               if (e.selectedIndex != 0)
               {
                   u=u+e.name+'='+e.options[e.selectedIndex].value+'&';
                   si[count++]=e.selectedIndex;
               }
               e.options[e.selectedIndex].selected=false;
           }
           for(k=0; k<si.length; k++)
               e.options[si[k]].selected=true;
       }
       else if(((e.type == 'checkbox' || e.type == 'radio') && e.checked && e.value))
       {
           u=u+e.name+'='+e.value+'&';
       }
       else if(e.value && !isblank(e.value) && e.type != 'checkbox' && e.type != 'radio')
       {
           if(e.value.indexOf('#') != -1)
               u=u+e.name+'='+escape(e.value)+'&';
           else
               u=u+e.name+'='+e.value+'&';
       }
   }
   return u.substr(0,u.length-1);
}
/** Function to check whether given string is really empty or not */
function isblank(s)
{
   for(var i=0; i < s.length; i++)
   {
       var c=s.charAt(i);

       if((c != ' ') && (c != "\n") && (c != "\t"))
           return false;
   }
   return true;
}

function cleanRewriteUrl(str)
{
    var rExps=[{re:/[\xC0-\xC6]/gi, ch:'a'},{re:/[\xE0-\xE6]/gi, ch:'a'},{re:/[\xC8-\xCB]/gi, ch:'e'},{re:/[\xE8-\xEB]/gi, ch:'e'},{re:/[\xCC-\xCF]/gi, ch:'i'},{re:/[\xEC-\xEF]/gi, ch:'i'},{re:/[\xD2-\xD6]/gi, ch:'o'},{re:/[\xF2-\xF6]/gi, ch:'o'},{re:/[\xD9-\xDC]/gi, ch:'u'},{re:/[\xF9-\xFC]/gi, ch:'u'},{re:/[\xD1]/gi, ch:'n'},{re:/[\xF1]/gi, ch:'n'},{re:/[^a-zA-Z0-9\/_|+ -]/gi, ch:''},{re:/[\/_| -]+/gi, ch:'-'}];

    for(var i=0, len=rExps.length; i<len; i++)
        str=str.replace(rExps[i].re, rExps[i].ch);

    return str.toLowerCase();
}

function cleanUrl(qs)
{
    var dm=tn='';
    var u=_su;
    var l =qs.elements.length;
    var arrSelTn = qs.elements.selTown.value.split(',');

    if(arrSelTn.length == 1 && qs.elements.selTown.value)
    {
        if(qs.elements.selTown.value.match('d'))
        {
            dm = qs.elements.selTown.value.replace('d','');
            val = idiv('selected_town_list_'+qs.elements.selTown.value).innerHTML;
            arrTnVal = val.split(', ');

            arrRmSp = arrTnVal[1].split('<');
            dmtnVal = arrRmSp[0];

            if(dm && dm != 15)
                u=u+'/'+cleanRewriteUrl(dmtnVal);
            else if(dm == 15)
                u=u+'/'+'ulkomailla';

            dm=qs.elements.selTown.value='';
        }
        else
        {
            tn = qs.elements.selTown.value;
            val = idiv('selected_town_list_'+tn).innerHTML;
            arrTnVal = val.split(',');

            tnVal = arrTnVal[0];
            arrRmSp = arrTnVal[1].split('<');
            dmVal = arrRmSp[0].replace(' ','');
            dmtnVal = cleanRewriteUrl(dmVal)+'/'+cleanRewriteUrl(tnVal);

            if(tn && tn != 451)
                u=u+'/'+dmtnVal;

            tn=qs.elements.selTown.value='';
        }
        u=u+'?';
    }
    else
        u=u+'/haku?';

    u=u.replace(/\s/g,'-').toLowerCase();

    for(var i = 0; i < l; i++)
    {
        var e=qs.elements[i];

        if(e.name!='search')
        {
            if(e.value && e.type == 'hidden')
                u=u+e.name+'='+e.value+'&';
            else if((e.value && e.type == 'select-one') || (e.type == 'checkbox' && e.checked))
                u=u+e.name+'='+e.value+'&';
            else if(e.type == 'select-multiple')
            {
                while (e.selectedIndex != -1)
                {
                    if (e.selectedIndex != 0)
                        u=u+e.name+'='+e.options[e.selectedIndex].value+'&';
                    e.options[e.selectedIndex].selected = false;
                }
            }
            else if(e.type != 'checkbox' && e.type != 'radio' && e.value && !isblank(e.value) && e.value != enterArea)
            {
                if(e.value.indexOf('#') != -1)
                    u=u+e.name+'='+escape(e.value)+'&';
                else
                    u=u+e.name+'='+e.value+'&';
            }
        }
    }
    window.location=u.substr(0,u.length-1);
    return false;
}

function getDomTown(domVal,curval,page,srch)
{
    var townStr='';
    var srchStr='';
    var mapId='';

    if(srch == 'map')
    {
        mapId='_map';
        srchStr='&srch='+srch;
    }

    var townUrl=_su+'/ajaxGetDomicileTown.php?ajxifrm=A&frm='+page+'&site=NC'+srchStr+'&';
    if(curval) townStr='&sel_id_town='+curval;

    jQuery.get(townUrl+'&id_domicile='+domVal+townStr,function(data){
        if(page == 'ADV')
            divId='adv_id_town';
        else if(page == 'BBLSIMP')
            divId='bbl_simp_id_town';
        if(page == 'SIMP')
            divId='srch_id_town';
        else if(page == 'POST')
            divId='post_id_town';
        else if(page == 'listService')
            divId='service_id_town';
        else if(page == 'EDIT_DEALER')
            divId='dealer_id_town';

        if(divId)
            jQuery('#'+divId+mapId).html(data);

        if(page == 'ADV')
            callAjax(document.adv);
        else if(page == 'BBLSIMP')
            makeAllTownSelected();
        else if(page == 'SIMP' && (srch != 'map' && srch != 'qs'))
            callAjax(document.srch);
    });
}

/** Make "Any" town selected on Domicile click*/
function makeAllTownSelected()
{
    var tnBbl = document.bblsrch.id_town_bubble;
    var townVal = tnBbl.options[0].value;
    var domTxt = document.bblsrch.lastDomicileBubble.value;
    var addAnyTown = true;

    if(tnBbl.name == 'id_town_bubble' && townVal)
    {
        if(townVal.match(/d/))
        {
            bblTownArr = document.bblsrch.id_sel_town;
            for(var i=0;i < bblTownArr.length;i++)
            {
                btl = bblTownArr.length;
                for(var j=0;j<btl;j++)
                {
                    tmpText = bblTownArr[j].text.split(' - ');
                    if(domTxt == tmpText[0])
                    {
                        addAnyTown = false;
                        break;
                    }
                }
            }
        }
    }

    if(addAnyTown == true)
    {
        tnBbl.options[0].selected = true;
        if(document.srch.selTown.value == '')
            document.srch.selTown.value = tnBbl.options[0].value;
        else
            document.srch.selTown.value += ','+tnBbl.options[0].value;

        if(document.bblsrch.selTownBubble.value == '')
            document.bblsrch.selTownBubble.value = tnBbl.options[0].value;
        else
            document.bblsrch.selTownBubble.value += ','+tnBbl.options[0].value;
        addSelTown(tnBbl,document.bblsrch.id_sel_town);
        callAjax(document.srch);
    }
}
/** Function to open the new window with given width and height
 * USE : When User connected with Facebook,Have permission to share updated ad.
 * */
function openNewWin(varUrl,w,h,varTitle,varLeft,varTop)
{
    var varLeft,varTop;
    if(varLeft == '')
        varLeft='0';

    if(varTop == '')
        varTop='0';

    var myfeatures='width='+w+',height='+h+',left='+varLeft+',top='+varTop+',directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,resizable=no,scrollbars=no';

    if(varTitle == '')
        varTitle='new';

    var newWin=window.open(varUrl,varTitle,myfeatures);
    newWin.focus();
}
function loadListingMap()
{
    isListingMapLoaded = false;
    showAddress(map_address,'','N',"list_map_canvas");
}
function listingMap(mapadd)
{
    setTimeout(function(){
        map_address = mapadd;
        if(isListingMapLoaded)
        {
            var script = document.createElement("script");
            script.type = "text/javascript";

            script.src = "http://maps.google.fi/maps?file=api&v=2&key="+MAP_API_KEY+"&async=2&hl=2&callback=loadListingMap";

            document.body.appendChild(script);
        }
        else
            loadListingMap();
    },500);

}
function cacheMapSearchDomicile(imgpath)
{
    mapSearchDomicile[0]=new Image(167,312); mapSearchDomicile[0].src=imgpath+'/map/map.gif';
    mapSearchDomicile[1]=new Image(167,312); mapSearchDomicile[1].src=imgpath+'/map/map1.gif';
    mapSearchDomicile[2]=new Image(167,312); mapSearchDomicile[2].src=imgpath+'/map/map2.gif';
    mapSearchDomicile[3]=new Image(167,312); mapSearchDomicile[3].src=imgpath+'/map/map3.gif';
    mapSearchDomicile[4]=new Image(167,312); mapSearchDomicile[4].src=imgpath+'/map/map4.gif';
    mapSearchDomicile[5]=new Image(167,312); mapSearchDomicile[5].src=imgpath+'/map/map5.gif';
    mapSearchDomicile[6]=new Image(167,312); mapSearchDomicile[6].src=imgpath+'/map/map6.gif';
    mapSearchDomicile[7]=new Image(167,312); mapSearchDomicile[7].src=imgpath+'/map/map7.gif';
    mapSearchDomicile[8]=new Image(167,312); mapSearchDomicile[8].src=imgpath+'/map/map8.gif';
    mapSearchDomicile[9]=new Image(167,312); mapSearchDomicile[9].src=imgpath+'/map/map9.gif';
    mapSearchDomicile[10]=new Image(167,312); mapSearchDomicile[10].src=imgpath+'/map/map10.gif';
    mapSearchDomicile[11]=new Image(167,312); mapSearchDomicile[11].src=imgpath+'/map/map11.gif';
    mapSearchDomicile[12]=new Image(167,312); mapSearchDomicile[12].src=imgpath+'/map/map12.gif';
    mapSearchDomicile[13]=new Image(167,312); mapSearchDomicile[13].src=imgpath+'/map/map13.gif';
    mapSearchDomicile[14]=new Image(167,312); mapSearchDomicile[14].src=imgpath+'/map/map14.gif';
    mapSearchDomicile[15]=new Image(167,312); mapSearchDomicile[15].src=imgpath+'/map/map15.gif';
    mapSearchDomicile[16]=new Image(167,312); mapSearchDomicile[16].src=imgpath+'/map/map16.gif';
    mapSearchDomicile[17]=new Image(167,312); mapSearchDomicile[17].src=imgpath+'/map/map17.gif';
    mapSearchDomicile[20]=new Image(167,312); mapSearchDomicile[20].src=imgpath+'/map/map20.gif';
    mapSearchDomicile[21]=new Image(167,312); mapSearchDomicile[21].src=imgpath+'/map/map21.gif';
    mapSearchDomicile[23]=new Image(167,312); mapSearchDomicile[23].src=imgpath+'/map/map23.gif';
    mapSearchDomicile[24]=new Image(167,312); mapSearchDomicile[24].src=imgpath+'/map/map24.gif';
}
function changeMapSearchDomicile(thisChoice)
{
    if (document.getElementById)
    {
        if(thisChoice == '')
            thisChoice=0;

        var dealerMap=document.getElementById('dealerMap');
        dealerMap.src=mapSearchDomicile[Number(thisChoice)].src;
    }
}
//function ajaxcall(formObj,containerName)
//{
//    formObj.ajxifrm.value = 'A';
//    var pars=jQuery(formObj).serialize();
//    jQuery.fn.colorbox.close();
//    jQuery.post('http://'+location.hostname+'/view.php',pars,function(data){
//        jQuery('#'+containerName).html(data);
//    });
//    return false;
//}
//function ajaxcallSDS(formObj,containerName)
//{
//    formObj.ajxifrm.value = 'A';
//    var pars=jQuery(formObj).serialize();
//    jQuery.fn.colorbox.close();
//    jQuery.post('http://'+location.hostname+'/viewCottage.php',pars,function(data){
//        jQuery('#'+containerName).html(data);
//    });
//    return false;
//}
function create_captchaImg()
{
    jQuery.get(_su+'/createCaptchaImg.php','',function(data){
        jQuery('#captcha_image').html(data);
    });
}
function searchReservation(frmObj,url,container)
{
    var pars=jQuery(frmObj).serialize();

    jQuery('#loadingImg_search_res').css('style','inline');

    jQuery.post(url,pars,function(str){
        if(str.substring(0,5) == "error")
        {
            jQuery('#msgSearchError').html('<div class="message">'+ str.substring(5)+'</div>');
            jQuery('#search_result').html('');
        }
        else
        {
            jQuery('#msgSearchError').html('');
            jQuery('#'+container).html(str);
        }
        jQuery('#loadingImg_search_res').hide();
    });
    if(container == 'res_manage')
        hideShowInfo('id_adReservation','id_adReservationClass','Reservation');
    return false;
}
