/* $Id: nx_common.js,v 1.15 2008/06/26 11:04:48 arwa Exp $ */

/* Function which checks whether given email is valid or not */
function emailInvalid(s)
{
	if(s.match(/^\w+([\.\+]?[-]{0,2}\w+)*[\.]?[-]?@\w+([\.-]?\w+)*(\.\w{2,4})+$/i))
		return true;
	else
		return false;
}

/* Function which checks whether given url address is valid or not */
function urlInvalid(s)
{
	if(!(s.match(/^http:\/\/[a-zA-Z0-9]+([_|\.-][a-zA-Z0-9~-]{1,})*\.([a-z]{2,4})(\/[a-zA-Z0-9~-]{1,}[_|\.]{0,1}[a-zA-Z0-9~]{1,}\/?)*([_|\.][a-zA-Z0-9~-]{1,})*$/i) || s.match(/^ftp:\/\/[a-zA-Z0-9]+([_|\.][a-zA-Z0-9~-]{1,})*\.([a-z]{2,4})(\/[a-zA-Z0-9~-]{1,}[_|\.]{0,1}[a-zA-Z0-9~]{1,}\/?)*([_|\.][a-zA-Z0-9~]{1,})*$/i)))
		return false;
	else
		return true;
}

/* Function to open new pop up window with fixed size */
function openWin(win_nm,nm)
{
	var h=screen.height;
	h=h-80;

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

	if(nm == '')
		nm='new';

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

/* 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=yes,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 to submit navigation */
function submitNav(pno,sort,ono)
{
	document.nav.page.value=pno;
	document.nav.sortCol.value=sort;
	document.nav.ord.value=ono;
	document.nav.submit();
}

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

/* Sort displaying vehicle list page according to specified arguments. */
function sortForm(col,ord)
{
	document.sortl.sortCol.value=col;
	document.sortl.ord.value=ord;
	document.sortl.submit();
}

/* To popup message while removing single records */
function confirmDel(ref)
{
	if(confirm(msgDeleteConfirm))
		window.location.replace(ref);
}

/* To popup message while removing multiple records */
function confirmMultiDel(act,frm)
{
	if(confirm(msgDeleteConfirm))
	{
		if (act != '')
			eval('document.'+frm+'.action='+act);

		eval('document.'+frm+'.submit()');
	}
}

/* Function to give alert to those users who Do not use standarad browsers */
function diffBrowser()
{
	alert(msgDifferentBrowser);
}

/* Function to swap image (used in Map etc.) */
function swapImage(imgname,path,carid,imageid)
{
	carid.value=imageid;
	imgname.src=path;
}

/* Function to check phone,fax,mobile for finland country */
function check(s)
{
	if(!s.match(/^[0-9\s\+-]{5,25}$/) )
		return false;
	else
		return true;
}

/** Function to check loginid string, various rules are followed. */
function checkLogin(s)
{
    if(!(s.match(/^[a-zA-Z0-9åÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´_]{4,12}$/)))
        return msgUserid;
    else
    {
        s=s.toLowerCase();

        if(s == 'admin' || s == 'webmaster' || s == 'help' || s == 'helpdesk' || s == 'user' || s == 'dealer' || s == 'dummy' ||
            s == 'nettiauto' || s == 'nettimoto' || s == 'nettivene' || s == 'nettivaraosa' ||s == 'nettimokki' ||
            s == 'nettikone' ||s == 'nettiasunto' || s == 'nettimarkkina' || s == 'nettikaravaani')
            return msgResdId;
    }
    return 1;
}

/* Function to check Password string, various rules are followed */
function checkPasswd(s)
{
	if(s.match(/^[a-zA-Z0-9åÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´_.-]{4,12}$/))
		return true;

	return false;
}

/*Function to check characters length of word */
function isLong(value,sep,len)
{
	if (value == '')
		return true;
	else
	{
		tempValue=value.split(sep);

		for(var i=0; i < tempValue.length; i++)
		{
			if(tempValue[i].length > len)
			{
				return 0;
				break;
			}
		}
	}
}

/*Function to check SSN number validation in Finland country */
function checkSsn(p)
{
    lChar=new Array('A','B','C','D','E','F','H','J','K','L','M','N','P','R','S','T','U','V','W','X','Y','Z');

	if(!(p.match(/^[0-9]{6}-[0-9]{3}[0-9a-zA-Z]{1}$/)))
		return false;
	else
    {
		ssn=p.replace('-','');
		first=ssn.substr(0,9);
		start1=ssn.substr(0,6);
		middle=ssn.substr(6,3);
		last=ssn.substr(9,1);

		remin=(first % 31);

		if(remin >= 0 && remin <= 9)
			rightChar=remin;
		else
			rightChar=lChar[remin-10];

		rightSSN=start1+'-'+middle+rightChar;

		if(rightSSN == p)
			return true;
		else
			return false;
	}
	return false;
}

/* Function to get stored cookie */
function Get_Cookie(name)
{
    var start=document.cookie.indexOf(name+'=');
    var len=start+name.length+1;

    if ((!start) && (name != document.cookie.substring(0,name.length)))
    	return null;

    if (start == -1)
    	return null;

    var end=document.cookie.indexOf(';',len);

    if (end == -1)
    	end=document.cookie.length;

    return unescape(document.cookie.substring(len,end));
}

/* Function to set new cookie */
function Set_Cookie(cookname,cookvalue,expires,path,cookdomain,secure)
{
    document.cookie=cookname+'='+escape(cookvalue)+((expires)?';expires='+expires:'')+((path)?';path='+path:'')+((cookdomain)?';domain='+cookdomain:'')+((secure)?';secure':'');
}

/* Function to delete cookie */
function Delete_Cookie(cookname,path,cookdomain)
{
    if(Get_Cookie(cookname))
    	document.cookie=cookname+'='+((path)?';path='+path:'')+((cookdomain)?';domain='+cookdomain:'')+';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/* Function to check First-Last name values when it is entered as full name */
function checkFullName(s)
{
	if(!s.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´_\s-]{1,50}$/))
		return false;
	else
		return true;
}

/* Function to check First-Last name values */
function checkName(s)
{
	if(!s.match(/^[a-zA-ZåÅäÄöÖé´ÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ-]{1,30}$/))
		return false;
	else
		return true;
}

/* Function to check hobby/reference values */
function checkHobbyRef(s)
{
	if(!s.match(/^[0-9a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ_:;@´()\s\+\,'.-]{1,250}$/))
		return false;
	else
		return true;
}

/* Function to check address/contact time values */
function checkAddContTime(s)
{
	if(!s.match(/^[0-9a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ_´:()\s\,'.!-]{1,250}$/))
		return false;
	else
		return true;
}

/* Function to check city values */
function checkCity(s)
{
	if(!s.match(/^[0-9a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ_´:\s\,-]{1,250}$/))
		return false;
	else
		return true;
}

/* Function to check zip values */
function checkZip(s)
{
	if(!s.match(/^[0-9a-zA-Z]{1,250}$/))
		return false;
	else
		return true;
}

/* This method is Used in Simple Search forms.*/
var textureName='';
var optChar='';
var optGroup='';
var textureGroup='';
var make_opt='';

function makeChange(mk,selVal)
{
	var bflag=1;
	textureName='';
	mk.length=1;
	var c=1;

	// If browser is IE but version is not 6.0 then flag will be false
	if(navigator.appVersion.indexOf('MSIE') != -1 && navigator.appVersion.indexOf('MSIE 6.0') == -1)
		bflag=0;
	if(navigator.appName == 'Netscape' && !(parseFloat(navigator.appVersion) >= 4.8))
		bflag=0;

    for(var i=0; i<mCount.length; i++)
	{
		var make_value=new String(mCount[i]);
		var make_text=new String(m[mCount[i]]);

		// Changes has been made for displaying makes in option group wise.
		// i.e. Starting with char A will listed below A Group, same as for B to Z.
		if(make_text.match(/^[A-Za-z]{1}/) && (!(navigator.userAgent.match(/Opera/))) && bflag == 1)
		{
			optChar='';
			optChar=make_text.substr(0,1);
			optGroup='T';

			if(textureName != optChar)
			{
				textureName=optChar;
				textureGroup=document.createElement('optgroup');
				textureGroup.label=textureName;
				mk.appendChild(textureGroup);
			}
		}
		else
			optGroup='F';

		if(optGroup != 'F' && textureName != '')
		{
			make_opt=document.createElement('option');
			make_opt.value=make_value;

			if(navigator.userAgent.match(/IE/))
				make_opt.innerText=make_text;
			else
				make_opt.text=make_text;

			textureGroup.appendChild(make_opt);
		}
		else
		{
			makeOpt=new Option(make_text,make_value);
			eval('mk.options[c]=makeOpt');
		}

		// Option group part ends here
		if(selVal == make_value)
			mk.selectedIndex=c;

		c++;
	}
}

/* Used in Simple Search forms. */
function modelChange(mk,mdl,selVal)
{
	if(mk.selectedIndex > -1)
		var id_make=new String(mk.options[mk.selectedIndex].value)
	else
		var id_make='';

	if(id_make != '')
	{
		if(mdl.length > 0 && mdl.options[0].value == '')
			mdl.length=1;
		if(mdl.length != 0)
        	mdl.selectedIndex=0;

		var c=1;

        arrModel=(mdId[id_make]).split(',');

        for(j=0; j < arrModel.length; j++)
        {
            var modelInfo=arrModel[j].split('#');

			var model_value=new String(modelInfo[0]);
			var model_text=new String(modelInfo[1]);

			modelOpt=new Option(model_text,model_value);
			eval('mdl.options[c]=modelOpt');

			if(selVal == modelInfo[0])
				mdl.selectedIndex=c;

			c++;
	    }
	}
	else
		mdl.length=1;
}

/* Change subType value */
function subTypeChange(subtype,selVal)
{
	c=1;

    for(var k=0; k<sCount.length; k++)
	{
		var svalue=new String(sCount[k]);
		var stext=new String(s[sCount[k]]);

		if(s[sCount[k]])
		{
			sOpt=new Option(stext,svalue);
			eval('subtype.options[c]=sOpt');

			if(selVal == sCount[k])
				subtype.selectedIndex=c;

			c++;
		}
	}
}

/* Function to change year list box options */
function yearChange(v1,v2,selVal)
{
	var tot=v1.selectedIndex+1;

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

	if(selVal == '' && v2.selectedIndex > 0)
		selVal=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(selVal == v1.options[i].value)
			v2.selectedIndex=j;
	}
}

/* Function to change price list box options sign argument is to display sign after the text */
function priceChange(v1,v2,selVal,sign)
{
	if(v1.selectedIndex < 1)
		var st=1;
	else
		var st=v1.selectedIndex;

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

	for(i=st,j=1; i < tot; j++,i++)
	{
		if(parseInt(v1.options[i].value) >= 1000)
		{
			var len=v1.options[i].value.length;
			var disp=v1.options[i].value;
			var disp1=disp.substr(0,len - 3);
			var disp2=disp.substr(len - 3,len);

			disp=disp1+' '+disp2+' '+sign;
		}
		else
			var disp=v1.options[i].value+' '+sign;

		temp=new Option(disp,v1.options[i].value);
		eval('v2.options[j]=temp');

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

function townChange(domicile,town,curval)
{
	if(domicile)
	{
		if(domicile.length)
		{
			{
				if(domicile.selectedIndex > -1)
				{
					idDomicile=new String(domicile.options[domicile.selectedIndex].value);
				}
				else
					idDomicile='';
			}
		}
		else
			idDomicile=domicile;
	}

	town.length=1;
	j=1;

	if(idDomicile != '')
	{
		if(tId[idDomicile])
		{
			for(i=0; i < tId[idDomicile].length; i++)
		    {
				cvalue=new String(tId[idDomicile][i]);
				ctext=new String(tName[idDomicile][i]);

				cOpt=new Option(ctext,cvalue);
				eval("town.options[j]=cOpt");

				if(tId[idDomicile][i] == curval)
					town.selectedIndex=j;
				j++;
			}
		}
	}
	else
	{
		j=1;
		town.length=1;
	}
}

function categoryChange(category,subcategory,curval)
{
	if(category)
	{
		if(category.length)
		{
			{
				if(category.selectedIndex > -1)
				{
					idCat=new String(category.options[category.selectedIndex].value);
				}
				else
					idCat='';
			}
		}
		else
			idCat=category;
	}

	subcategory.length=1;
	j=1;

	if(idCat != '')
	{
		if(sId[idCat])
		{
			for(i=0; i < sId[idCat].length; i++)
		    {
				cvalue=new String(sId[idCat][i]);
				ctext=new String(sName[idCat][i]);

				cOpt=new Option(ctext,cvalue);
				eval("subcategory.options[j]=cOpt");

				if(sId[idCat][i] == curval)
					subcategory.selectedIndex=j;
				j++;
			}
		}
	}
	else
	{
		j=1;
		subcategory.length=1;
	}
}

// function to change town list according the selected domicile.
function partTownChange(domicile,town,curval,page)
{
	if(page == 'simp')
	{
		town=document.getElementById(town);
		townlist=curval.split(',');
	}
	if(domicile)
	{
		if(domicile.length)
		{
			if(domicile.selectedIndex > -1)
			{
				idDomicile=new String(domicile.options[domicile.selectedIndex].value);
			}
			else
				idDomicile='';
		}
		else
			idDomicile=domicile;
	}

	if (page == 'listService')
	{
		town.length=0;
		j=0;
	}
	else
	{
		town.length=1;
		j=1;
	}
	if(idDomicile != '')
	{
		if(tId[idDomicile])
		{
			for(i=0; i < tId[idDomicile].length; i++)
		    {
				cvalue=new String(tId[idDomicile][i]);
				ctext=new String(tName[idDomicile][i]);

				cOpt=new Option(ctext,cvalue);
				eval("town.options[j]=cOpt");

				if(page == 'simp')
				{
					for(k=0; k < townlist.length; k++)
					{
						if(cvalue == townlist[k])
							town.options[j].selected=true;
					}
				}
				else
				{
					if(tId[idDomicile][i] == curval)
					town.selectedIndex=j;
				}
				j++;
			}
		}
	}
	else
	{
		if (page == 'listService')
		{
			town.length=0;
			j=0;
		}
		else
		{
			town.length=1;
			j=1;
		}
	}
}
/** Function to change the video status. */
function changeVideoStatus(idAd,videoStatus,idReporter,siteCode)
{
    if(confirm(msgChangeStatus) && siteCode != '')
    {
        new Ajax.Updater('vStatus'+idAd+'_'+idReporter,'/extra/admin/changeVideoStatus.php',
                            {
                                method: 'get',
                                parameters:
                                {
                                    id_ad: idAd,
                                    video_status: videoStatus,
                                    id_reporter: idReporter,
                                    site_code: siteCode,
                                    ajxifrm: 'A'
                                }
                            }
                        );
    }
    else
    {
        return;
    }
}