/* $Id: nh_common.js,v 1.68 2012/01/17 06:34:32 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 changeSubType(formVar,cat,listTypeBox,selectedOpt)
{
    var newOptGroup;
    var opt;
    var listTypeBox;
    var selCat = eval("document.typeFrm."+"subType_"+cat+".value");
    var subTypeArr= selCat.split(",");

    if(navigator.appName == 'Opera')
    {
        if(listTypeBox.options.length > 1)
        {
            listTypeBox.options.length = 1;
            listTypeBox.options[0].selected = true;
        }

        for(i=1;i<ht.length;i++)
        {
            j=0;
            while(r[i][j])
            {
                if(isExists(r[i][j],subTypeArr))
                {
                    while(r[i][j])
                    {
                        if(isExists(r[i][j],subTypeArr))
                        {
                            if(selectedOpt != '' && selectedOpt == r[i][j])
                                opt = new Option(s[i][j],r[i][j],true);
                            else
                                opt = new Option(s[i][j],r[i][j]);

                            listTypeBox.options.add(opt);
                        }
                        j++;
                    }
                    continue;
                }
                j++;
            }
        }

    }
    else
    {
        if(listTypeBox.options.length > 1)
        {
            var textCap = listTypeBox.firstChild.text;
            var eachgroup = listTypeBox.firstChild;

            for(i=1;i<ht.length;i++)
            {
                var myele = document.getElementById("grp_"+i);

                if(myele)
                {
                    if(myele.childNodes.length > 0)
                    listTypeBox.removeChild(myele);
                }
            }
        }
        for(i=1;i<ht.length;i++)
        {
            j=0;
            while(r[i][j])
            {
                if(isExists(r[i][j],subTypeArr))
                {
                    newOptGroup = document.createElement('optgroup');
                    newOptGroup.setAttribute('label',ht[i][i-1]);
                    newOptGroup.setAttribute('id',"grp_"+i);
                    listTypeBox.appendChild(newOptGroup);

                    while(r[i][j])
                    {
                        if(isExists(r[i][j],subTypeArr))
                        {
                            opt = document.createElement('option');
                            opt.value=r[i][j];

                            if (navigator.userAgent.match(/IE/))
                                opt.innerText=s[i][j];
                            else
                                opt.text=s[i][j];

                            if(selectedOpt != '' && selectedOpt == r[i][j])
                                opt.selected = true;
                            newOptGroup.appendChild(opt);
                        }
                        j++;
                    }
                    continue;
                }
                j++;
            }
        }
    }
}

function getSortCode(selType)
{
    for(i=1;i<ht.length;i++)
    {
        j=0;
        while(r[i][j])
        {
            if(r[i][j] == selType)
                return sc[i][j];
            j++;
        }
    }
    return 0;
}

function isExists(id,subTypeArr)
{
    var i=0;

    while(subTypeArr[i])
    {
        if(id == subTypeArr[i])
            return true;
        i++;
    }
    return false;
}

function check_search()
{
    if(isblank(document.search.veh_id_reg_no.value))
    {
        alert(msgBlankSearchHouse);
        document.search.veh_id_reg_no.focus();
        return false;
    }
    else
    {
        //If house 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.replace(_su + '/viewHouse.php?id_house=' + document.search.veh_id_reg_no.value);
            }
            else
            {
                alert(msgNumeric);
                document.search.veh_id_reg_no.focus();
                return false;
            }
        }
    }
    return false;
}
function car_quiz()
{
    window.open(_su + 'carQuiz.php','ap','scrollbars=auto,resizable=yes,width=555,height=700');
    AjaxUpdater('car_quiz',_su + '/updateQuizCount.php');
}
function navigationHitCounter(id)
{
    AjaxUpdater('',_su + '/navigationHitCounter.php?id='+id);
}
//This method is Used in Simple Search and House Post forms.
function houseTypeChange(v1,v2,curval,cond)
{
	var houseid='';

	if(isNaN(curval))
		var id_make='';
	else
		var id_make=curval;

	if(v1)
	{
		if(v1.length)
		{
			if(v1.selectedIndex > -1)
				var houseid=new String(v1.options[v1.selectedIndex].value);
			else
				var houseid='';
	  	}
		else
			var houseid=v1;
	}
	if(houseid != '') 	//House Type is selected
	{
		if(cond == 'Y' || (v2.length > 0 && v2.options[0].value == ''))
		{
			v2.length=1;
			var j=1;
		}
		else 			//Posting House
		{
			v2.length=0;
			var j=0;
		}
		if(v2.length != 0)
        	v2.selectedIndex=0;

		for(i=0; i < e[houseid].length; i++)
	    {
			var make_value=new String(e[houseid][i]);
			var make_text=new String(f[houseid][i]);

			if(cond == 'POST' || a[make_value] > 0) 	//Posting house && Simple Search
			{
				makeOpt=new Option(make_text,make_value);
				eval("v2.options[j]=makeOpt");

				if(id_make == make_value)
					v2.selectedIndex=j;

				j++;
			}
	    }
		if(v2.selectedIndex == '' || v2.selectedIndex < 1)
			v2.selectedIndex=0;
	}
	else
	{
		//When House Type is not selected (Simple Search)
		v2.length=1;
		var j=1;

		for(z=0; z < u.length; z++)
		{
			if(a[u[z]] > 0)
			{
				makeOpt=new Option(b[u[z]],u[z]);
				eval("v2.options[j]=makeOpt");

				if(id_make == u[z])
					v2.selectedIndex=j;

				j++;
			}
		}
	}
}

//Function to change the subtype corresponding to house type
function subTypeChange(v1,v2,curval,cond)
{
	//If the selected value is not passed in curval, then check whether any subtype is selected and
	//if yes, store it as curval.
	if(curval == '' && v2.selectedIndex > 0)
		curval=v2.options[v2.selectedIndex].value;

	if(cond == 's' || cond == 'p')	//'s' -> simple search, 'p'-> house post/edit
		curval="'"+curval+"'";

	//holds the seleted house type.
	var typeId='';

	if(v1.selectedIndex >= 0)
		typeId=v1.options[v1.selectedIndex].value;

	var opt;
	v2.length=0;
	var j=0;

	if(cond == 's')	//For simplate search display the 'House subtype' as default option.
	{
		opt=new Option(subtype,'');
		eval("v2.options[j]=opt");
		j++;
	}
	else if(cond == 'p')	//For house post/edit, display the blank value as default option.
	{
		opt=new Option('','');
		eval("v2.options[j]=opt");
		j++;
	}

	if(typeId != '') //If the house type is selected(house post/edit, simple search and advanced search)
	{
		for(var i=0; i < r[typeId].length; i++)
		{
			opt=new Option(s[typeId][i],r[typeId][i]);
			eval("v2.options[j]=opt");

			if(curval.search("'"+r[typeId][i]+"'") != -1)
				v2.options[j].selected=true;

			j++;
		}
	}
	else	//If the house type is not selectd(simple search and advacned search)
	{
		var idHouseType;
		var strIdSubType=new String('');

		//Loop through all of the house types.
		for(var m=0; m < v1.length; m++)
		{
			var idHouseType=v1.options[m].value;

			if(idHouseType > 0)
			{
				for(var i=0;i < r[idHouseType].length; i++)
				{
					//Check whether the subtype has been already added in the listbox as one subtype
					// can belong to more than one house type.
					if(strIdSubType.search("'"+r[idHouseType][i]+"'") == -1)
					{
						opt=new Option(s[idHouseType][i],r[idHouseType][i]);
						eval("v2.options[j]=opt");

						if(curval.search("'"+r[idHouseType][i]+"'") != -1)
							v2.options[j].selected=true;

						strIdSubType=strIdSubType+"'"+r[idHouseType][i]+"',";
						j++;
					}
				}
			}
		}
	}
}

//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 Length,Power and Price list box options
function listboxChange(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 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 suhmit favorite page by adding/removing houses
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]+' '+msgAddFavBig);
                jQuery('#Add_To_Fav_End').html(arr[1]+' '+msgAddFavBig);
            }
            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' && document.fav.elements[i].alt != 'compHouse')
					{
						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,house etc. and 'nm' can be the user name,
//dealer name or house id etc.
function newConfirmDel(ref,label,nm)
{
	if(confirm(msgNewDeleteConfirm+" "+label+", '" + nm + "'?"))
		window.location.replace(ref);
}

//Part of rotating banner module, to show alter text on windows's status bar
function descript()
{
	if(alt[zg])
	{
		window.status=alt[zg];

		if ((navigator.appName == 'Netscape') && (parseFloat(navigator.appVersion.substr(0,3)) < 5.0))
		{
			document.netBan.visibility='show';
			text='';
			text=alt[zg];

			document.netBan.document.write('<layer id="id1" top="20" left="450" bgcolor="#f7f7d3" style="border:0px solid white; font-family: verdana; font-size:11px; color:black;">'+text+'</layer>');
		}
		else
			document.banner.title=alt[zg];
	}
}

//Part of rotating banner module, to remove alter text from windows's status bar
function remove()
{
	window.status='';

	if ((navigator.appName == 'Netscape') && (parseFloat(navigator.appVersion.substr(0, 3)) < 5.0))
		document.netBan.visibility='hide';
}

//Function to select Acc from 1 listbox and to put into
//another listbox used in Advanced search page
function addAcc(v1,v2)
{
	if(v1.id_acc.selectedIndex >=0 && v1.id_acc[v1.id_acc.selectedIndex].value != '')
	{
		if(v2.length >= 1)
		{
			if(v2.options[0].value == '')
				document.adv.id_sel_acc.length--;

			if(v2.selectedIndex == 1)
				v2.selectedIndex=0;
		}
		else
			v2.length=0;
		var tot=v2.length;
		var dup=0;

		for(o=0; o<tot; o++)
		{
			if(v2.options[o].value == v1.id_acc[v1.id_acc.selectedIndex].value)
			{
				dup=1;
				alert(msgDupSelected);
			}
		}
		if(dup != 1)
		{
			selOpt=new Option(v1.id_acc[v1.id_acc.selectedIndex].text,v1.id_acc[v1.id_acc.selectedIndex].value);
			eval("v2.options[tot]=selOpt");
		}
	}
	v1.id_acc.selectedIndex=-1;
}

//Function to remove selected Acc list used in Advanced search page
function removeAcc(v1)
{
	var p=v1.id_sel_acc.selectedIndex;
	var j=0;
	var tot=v1.id_sel_acc.options.length;

	if(tot > p)
	{
		for(i=p; i<=tot; i++)
		{
			j=i+1;

			if(j > (tot-1))
				j=tot-1;

			var disp=v1.id_sel_acc[j].text;
			var val=v1.id_sel_acc[j].value;

			selOpt=new Option(disp,val);
			eval("v1.id_sel_acc.options[i]=selOpt");
		}
	}
	v1.id_sel_acc.length=tot-1;

	//If no option is there then add "any" as default
	if(v1.id_sel_acc.options.length == 0)
	{
		selOpt=new Option("----------- "+modelDefVal+" -----------",'');
		eval("v1.id_sel_acc.options[0]=selOpt");
	}
}

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

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

    return false;
}

//function to validation of the string value of text box. 2005:10:14 by amit.
function stringValidation(v1){
  	var val =v1.value;
  	if(!isNaN(val)){
  		alert(msgString);
  		v1.value="";
		v1.focus();
    }
}

//function to validation of the string value of text box. 2005:10:14 by amit.
function numericValidation(v1){
  	var val =v1.value;
  	if(isNaN(val)){
  		alert(msgNumeric);
  		v1.value="";
		v1.focus();
    }
}

function RemoveGroup(groupLabel,selectId)
{
    try
    {
        var selectList=document.getElementById(selectId);
        var eachGroup=selectList.firstChild;

        while (groupLabel != eachGroup.label)
            eachGroup=eachGroup.nextSibling;

        selectList.removeChild(eachGroup);
    }
    catch(er){}
}

function typeChange(typeID,cat,defValAreaFrom,defValPriceFrom,defValAreaTo,defValPriceTo){

	cap = defValAreaFrom;
	val = "";
	opt = new Option(cap,val);
	document.srch.opt_area_from.options[0] = opt;

	cap = defValPriceFrom;
	val = "";
	opt = new Option(cap,val);
	document.srch.opt_price_from.options[0] = opt;

	cap = defValAreaTo;
	val = "";
	opt = new Option(cap,val);
	document.srch.opt_area_to.options[0] = opt;

	cap = defValPriceTo;
	val = "";
	opt = new Option(cap,val);
	document.srch.opt_price_to.options[0] = opt;

 	document.srch.opt_area_to.options.length = 1;
 	document.srch.opt_price_to.options.length = 1;
//
	if((typeID != '') && (cat.length == 1)){

		arrTC = eval(cat+'data'+sCode[typeID]+'_C');
		arrTV = eval(cat+'data'+sCode[typeID]+'_V');

		arrTC2 = eval('P_'+cat+'data'+sCode[typeID]+'_C');
		arrTV2 = eval('P_'+cat+'data'+sCode[typeID]+'_V');

		//For Area
		document.srch.opt_area_from.options.length = arrTV.length-1;
		document.srch.opt_area_to.options.length = arrTV.length-1;
		for(i=0; i < arrTV.length-1; i++)
		{
			cap = new String(arrTC[arrTV[i]]);
			val = new String(arrTV[i]);
			opt = new Option(cap,val)
			opt1 = new Option(cap,val)
			document.srch.opt_area_from.options[i+1] = opt;
			document.srch.opt_area_to.options[i+1] = opt1;
		}
		document.srch.opt_area_from.selectedIndex = 0;
		document.srch.opt_area_to.selectedIndex = 0;

		//For Price
		document.srch.opt_price_from.options.length = arrTV2.length-1;
		document.srch.opt_price_to.options.length = arrTV2.length-1;
		for(i=0; i < arrTV2.length-1; i++)
		{
			cap = new String(arrTC2[arrTV2[i]]);
			val = new String(arrTV2[i]);
			opt = new Option(cap,val)
			opt1 = new Option(cap,val)
			document.srch.opt_price_from.options[i+1] = opt;
			document.srch.opt_price_to.options[i+1] = opt1;
		}
		document.srch.opt_price_from.selectedIndex = 0;
		document.srch.opt_price_to.selectedIndex = 0;
	}else{
		//For Area
		document.srch.opt_area_from.options.length = 1;

		//For Price
		document.srch.opt_price_from.options.length = 1;
	}
}

function areaFromChange(typeID,cat,defVal,fromID,objToId){
	cap = defVal;
	val = "";
	opt = new Option(cap,val);
	objToId.options[0] = opt;
	if((typeID != '') && (cat.length == 1) && (fromID != ''))
	{
		arrTC = eval(cat+'data'+sCode[typeID]+'_C');
		arrTV = eval(cat+'data'+sCode[typeID]+'_V');

		for (i=0;i<arrTV.length;i++){
			if (arrTV[i]==fromID){
				fromID = i;
			}
		}
		objToId.options.length = (arrTV.length - fromID);
		//for(i=fromID; i < arrTC[arrTV[arrTV.length-1]]; i++)
		for(i=fromID; i < arrTV.length; i++)
		{
			cap = new String(arrTC[arrTV[i]]);
			val = new String(arrTV[i]);
			opt = new Option(cap,val)
			objToId.options[i-fromID+1] = opt;
		}
		document.srch.optLastArea.value = arrTV[i-1]
		objToId.selectedIndex = 0;
	}else{
		objToId.options.length = 1;
	}

}

function priceFromChange(typeID,cat,defVal,fromID,objToId){
	cap = defVal;
	val = "";
	opt = new Option(cap,val);
	objToId.options[0] = opt;
	if((typeID != '') && (cat.length > 0) && (fromID != ''))
	{
		arrTC = eval('P_'+cat+'data'+sCode[typeID]+'_C');
		arrTV = eval('P_'+cat+'data'+sCode[typeID]+'_V');

		for (i=0;i<arrTV.length;i++){
			if (arrTV[i]==fromID){
				fromID = i;
			}
		}
		objToId.options.length = (arrTV.length - fromID);

		//for(i=fromID; i < arrTC[arrTV[arrTV.length-1]]; i++)
		for(i=fromID; i < arrTV.length; i++)
		{
			cap = new String(arrTC[arrTV[i]]);
			val = new String(arrTV[i]);
			opt = new Option(cap,val);
			objToId.options[i-fromID+1] = opt;
		}
		document.srch.optLastPrice.value = arrTV[i-1];
		objToId.selectedIndex = 0;
	}else{
		objToId.options.length = 1;
	}
}

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;
	}
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

//	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('nh_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',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('nh_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',pars,function(data){
            jQuery('#submitXQA').html(data);
            setTimeout("whiteBG();",1000);
        });
        return false;
    }
    else
        return false;
}
//		Fn. for Login modal
function loginmodal(opt,rPage)
{
    url=_su+'/modalLogin.php?mainOnly=1';
	//  D => Simple Dealer , N => From View Page , P => Post Ad, RP => any given redirect page rPage
	if(opt == 'D')
	    pageURL = url+'&sd=1&fwdUrl='+location.href.replace('#','_hash_');
	else if(opt == 'N')
	{
	    var srchCTG=location.href.search("&ctg");
	    var srchHash=location.href.search("#");
	    if(srchCTG > 0)
	        pageURL = url+'&sd=0&fwdUrl='+location.href.replace('&ctg','_ctg');
	    else 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=='LA') //login authentication
        pageURL=url+'&redirectPage='+rPage+'&opt='+opt;
    else if(opt == 'POP')
        pageURL=url+'&fwdUrl='+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(data == "success")
            {
                if(frmObj.postad.value == 1)
                    window.location.href = _su+"/postAd.php";
                else 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(frmObj.redirectPage && frmObj.redirectPage.value != '')
                    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 = _su;
            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_house='+idAd+'&video_status='+videoStatus+'&id_reporter='+idReporter+'&ajxifrm=A';
        AjaxUpdater('vStatus'+idAd+reporterSuffix,_su + '/extra/admin/changeVideoStatus.php?'+pars);
    }
    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');
}
/* function to Hide/Show star as mandatory/optional field */
function showHideMendetoryStar(id_build,ele_id)
{
    if(id_build == 3)
        document.getElementById(ele_id).style.display='none';
    else
        document.getElementById(ele_id).style.display='inline';
}
/** 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=NH';
   var value = loadingImg = '';
   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 setFloorPlan(id)
{
    var scriptName = location.href.substring((location.protocol.length + location.hostname.length + 3)).split('?');

    var frmObj = document.forms.attachimg;
    var l = frmObj.elements.length;

    frmObj.opt.value = 'floorplan';

    var params = '';
    for (var i = 0; i < l; i++)
    {
        var e = frmObj.elements[i];
        if (e.name && e.value && e.name != 'undefined' && e.type != 'checkbox' && e.type != 'radio') {
            params += e.name + '=' + e.value + '&';
        }
    }
    params += '&id=' + id;
    if(scriptName[1].match('&nx=1'))
    	params += '&nx=1';

    jQuery.post(_su + '/' + scriptName[0],params,function(data){
        document.getElementById('uploaded_image').innerHTML = data;

    });
    return false;
}

//function ajaxcall(formObj,containerName)
//{
//    formObj.ajxifrm.value = 'A';
//    var pars=jQuery(formObj).serialize();
//    jQuery.fn.colorbox.close();
//    jQuery.post('http://'+location.hostname+'/viewHouse.php',pars,function(data){
//        jQuery('#'+containerName).html(data);
//    });
//    return false;
//}

function getDomTown(domVal,curval,page,srch)
{
    var townStr='';
    var srchStr='';
    var mapId='';
    var simpAjaxCall="callAjax(document.srch);";
    if(srch == 'map')
    {
        mapId='_map';
        srchStr='&srch='+srch;
    }
    var townUrl=_su+'/ajaxGetDomicileTown.php?ajxifrm=A&frm='+page+'&site=NH'+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 == 'SIMP')
            divId='srch_id_town'+mapId;
        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).html(data);

        if(page == 'ADV')
            callAjax(document.adv);
        else if(page == 'SIMP' && (srch != 'map' || srch != 'qs'))
            callAjax(document.srch);
    });
    return false;
}
function cacheMapDealer(imgpath)
{
    mapDealer[0]=new Image(167,312); mapDealer[0].src=imgpath+"/map/map.gif";
    mapDealer[1]=new Image(167,312); mapDealer[1].src=imgpath+"/map/map1.gif";
    mapDealer[2]=new Image(167,312); mapDealer[2].src=imgpath+"/map/map2.gif";
    mapDealer[3]=new Image(167,312); mapDealer[3].src=imgpath+"/map/map3.gif";
    mapDealer[4]=new Image(167,312); mapDealer[4].src=imgpath+"/map/map4.gif";
    mapDealer[5]=new Image(167,312); mapDealer[5].src=imgpath+"/map/map5.gif";
    mapDealer[6]=new Image(167,312); mapDealer[6].src=imgpath+"/map/map6.gif";
    mapDealer[7]=new Image(167,312); mapDealer[7].src=imgpath+"/map/map7.gif";
    mapDealer[8]=new Image(167,312); mapDealer[8].src=imgpath+"/map/map8.gif";
    mapDealer[9]=new Image(167,312); mapDealer[9].src=imgpath+"/map/map9.gif";
    mapDealer[10]=new Image(167,312); mapDealer[10].src=imgpath+"/map/map10.gif";
    mapDealer[11]=new Image(167,312); mapDealer[11].src=imgpath+"/map/map11.gif";
    mapDealer[12]=new Image(167,312); mapDealer[12].src=imgpath+"/map/map12.gif";
    mapDealer[13]=new Image(167,312); mapDealer[13].src=imgpath+"/map/map13.gif";
    mapDealer[14]=new Image(167,312); mapDealer[14].src=imgpath+"/map/map14.gif";
    mapDealer[15]=new Image(167,312); mapDealer[15].src=imgpath+"/map/map15.gif";
    mapDealer[16]=new Image(167,312); mapDealer[16].src=imgpath+"/map/map16.gif";
    mapDealer[17]=new Image(167,312); mapDealer[17].src=imgpath+"/map/map17.gif";
    mapDealer[20]=new Image(167,312); mapDealer[20].src=imgpath+"/map/map20.gif";
    mapDealer[21]=new Image(167,312); mapDealer[21].src=imgpath+"/map/map21.gif";
    mapDealer[23]=new Image(167,312); mapDealer[23].src=imgpath+"/map/map23.gif";
    mapDealer[24]=new Image(167,312); mapDealer[24].src=imgpath+"/map/map24.gif";
}
function changeMapDealer(thisChoice)
{
    if (document.getElementById)
    {
        if(thisChoice == "")
            thisChoice=0;

        var dealerMap=document.getElementById("dealerMap");
        dealerMap.src=mapDealer[Number(thisChoice)].src;
    }
}
function check_dealer()
{
    if(isblank(document.searchDealer.search_dealer.value))
    {
        alert(msgBlankDealer);
        document.searchDealer.search_dealer.focus();
        return false;
    }
}
function openHideDomicile(domeName,domeId)
{
    var checkAjaxId='checkAjax_'+domeId;
    var imgLinkId=domeName+'_'+domeId;
    var AjaxLoaderId='loadingImg_'+domeId;
    /** Check weather list display using ajax request OR normal show/hide id. */
    if(jQuery('#'+checkAjaxId).attr("class") == checkAjaxId)
    {
        if(document.getElementById(domeName).style.display == 'none')
        {
            document.getElementById(domeName).style.display='block';           ;
            jQuery('#'+imgLinkId).attr({title:C_HIDE,'class':'arrow_hide'});
        }
        else
        {
            document.getElementById(domeName).style.display='none';
            jQuery('#'+imgLinkId).attr({title:C_OPEN,'class':'arrow_open'});
        }
    }
    else
    {
        jQuery('#'+AjaxLoaderId).css('display','inline');
        var pars='ajxifrm=A&id_domicile='+domeId+'&domeName='+domeName;
        jQuery.get('http://'+location.hostname+'/ajaxListDealer.php',pars,function(data){

            jQuery('#'+domeName).html(data);
            jQuery('#'+domeName).show();

            jQuery('#'+checkAjaxId).attr('class',checkAjaxId);
            jQuery('#'+imgLinkId).attr({title:C_HIDE,'class':'arrow_hide'});

            jQuery('#'+AjaxLoaderId).hide();
        });
        return false;
    }
     return false;
}
/**
Function to open/hide whole domicile list dealer.
var openAll='Y' set global variable for check hide/openall domicile.
*/
function openHideAllDomicile()
{
    if(idDomJs)
    {
        /** Check weather list display using ajax request OR normal show/hide id. */
        if(jQuery('#checkAjax_open_hide').attr("class") == 'ajax_done')
        {
            for(var i=0; i<idDomJs.length; i++)
            {
                arrDomInfo=(idDomJs[i]).split('@');
                var domeId=arrDomInfo[0];
                var domeName=arrDomInfo[1];
                var checkAjaxId='checkAjax_'+domeId;
                var imgLinkId=domeName+'_'+domeId;
                var AjaxLoaderId='loadingImg_'+domeId;

                if(openAll == 'Y')
                {
                    jQuery('#'+domeName).show();
                    jQuery('#'+imgLinkId).attr({title:C_HIDE,'class':'arrow_hide'});
                }
                else
                {
                    jQuery('#'+domeName).hide();
                    jQuery('#'+imgLinkId).attr({title:C_OPEN,'class':'arrow_open'});
                }
            }
            if(openAll == 'Y')
            {
                openAll='N';
                jQuery('#open_hide').attr({href:'http://'+location.hostname+'/listDealer.php',title:C_HIDE_ALL,'class':'arrow_hide'});
                jQuery('#open_hide').html(C_HIDE_ALL);
            }
            else
            {
                openAll='Y';
                jQuery('#open_hide').html(C_OPEN_ALL);
                jQuery('#open_hide').attr({href:'http://'+location.hostname+'/listDealer.php?opt=open_all',title:C_OPEN_ALL,'class':'arrow_open'});
            }
        }
        else
        {
            jQuery('#loadingImg_open_hide').show();
            var strIdDomJs='';

            for(var i=0; i<idDomJs.length; i++)
            {
                arrDomInfo=(idDomJs[i]).split('@');
                strIdDomJs +=arrDomInfo[0]+',';
            }
            strIdDomJs=strIdDomJs.slice(0,-1);

            var pars='ajxifrm=A&open_hide=all&strIdDom='+strIdDomJs;
            jQuery.get('http://'+location.hostname+'/ajaxListDealer.php',pars,function(data){

                jQuery('#open_hide_all').html(data);
                jQuery('#checkAjax_open_hide').attr('class','ajax_done');

                openAll='N';
                jQuery('#open_hide').attr({href:'http://'+location.hostname+'/listDealer.php',title:C_HIDE_ALL,'class':'arrow_hide'});
                jQuery('#open_hide').html(C_HIDE_ALL);

                jQuery('#loadingImg_open_hide').hide();
            });
            return false;
        }
         return false;
    }
}
function create_captchaImg()
{
    jQuery.get(_su+'/createCaptchaImg.php','',function(data){
        jQuery('#captcha_image').html(data);
    });
}
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);

}
