var xmlHttp;

function totextmode()
{
		var returnedtext;
		try{xmlHttp=new XMLHttpRequest();}
		catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
		xmlHttp.onreadystatechange=function()
		{if(xmlHttp.readyState == 4 ){location.reload();};}
		var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	  
		xmlHttp.open("GET","/totextmode.php?"+ strTS ,true);
		xmlHttp.send(null);		
}

function topicturemode()
{
		var returnedtext;
		try{xmlHttp=new XMLHttpRequest();}
		catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
		xmlHttp.onreadystatechange=function()
		{if(xmlHttp.readyState == 4 ){location.reload();};}
		var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	  
		xmlHttp.open("GET","/topicturemode.php?"+ strTS ,true);
		xmlHttp.send(null);			
}

function showlocation()
{
	var e = document.getElementById('copyadlocation');
	if(e.style.display =='none')
		e.style.display = 'block';
	else
		e.style.display = 'none';
		
	return;	
}

function jsfilterbytag(x,s)
{
	if(x=='') 
	{
		window.location = "/seller/" + s + "/1/";
		return;
	}
	window.location = "/seller/"+ s +"/tag/"+ x + "/1/";
}

function switchtagbox(x)
{	
	var y = document.getElementById('databox'+x).innerHTML
	document.getElementById('tagbox').innerHTML = y;
	document.getElementById('tagbox').style.display = 'block';
	return;
}

function switchall()
{
	document.getElementById('commentsbox').innerHTML = document.getElementById('positive_comments').innerHTML + document.getElementById('neutral_comments').innerHTML + document.getElementById('negative_comments').innerHTML;	
}

function switchpositive()
{
	document.getElementById('commentsbox').innerHTML = document.getElementById('positive_comments').innerHTML;
}

function switchneutral()
{
		document.getElementById('commentsbox').innerHTML = document.getElementById('neutral_comments').innerHTML;
}

function switchnegative()
{
		document.getElementById('commentsbox').innerHTML = document.getElementById('negative_comments').innerHTML;
}

function removefromfavour(adid2,userid)
{
				var returnedtext;
				try{xmlHttp=new XMLHttpRequest();}
				catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
				var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	
				var url = "/fav.php?";
				var params =   '&userid=' + userid
				 			 + '&a=REMOVE'
							 + '&adid2=' + adid2;
							 
				xmlHttp.open("POST", url, true);
				//Send the proper header information along with the request
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				
				xmlHttp.onreadystatechange = function() 
				{//Call a function when the state changes.
					if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
					{
						returnedtext = xmlHttp.responseText; 
						if(returnedtext == 'OK')
						{
							alert('Ad removed from favourites');
							document.getElementById('favourlink').innerHTML = '<a href="javascript:;;" rel="nofollow" onclick="addtofavour(\''+ adid2 +'\',\'' + userid + '\');"><strong>[+] Add to Favourites</strong></a>';
						}
					};
				};
				xmlHttp.send(params);
}

function addtofavour(adid2,userid)
{
				var returnedtext;
				try{xmlHttp=new XMLHttpRequest();}
				catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
				var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	
				var url = "/fav.php?";
				var params =   '&userid=' + userid
				 			 + '&a=ADD'
							 + '&adid2=' + adid2;
							 
				xmlHttp.open("POST", url, true);
				//Send the proper header information along with the request
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				
				xmlHttp.onreadystatechange = function() 
				{//Call a function when the state changes.
					if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
					{
						returnedtext = xmlHttp.responseText; 
						if(returnedtext == 'OK')
						{
							alert('Ad added to favourites');
							document.getElementById('favourlink').innerHTML = '<a href="javascript:;;" rel="nofollow" onclick="removefromfavour(\''+ adid2 +'\',\'' + userid + '\');"><strong>[-] Remove from Favourites</strong></a>';
						}
					};
				};
				xmlHttp.send(params);
}

function showratebox()
{
	var e = document.getElementById('ratebox');
	if(e.style.display =='none')
		e.style.display = 'block';
	else
		e.style.display = 'none';
		
	return;
}

function viewcomments()
{
	var e = document.getElementById('commentsbox');
	if(e.style.display =='none')
	{
		e.style.display = 'block';
	}
	else
	{
		e.style.display = 'none';
	}
		
	return;		
}

var q=Array();

function get_ratings(userid)
{
		var xmlHttp;
		var returnedtext;
		try{xmlHttp=new XMLHttpRequest();}
		catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{ 
				returnedtext = xmlHttp.responseText;  
				var s;
				s = returnedtext.split(',');
				document.getElementById('positive_rating').innerHTML = s[0];
				document.getElementById('neutral_rating').innerHTML = s[1];
				document.getElementById('negative_rating').innerHTML = s[2];
				document.getElementById('classifieds_rating').innerHTML = s[3];				
			};
		};
		var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	  
		xmlHttp.open("GET","/get_ratings.php?userid=" + userid  + "&"+ strTS ,true);
		xmlHttp.send(null);		
}

function neutral(userid,ad)
{
				if( xmlHttp )
				{
					q[q.length]=userid;
					q[q.length]=ad;
					return false;
				}
				document.getElementById("fade").style.display = 'block';
				document.getElementById("light").style.display = 'block';					
				var returnedtext;
				try{xmlHttp=new XMLHttpRequest();}
				catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
				var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	
				var url = "/rate.php";
				var params =   'sellerid=' + userid
							 + '&adid2=' + ad
							 + '&comment=' + encodeURIComponent(document.getElementById('txacomments').value)
							 + '&ratetype=NEITHER'
							 + '&t=' + strTS;
							 
				xmlHttp.open("POST", url, true);
				
				//Send the proper header information along with the request
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				
				xmlHttp.onreadystatechange = function() 
				{//Call a function when the state changes.
					if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						if(response == 'OK')
						{
							document.getElementById('answerbox').innerHTML = '<strong>Your rating was submitted successfully</strong>';
							get_ratings(userid);
						}
						if(response == 'RATED_BEFORE')
						{
							document.getElementById('answerbox').innerHTML = '<strong>You have rated on this Ad previously</strong>';															
						}
						xmlHttp=null;
   						if(q.length)
						{
							a=q.shift();
							b=q.shift();
							positive(a,b);
						}						
						document.getElementById("fade").style.display = 'none';
						document.getElementById("light").style.display = 'none';							
					};
				};
				xmlHttp.send(params);
}

function negative(userid,ad)
{
				if( xmlHttp )
				{
					q[q.length]=userid;
					q[q.length]=ad;
					return false;
				}
				document.getElementById("fade").style.display = 'block';
				document.getElementById("light").style.display = 'block';					
				var returnedtext;
				try{xmlHttp=new XMLHttpRequest();}
				catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
				var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	
				var url = "/rate.php";
				var params =   'sellerid=' + userid
							 + '&adid2=' + ad
							 + '&comment=' + encodeURIComponent(document.getElementById('txacomments').value)
							 + '&ratetype=NEGATIVE'
							 + '&t=' + strTS;
							 
				xmlHttp.open("POST", url, true);
				
				//Send the proper header information along with the request
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				
				xmlHttp.onreadystatechange = function() 
				{//Call a function when the state changes.
					if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						if(response == 'OK')
						{
							document.getElementById('answerbox').innerHTML = '<strong>Your rating was submitted successfully</strong>';
							get_ratings(userid);
						}
						if(response == 'RATED_BEFORE')
						{
							document.getElementById('answerbox').innerHTML = '<strong>You have rated on this Ad previously</strong>';															
						}
						xmlHttp=null;
   						if(q.length)
						{
							a=q.shift();
							b=q.shift();
							positive(a,b);
						}						
						document.getElementById("fade").style.display = 'none';
						document.getElementById("light").style.display = 'none';							
					};
				};
				xmlHttp.send(params);
}

function positive(userid,ad)
{


				if( xmlHttp )
				{
					q[q.length]=userid;
					q[q.length]=ad;
					return false;
				}
				document.getElementById("fade").style.display = 'block';
				document.getElementById("light").style.display = 'block';				
				var returnedtext;
				try{xmlHttp=new XMLHttpRequest();}
				catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
				var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	
				var url = "/rate.php";
				var params =   'sellerid=' + userid
							 + '&adid2=' + ad
							 + '&comment=' + encodeURIComponent(document.getElementById('txacomments').value)
							 + '&ratetype=POSITIVE'
							 + '&t=' + strTS;
							 
				xmlHttp.open("POST", url, true);
				
				
				//Send the proper header information along with the request
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				
				xmlHttp.onreadystatechange = function() 
				{//Call a function when the state changes.
					if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						if(response == 'OK')
						{
							document.getElementById('answerbox').innerHTML = '<strong>Your rating was submitted successfully</strong>';
							get_ratings(userid);
						}
						if(response == 'RATED_BEFORE')
						{
							document.getElementById('answerbox').innerHTML = '<strong>You have rated on this Ad previously</strong>';															
						}
						xmlHttp=null;
   						if(q.length)
						{
							a=q.shift();
							b=q.shift();
							positive(a,b);
						}						
						document.getElementById("fade").style.display = 'none';
						document.getElementById("light").style.display = 'none';							
					};
				};
				xmlHttp.send(params);
}

function stripHTML(x)
{	
	var stripped = x.replace(/(<([^>]+)>)/ig,""); 
	return stripped;
}

function toggleDisplayMod(x)
{
	var y = document.getElementById(x);
	if(y.style.display=='none')
	{
		y.style.display = 'block';
	}
	else
	{
		y.style.display = 'none';			
	}
}

function toggleMode()
{
	var x = document.form1.editmode;	
		
	var xLength = x.length;

	for(var i = 0; i < xLength; i++) 
	{
		if(x[i].checked) 
		{
			if(x[i].value == "simplemode")
			{
				document.getElementById('simpleeditarea').style.display = 'block';
				document.getElementById('fulleditarea').style.display = 'none';	
				//document.getElementById('prestrip').value = document.getElementById('ad_description_rEdit').contentDocument.body.innerHTML;
				//document.getElementById('ad_description_simple').value = document.getElementById('ad_description_rEdit').contentDocument.body.innerHTML;
			}
			if(x[i].value == "advancemode")
			{
				document.getElementById('fulleditarea').style.display = 'block';
				document.getElementById('simpleeditarea').style.display = 'none';	
				//document.getElementById('ad_description_rEdit').contentDocument.body.innerHTML = document.getElementById('ad_description_simple').value;				
			}			
		}
	}			

}

function changeValue()
{
	document.getElementById('ad_hiddencategory').value = document.getElementById('ad_category').value;
}

function getElementsByClass(theClass) 
{
    var allPageTags = new Array();

    //Populate the array with all the page tags
    allPageTags=document.getElementsByTagName("*");
    var Elements = new Array();

    //Cycle through the tags
    var n = 0;
    for (i=0; i<allPageTags.length; i++) {
        //Pick out the tags with our class name
        if (allPageTags[i].className==theClass) {
            Elements[n] = allPageTags[i];
            n++;
        }
    }

    return Elements;
}


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function setCookie(NameOfCookie, value) 
{
	var ExpireDate = new Date ();
	var expiredays;
	
	ExpireDate.setTime(ExpireDate.getTime() + (365 * 24 * 3600 * 1000));

	document.cookie = NameOfCookie + "=" + escape(value) + 
	
	((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

function deleteCookie(NameOfCookie) 
{
	document.cookie = NameOfCookie + "=" +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function getCookie(NameOfCookie)
{

	if (document.cookie.length > 0) 
	{ 
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) // Note: != means "is not equal to"
		{ 
			begin += NameOfCookie.length+1; 
			
			end = document.cookie.indexOf(";", begin);
			
			if (end == -1) end = document.cookie.length;
			
			return unescape(document.cookie.substring(begin, end)); 		
		} 
	}
	return null; 

}



/*Notoriously inefficient.
 =x....
*/

function openclosechild(cid)
{
	var divs = document.getElementsByTagName('div');
	var c = cid.toString();
	var x = c.split('_');
	for (var i = 0; i < divs.length; i++)
	{
			if(divs[i].className == 'closeopen')
			{		
				if(Left(divs[i].id.toString(),9) == 'childcat_')
				{	
					var y = divs[i].id.toString();
					var z = y.split('_');
					if(z[2].toString() == c.toString())
					{
						if(document.getElementById(divs[i].id).style.display == 'none')
							document.getElementById(divs[i].id).style.display = 'block';
						else
							document.getElementById(divs[i].id).style.display = 'none';
					}
				}
			}
	}
}

function detectTree()
{
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++)
	{
		if(divs[i].className == 'closeopen')
		{
			if(Left(divs[i].id.toString(),9) == 'childcat_')
			{
				var a1 = divs[i].id.toString();
				var a1_arr = a1.split('_');
				var a1_cat = a1_arr[2].toString();	
				if(getCookie(a1_cat) == 'CLOSE')
				{
					document.getElementById(divs[i].id).style.display = 'none';
					document.getElementById('plusminus_' + a1_cat).innerHTML = '[+]';
				}
				else if(getCookie(a1_cat) == 'OPEN')
				{
					document.getElementById(divs[i].id).style.display = 'block';
					document.getElementById('plusminus_' + a1_cat).innerHTML = '[-]';					
				}
			}
		}		
	}
}

function expandcollapse(catid) 
{
	var cid = document.getElementById('plusminus_' + catid);
	var message;
	if(cid.innerHTML == '[-]')
	{
		cid.innerHTML = '[+]';
		var divs = document.getElementsByTagName('div');
		for (var i = 0; i < divs.length; i++)
		{
			if(divs[i].className == 'closeopen')
			{
				if(Left(divs[i].id,10) == 'parentcat_')
				{
					var y = divs[i].id.toString();
					var z = y.split('_');
					if(z[1].toString() == catid.toString())
					{
						var a1 = divs[i].id.toString();
						var a1_arr = a1.split('_');
						var a1_cat = a1_arr[1];
						openclosechild(a1_cat);
						setCookie(catid,'CLOSE');
						return;
					}
				}
			}
		}
	}
	else
	{
		cid.innerHTML = '[-]';
		var divs = document.getElementsByTagName('div');
		for (var i = 0; i < divs.length; i++)
		{
			if(divs[i].className == 'closeopen')
			{
				if(Left(divs[i].id,10) == 'parentcat_')
				{
					var y = divs[i].id.toString();
					var z = y.split('_');
					if(z[1].toString() == catid.toString())
					{
						var a1 = divs[i].id.toString();
						var a1_arr = a1.split('_');
						var a1_cat = a1_arr[1];
						openclosechild(a1_cat);
						setCookie(catid,'OPEN');
						return;
					}				
				}
			}
		}
	}
}

function filtermyads()
{
	var x = document.getElementById('filterads').value;
	if(x == '_myads')
		window.location = 'myads.php?filter=0';
	if(x == '_myexpiredads')
			window.location = 'myads.php?filter=1#1';
	if(x == '_myongoingads')
			window.location = 'myads.php?filter=2#2';
	if(x == '_myexpiringads')
			window.location = 'myads.php?filter=3#3';			
		
	return;
}

function toggleadvsearch()
{
	var x = document.getElementById('advsearch');
	
	if(x.style.display == 'block')
	{
		x.style.display = 'none';
		document.getElementById('advsearch_link').innerHTML = 'More';
	}
	else
	{
		x.style.display = 'block';	
		document.getElementById('advsearch_link').innerHTML = 'Less';
	}
	
	return;	
}

function togglecomments()
{
	var x = document.getElementById('divcomments');
	
	if(x.style.display == 'block')
	{
		x.style.display = 'none';	
	}
	else
	{
		x.style.display = 'block';		
	}
	
	return;
}

function togglerelated()
{
	var x = document.getElementById('divrelated');
	
	if(x.style.display == 'block')
	{
		x.style.display = 'none';	
	}
	else
	{
		x.style.display = 'block';		
	}
	
	return;
}

function toggleoptionpane()
{
	var x = document.getElementById('divOptionsPane');
	
	if(x.style.display == 'block')
	{
		x.style.display = 'none';	
	}
	else
	{
		x.style.display = 'block';		
	}
	
	return;
}

function newad()
{
	 var width  = 860;
	 var height = 500;
	 var left   = (screen.width  - width)/2;
	 var top    = (screen.height - height)/2;
	 var params = 'width='+width+', height='+height;
	 params += ', top='+top+', left='+left;
	 params += ', directories=no';
	 params += ', location=no';
	 params += ', menubar=no';
	 params += ', resizable=no';
	 params += ', scrollbars=yes';
	 params += ', status=no';
	 params += ', toolbar=no';
	 newwin=window.open('/newad.php?','windowname5', params);
	 if (window.focus) {newwin.focus()}
	 return false;	
}


function editad(ad_id)
{
	 var width  = 850;
	 var height = 500;
	 var left   = (screen.width  - width)/2;
	 var top    = (screen.height - height)/2;
	 var params = 'width='+width+', height='+height;
	 params += ', top='+top+', left='+left;
	 params += ', directories=no';
	 params += ', location=no';
	 params += ', menubar=no';
	 params += ', resizable=no';
	 params += ', scrollbars=yes';
	 params += ', status=no';
	 params += ', toolbar=no';
	 newwin=window.open('/editmyad.php?em=1&ad_ref='+ad_id,'windowname5', params);
	 if (window.focus) {newwin.focus()}
	 return false;	
}

function quickjumptomyad()
{
	var x = document.getElementById('myadselector').value;
	window.location = 'myads.php' + x;
	return;
}

function removepreviewpicture()
{
	document.getElementById('uploadimg1').src = '/img/spacer.gif';
	document.getElementById('previewpicture').value = '';	
}

function showad(ad_id)
{
	 var width  = 850;
	 var height = 500;
	 var left   = (screen.width  - width)/2;
	 var top    = (screen.height - height)/2;
	 var params = 'width='+width+', height='+height;
	 params += ', top='+top+', left='+left;
	 params += ', directories=no';
	 params += ', location=no';
	 params += ', menubar=no';
	 params += ', resizable=no';
	 params += ', scrollbars=yes';
	 params += ', status=no';
	 params += ', toolbar=no';
	 newwin=window.open('/view.php?ad_ref='+ad_id,'windowname5', params);
	 if (window.focus) {newwin.focus()}
	 return false;
}

function showCategory()
{
	var strH = "<select  class='formSelect' id='ad_category' name='ad_category' onchange='changeValue();'>";	
	var xmlHttp;
	var returnedtext;
	try{xmlHttp=new XMLHttpRequest();}
	catch (e){try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX!");return false;}}}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{ 
			returnedtext = xmlHttp.responseText;  
			strH = strH + returnedtext + "</select>";
	
			document.getElementById('span_changeCategory').innerHTML = strH;	
			document.getElementById('ad_hiddencategory').value = document.getElementById('ad_category').value;
			
		};
	};
	
	var strTS = new Date().toString().replace(/(\s|\:)/gi,"");	  
	xmlHttp.open("GET","/cache/categories_dropdown.htm?"+ strTS ,true);
	xmlHttp.send(null);		
	
	return;
}