rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd()
{
	rnd.seed = (rnd.seed*9301+49297)%233820;
	return rnd.seed/(233280.0);
}

function rand(number)
{
	return Math.ceil(rnd()*number);
}

function pop(url,w,h) {
	url = url+".php";
	//loader = "load.html";
	w = w ? w : 350;
	h = h ? h : 400;
	popWin = window.open(url,rand(10000000),'scrollbars=yes,width=' + w + ',height=' + h);	
}

function fSelect(w) {
	//country changed
	if (w==0) {
		//country is iran
		if (p.options[p.selectedIndex].value==1) {
			//set regions
			r.options.length = 1;
		    for (var i=0; i<iranArray.length; i++) {
				var op = new Option(iranArray[i][0],i+1);
	    		r.options[r.length] = op;
		    }
		}
		else {
			r.options.length = 1;
			c.options.length = 1;
		}
		r.options[0].selected = true;
		c.options[0].selected = true;
	}
	
	//region changed
	else {
		var cR = r.options[r.selectedIndex].value;
		if (cR>0) {
			cR = cR-1;
			iL = iranArray[cR].length-1;
			//set cities
			c.options.length = 1;
		    for (var i=0; i<iL; i++) {
				var op = new Option(iranArray[cR][i+1],i+1);
	    		c.options[c.length] = op;
		    }
		}
		else {
			c.options.length = 1;
		}
		c.options[0].selected = true;
	}
}


function isBlank(s) {
	var whitespace = " \t\n\r";
    if ((s == null) || (s.length == 0)) return true;
    for (var i=0; i<s.length; i++) {   
        var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false; // not blank
    }
    return true;
}

function isIllegalText(s) {
 	var illegal = /[^\w:.\;,\!\?\"\'\-\@\s\r\n\%\$\/\&\*\#\!\(\)]/;
    if (s.search(illegal)!=-1) return true;
}



/* this function is defective. Principally: after the @, _ is not allowed. Before the @,
   many more characters are allowed than the regexp permits.
   -mnc */
   
function isNotEmail(s) {
	if (s.search(/[^a-zA-Z0-9_\-@.]/) != -1)return true;
	if (s.length<6) return true;
	var a = s.indexOf('@');
	var p = s.lastIndexOf('.');
	if ((a==-1)||(p==-1))return true;

	var n = 0;
	var f = s.substring(0,a);
	var se = s.substring(a+1,p);
	var t = s.substring(p+1,s.length);
	if((f.length<1)||(se.length<1)||(t.length < 2)) return true;
}

function eText(f,n,sp) {
	nT = document.forms[f].elements[n].value;
	newf = f;
	if (isNotEmail(nT)) sError(newf);
	else {
		cError(newf);
		if (sp) document.forms[f].submit();
	}
}

/* not unicode-compliant */

function isWText(s) {
 	var illegal = /[^\w\"\'\-\s\%]/;
	for (i=0;i<s.length;i++) {
	    var k = s.charCodeAt(i);
	    var t = s.charAt(i);
		if (t.search(illegal)!=-1) {
		 	if((k>191)&&(k<256)) {}
			else {return true;}
		}
	}
}

function cText(f,n,sp) {
	nT = document.forms[f].elements[n].value;
	newf = f;
	if (isWText(nT)) sError(newf);
	else {
		cError(newf);
		if (sp) document.forms[f].submit();
	}
}


function cFields(n) {
	var f = document.forms[0];
	var er = false;
	nS = "You have left a required field blank or used illegal characters."
	for (i=0;i<n;i++) {
		s = f.elements[i].value;
		if (isBlank(s)||isIllegalText(s)) {er=true;break;}
	}
	if (er) sError(0,nS);
	else f.submit();
}

function sError(f,s) {
	var a = (s)? s:'ERROR';
	if (document.layers) alert(a);
	else {document.getElementById('msg'+f).innerHTML = a+"  &nbsp;";}
}

function cError(f) {
	if (!document.layers) {document.getElementById('msg'+f).innerHTML = ' ';}
}




