popWindow=null;

function NewWinmiddle(myurl, w, h){
	width=w;
	height=h;

	url=myurl;
	windowName="largeView";
	agent=navigator.appVersion;
	version=parseInt(agent);
	//figure out the top/left of the screen
	//taking the menu bar into consideration
	if (version>=4) {
	leftCorner=screen.width/2 - width/2;
	topCorner=screen.height/2 - height/2;
	}else{
	leftCorner=20;
	topCorner=20;
	}
	features=",screenX="+leftCorner+",screenY="+topCorner+",left="+leftCorner+",top="+topCorner;
	features=features+",scrollbars=0,resizable=1";
	if (popWindow==null) {
	popWindow=open(url,windowName, "width="+width+",height="+height+ features);
	// for IE3, focus, closed, and opener doesn't work, so we have to tell
	// the window opened who's the opener
	if (version<3)
		setTimeout("popWindow.mama=window",250);
	}else{
		if (version<3) {
			popWindow.close();
			popWindow=null;
			setTimeout("NewWinmiddle('"+url+"',"+width+","+height+")", 250);
			setTimeout("popWindow.mama=window",500);
		} else if (!popWindow.closed){
					popWindow.close();
					popWindow=null;
					setTimeout("NewWinmiddle('"+url+"',"+width+","+height+")", 250);

				}else{
					popWindow=null;
					popWindow=open(url,windowName, "width="+width+",height="+height+features);
					popWindow.focus();
				}
	}
}//end function

function Toggle(e, thisForm)    {
		if (e.checked) {
			Highlight(e);
		    thisForm.toggleAll.checked = AllChecked();
		}	else {
		    Unhighlight(e);
	    thisForm.toggleAll.checked = false;
		}
}//end fct

    function ToggleAll(e, thisForm)
    {
	if (e.checked) {
	    CheckAll(thisForm);
	} else {
	    ClearAll(thisForm);
	}  }

    function Check(e)
    {
	e.checked = true;
	Highlight(e);
    }

    function Clear(e)
    {
	e.checked = false;
	Unhighlight(e);
    }

    function CheckAll(thisForm)
    {
	var ml = thisForm;
	var len = ml.elements.length;
	for (var i = 0; i < len; i++) {
	    var e = ml.elements[i];
	   // if (e.name == "Mid") {
		Check(e);
	   // }
	}
	//ml.toggleAll.checked = true;
    }

    function ClearAll(thisForm)
    {
	var ml = thisForm;
	var len = ml.elements.length;
	for (var i = 0; i < len; i++) {
	    var e = ml.elements[i];
	 //   if (e.name == "Mid") {
		Clear(e);
	//    }
	}
	//ml.toggleAll.checked = false;
    }

    function Highlight(e)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
	    if (r.className == "msgnew") {
		r.className = "msgnews";
	    }
	    else if (r.className == "msgold") {
		r.className = "msgolds";
	    }
	}
    }

function Unhighlight(e)
    {
	var r = null;
	if (e.parentNode && e.parentNode.parentNode) {
	    r = e.parentNode.parentNode;
	}
	else if (e.parentElement && e.parentElement.parentElement) {
	    r = e.parentElement.parentElement;
	}
	if (r) {
	    if (r.className == "msgnews") {
		r.className = "msgnew";
	    }
	    else if (r.className == "msgolds") {
		r.className = "msgold";
	    }
	}
}//end fct

function AllChecked(thisForm)
    {
	ml =thisForm;
	len = ml.elements.length;
	for(var i = 0 ; i < len ; i++) {
	    if (ml.elements[i].name == "Mid" && !ml.elements[i].checked) {
		return false;
	    }
	}
	return true;
}//end fct



function Hide(valoare){
document.getElementById(valoare).style.visibility='hidden';
document.getElementById(valoare).style.display = 'none';

}
function Show(valoare){
document.getElementById(valoare).style.visibility='visible';
document.getElementById(valoare).style.display = 'block';
}
function ShowHide(label) {
		if (document.getElementById(label).style.display == 'none') {
			document.getElementById(label).style.visibility='visible';
			document.getElementById(label).style.display = 'block'
		 }
		else {document.getElementById(label).style.visibility='hidden';
			document.getElementById(label).style.display = 'none';
		}
}



var ns6=document.getElementById&&!document.all
var ie=document.all




function open_win(url, title, width, height){
//screenX. screenY ;

	var scrwidth=screen.width;
	var scrheight=screen.height;
	var top=(scrheight-height)/2;
	var left=(scrwidth -width)/2;
	var prop='width='+width+', height='+height+', top='+top+', left='+left+', directories=no, resizable=yes, scrollbar=yes';
	popup=window.open(url, title, prop);
	popup.focus();

} //END FUNCTION




function Check_Form(frm){ //check form required fields
	var fields_names = new Array('add_title', 'add_content', 'category', 'id_list', 'offer_title', 
		'offer_domain', 'description', 'unlogged_sender_name', 'unlogged_sender_email', 'subject');
	var fields_mess = new Array('Titlu anunt', 'Continut anunt', 'Categoria', 'Lista de parteneri', 'Subiectul cererii de oferta', 
		'Domeniul cererii', 'Obiectul cererii', 'Nume', 'Email', 'Subiect');
	var len= frm.elements.length;
	var nrq=0;
	var ans_arr=new Array();
	var idq_arr=new Array();
	var req_val="";
	for (i=0; i<len; i++){
		var el =frm.elements[i];


		if (el.id=="required") { 
			req_val=req_val+', '+el.name;
			if (el.type=='select-one' && el.value=='0') {
				for(var j=0; j<fields_names.length; j++){
					if(fields_names[j]==el.name){
						var this_elem_message = fields_mess[j];
					}
				}
				alert ("Acest camp (" + this_elem_message + ") este obligatoriu!");
				el.focus();
				return false;
			}//end if
			if (el.value=="" ) {
				for(var j=0; j<fields_names.length; j++){
					if(fields_names[j]==el.name){
						var this_elem_message = fields_mess[j];
					}
				}
				alert ("Acest camp (" + this_elem_message + ") este obligatoriu!");
				el.focus();
				return false;
			} //end if (el.value=="" )

		}//end if (el.id=="required")

		if ((el.name=='year') && (el.value1900 || el.value>2010 || (parseFloat(el.value)!=el.value)) ){
			alert ("Valoarea introdusa pentru an nu este corecta!");
			el.focus();
			return false;
		}
		if ((el.name=='price') &&  (parseFloat(el.value)!=el.value)) {
			alert ('Valoarea acestui camp trebuie sa fie un numar!'); //Price must be a number! Do not use characters!
			el.focus();
			return false;
		}

		if (el.name=='email')  {
			var emailStr = el.value;
			var emailPat=/^(.+)@(.+)$/;
			var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
			var validChars="\[^\\s" + specialChars + "\]";
			var quotedUser="(\"[^\"]*\")";
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
			var atom=validChars + '+';
			var word="(" + atom + "|" + quotedUser + ")";
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");


			var matchArray=emailStr.match(emailPat);
			if (matchArray==null) {
				alert("Adresa de email nu este corecta");//Email address seems incorrect (check @ and .'s)
				frm.email.focus();
				return false;
			}
			var user=matchArray[1];
			var domain=matchArray[2];

			// See if "user" is valid
			if (user.match(userPat)==null) {
				// user is not valid
				alert("Adresa de email nu este corecta.") ; //The username doesn't seem to be valid.
				frm.email.focus();
				return false;
			}


			// Domain is symbolic name
			var domainArray=domain.match(domainPat)
			if (domainArray==null) {
				alert("Domeniul nu este valid.") ; //The domain name doesn't seem to be valid
				frm.email.focus();
				return false;
			}
			var atomPat=new RegExp(atom,"g");
			var domArr=domain.match(atomPat);
			var len2=domArr.length;
			if (domArr[domArr.length-1].length<2 || 	domArr[domArr.length-1].length>3) {
			   // the address must end in a two letter or three letter word.
			   alert("Adresa de email este invalida."); //The address must end in a three-letter domain, or two letter country.
			   frm.email.focus();
			   return false
			}

			// Make sure there's a host name preceding the domain.
			if (len2<2) {
			   alert("Adresa de email este invalida!"); // This address is missing a hostname
			   frm.email.focus();
			   return false
			}
		}//end if (el.name=='email')

	}//end for
	return true;
}//end function

var clicks = 0;

function alerterSubmit()
{
	var show=false;
	if (show == true)
	{
		alert('If you encounter an error after submitting your ad, do not try again or you may be charged more than once.  Call: 1-800-387-2337 or email: paa@traderonline.com. ');
	}
	return true;
}


//***********************************************************************************//
function isNumeric(val)
{
	// R. Goodwin 10.01.01
	var valStr = val.toString();
	var foundError = false;
	for (var i = 0; i < valStr.length; i++)
	{
		var thisChar = valStr.charAt(i)
		if (thisChar < '0' || thisChar > '9')
		{
			foundError = true;
		}
	}
	if (foundError == true)
	{
		return false;
	}
	else
	{
		return true;
	}
}
function isFloat(val)
{
	// R. Goodwin 10.01.01
	var valStr = val.toString();
	var foundError = false;
	for (var i = 0; i < valStr.length; i++)
	{
		var thisChar = valStr.charAt(i)
		if (thisChar < '0' || thisChar > '9')
		{
			if (thisChar != ".")
			{
				foundError = true;
			}
		}
	}
	if (foundError == true)
	{
		return false;
	}
	else
	{
		return true;
	}
}
function phoneFormat(field)
{
	// This nifty function returns true if this is a phone format.
	var test = field.toString();
	var badphone = true;
	//var newphone = '';
	// If there's a length of the phone number
	if (test.length >= 1)
	{
		for (var i = 0; i < test.length; i ++)
		{
			testChar = test.charAt(i);
			if (testChar >= "0" && testChar <= "9")
			{
				newphone = newphone + testChar;
			}
		}
	}
	if (newphone.length == 10 || newphone.length == 7)
	{
		badphone = false;
	}
	if (badphone == true)
	{
		newphone = '';
		return false;
	}
	else
	{
		return true;
		return newphone;
	}
}
function isEmail(field)
{
	// R. Goodwin 10.01.01
	var stopLen = field.length;
	var foundAt = false;
	var foundDot = false;
	for (var i = 0; i < stopLen; i ++)
	{
		var thisChar = field.charAt(i);
		if (thisChar == '@')
		{
			foundAt = true;
		}
		if (thisChar == '.')
		{
			foundDot = true;
		}
	}
	if (foundAt == true && foundDot == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//****************************************************//






function testField(field,type)
{
	//var newphone = '';
	if (type == 'text')	{
		field = field.toString();
		field = field.replace(" ","");
		if (field.length <= 0)		return false;
		else return true;
	}
	if (type == 'integer')	{
		if (isNumeric(field) == false)	{
			return false;
		} else 	{
			return true;
		}
	}
	if (type == 'float')	{
		if (isFloat(field) == false) 		{
			return false;
		} else 		{
			return true;
		}
	}
	if (type == 'phone')
	{
		if (phoneFormat(field,newphone) == false)
		{
			return false;
		}
		else
		{
			return newphone;
		}
	}
	if (type == 'email')
	{
		if (isEmail(field) == false)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	if (type == 'select')
	{
		if (field.options[field.selectedIndex].value == '' || field.options[field.selectedIndex].value == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	if (type == 'cc')
	{
		test = field.toString();
		if (test.length == 0)
		{
		 return false;
		}
		else if (test.slice(1,1) == 5 && test.length != 16)
		{
			return false;
		}
		else if (test.slice(1,1) == 4 && (test.length != 13 || test.length != 16))
		{
			return false;
		}
		else if (test.slice(1,1) == 3 && test.length != 15)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}

function findInString(string,val)
{
	var str = string.toString();
	var strLen = str.length;
	var found = false;
	var text = '';
	for (var x = 0; x < strLen; x++)
	{
		if (str.charAt(x) == ',' || str.charAt(x) == '|')
		{
			text = '';
		}
		else
		{
			text = text + str.charAt(x);
		}
		if (text == val)
		{
			found = true;
		}
	}
	if (found == true)
	{
		return true;
	}
	else
	{
		return false;
	}
}
