// JavaScript Document
var xmlHTTP
var Category
var Cities
var categoryXMLHttp
var stateXMLHttp
var imageXMLHttp
var timeInterval
var uploadDir = ""
var filename = ""
var sign = ""
var remove = "&nbsp;&nbsp;&nbsp;<a href = '#' onclick = 'removeimage()'>entfernen</a>";


//function to select category
function selectCategory(str, selected)
{
	Category = str
    Subcategory = selected
	categoryXMLHttp = GetXmlHttpObject()
	if(categoryXMLHttp == null)
	{
		alert("XML-HTTP object was not Created")
		return;
	}
		var URL="categories.php?categoryID=" + str +"&catID=" + Math.random() +"&subcategoryID=" + selected
		categoryXMLHttp.onreadystatechange = Categorychanged
		categoryXMLHttp.open("GET",URL,"false")
		categoryXMLHttp.send(null)
		
}

//calls this function on selectCategory onreadystatechange
function Categorychanged()
{
	if(categoryXMLHttp.readyState == 4)
	{
	document.getElementById('subCategoryList').innerHTML = categoryXMLHttp.responseText
	}
	
}


//function to select Cities
function selectCities(str)
{
	//Cities = str
	//alert(Cities)
	
	var obj=document.getElementsByName("States")[0];
	for(i=0;i<=obj.options.length;i++)
	{
		if(obj.options[i].value==str)
		{
		obj.selectedIndex=i;
		break;
		}
	}
	stateXMLHttp = GetXmlHttpObject()
	if(stateXMLHttp == null)
	{
		alert("XML-HTTP object was not Created")
		return;
	}
		var URL="cities.php?stateid=" + str +"&sid=" + Math.random()
		//alert(URL)
		stateXMLHttp.onreadystatechange = statechanged
		stateXMLHttp.open("GET",URL,"false")
		stateXMLHttp.send(null)
		
}

//calls this function on selectCategory onreadystatechange
function statechanged()
{
		
	if(stateXMLHttp.readyState == 4)
	{
	document.getElementById('cityList').innerHTML = stateXMLHttp.responseText
	//document.getElementById('next').style.visibility='visible';
	}
	
}


//function to upload the image
function imageUploader()
{

	imageXMLHttp = GetXmlHttpObject()
	if(imageXMLHttp == null)
	{
		alert("XML-HTTP object was not Created")
		return
	}
	var URL="upload.php?filename=" + filename + "&uploadID=" + Math.random();
	imageXMLHttp.onreadystatechange = uploadImage
	imageXMLHttp.open("GET",URL,"false")
	imageXMLHttp.send(null)
}
//calls this function on imageUploader onreadystatechange
function uploadImage()
{
	
	if(imageXMLHttp.readyState==4)
	{
		if(imageXMLHttp.responseText.length > 0)
		{
			clearInterval(timeInterval);
		}
		//document.getElementById('image').innerHTML=imageXMLHttp.responseText
		document.getElementById('image').innerHTML = filename + remove
		document.postads.images.value = imageXMLHttp.responseText
	}
	
}

function uploadForm(obj)
{
	uploadDir = obj.value;
	filename = uploadDir.substr(uploadDir.lastIndexOf('\\')+1);
	document.getElementById('uploadimage').submit();
	timeInterval = setInterval("imageUploader()", 500);
}
//function to sign up
function signUp(str)
{
	var email=str
	//alert(email)
	signXMLHttp = GetXmlHttpObject()
	if(signXMLHttp == null)
	{
		alert("XML-HTTP object was not Created")
		return
	}
	var URL="signup.php?email=" + email + "&eid=" + Math.random()
	signXMLHttp.open("GET",URL,"false")
	signXMLHttp.onreadystatechange=myAccount
	signXMLHttp.send(null)
}
function myAccount()
{
	if(signXMLHttp.readyState == 4)
	{
		var response = signXMLHttp.responseText.split(",");
		sign = response[1]
		if(sign == 1)
		{
			document.getElementById('error').innerHTML = response[0]
			document.getElementById('email').focus()
			
		}
		if(sign == 2)
		{
			document.getElementById('error').innerHTML = response[0]
			document.getElementById('email').select()
			
		}
		if(sign == 3)
		{
			document.getElementById('error').innerHTML = response[0]
			document.getElementById('password').focus()
			
		}
		
	}
}
function validate()
{
	if(sign == 3 && document.getElementById('password').value != 0 && document.getElementById('password2').value != 0 &&
		document.getElementById('password').value == document.getElementById('password2').value)
	{
		return true
	}
	else
	{
		return false
	}
}

function validateSignUp()
{
	if(sign == 3 && document.getElementById('password').value != 0 && document.getElementById('password2').value != 0 &&
		document.getElementById('password').value == document.getElementById('password2').value)
	{
		return true
	}
	else
	{
		return false
		alert("Bitte geben Sie in beiden Passwortfeldern identische Passw&ouml;rter an");
	}
}

//function to login
function login(str)
{
	var email=str
	//alert(email)
	signXMLHttp = GetXmlHttpObject()
	if(signXMLHttp == null)
	{
		alert("XML-HTTP object was not Created")
		return
	}
	var URL="signup.php?email=" + email + "&eid=" + Math.random()
	signXMLHttp.open("GET",URL,"false")
	signXMLHttp.onreadystatechange=myLogin
	signXMLHttp.send(null)
}
function myLogin()
{
	if(signXMLHttp.readyState == 4)
	{
		var response = signXMLHttp.responseText.split(",");
		sign = response[1]
		if(sign == 1)
		{
			document.getElementById('error').innerHTML = response[0]
			document.getElementById('email').focus()
			
		}
		else
		{
			document.getElementById('error').innerHTML = ""
		}
	}
}


function loginvalidate()
{
	if(sign > 1 && document.getElementById('password').value > 0 )
	{
		return false
	}

}

//function to create XMLHTTP object
function GetXmlHttpObject()
{
	var xmlHttp = null
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest()
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
      }
    catch (e)
      {
      try
        {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!")
        }
      }
    }
   return xmlHttp
}


//function to validate the category list
function selectSubcategory()
{
	
	if(document.getElementById('Categories').value == '')
	{
		alert("Select the Category")
		return false
	}
	if(document.getElementById('Subcategories').value == '')
	{
		alert("Select the SubCategory")
		return false
	}
		
}

//to remove the uploaded image
function removeimage()
{
	document.getElementById('image').innerHTML = '<input name="uploader" type="file" id="uploader" onchange="uploadForm(this);"/>';
	document.category.images.value = ""
}
//to submit a form

function formSubmit(form_name)
{
document.getElementById(form_name).submit()
}

function editImageLoad()
{
	alert(document.getElementById('images').value);
	document.getElementById('editimage').src=document.getElementById('images').value
}
//loads the location w.r.to the posted location
function dropselect(hidden,object)
{
	var hidden=document.getElementsByName(hidden)[0].value;
	var obj=document.getElementsByName(object)[0];
	
	for(i=0;i<=obj.options.length;i++)
	{
		if(obj.options[i].value==hidden)
		{
		obj.selectedIndex=i;
		break;
		}
	}
}

//used to redirect the site to the corresponding ad
function postRedirect(value)
{
	window.location.href="classified/?c="+value+"&action=newad";
	return false;
}

function contactValidation(formName){

if(isEmail(((formName.email)),'Email')){return false;}
if(isEmpty(((formName.message)),'Message')){return false;}
return false							 
}

function isEmpty(var1,var2)
{
	if(var1.value=="")
	 {
		 alert(var2)
		 var1.focus()
	     return true
 	 }
}

function isEmail(var1,var2)
{
	if(var1.value=="")
	 {
		 alert("Bitte  "+var2+" Adresse eingeben");
		 var1.focus()
	     return true
 	 }
     if(var1.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
	{
		alert("Ihre  "+var2+" Adresse ist fehlerhaft");
		var1.focus()
		return true
	}
	 
}

function updateCaptcha()
{
	
var img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
}

//contact form
function contactForm(elementId,postedId,location,captcha,cssClass,linkclass, postEmailId, postMessage)
{
 
	var control=document.getElementById(elementId);
	
		control.style.display='block';
        
		postMessage = postMessage.replace(/<br \/>/g, "\n");
	control.innerHTML='<form  name="cForm" method="post" id="cForm"><div  style=" margin:0px 0px 0px 0px; width:428px; border:2pt solid #999999;"><div style="line-height:36px;" class="'+cssClass+'-crumb"><div align="center" style="font-family:Verdana, Arial, Helvetica, sans-serif;"><div style="float:right;" class="'+linkclass+'"><a href="javascript:void(0)" onclick="afterpopup(\'contact\')" style="text-decoration:none; color: #999999; ">[X]</a>&nbsp;&nbsp;</div><div align="center" ><strong>Kontakt</strong></div></div></div><div style="padding:5px 5px 0px 8px;"><div><label>Absender E-Mail: *</label></div><div> <input size="40" name="email" type="text" id="email" onfocus="updateCaptcha();" value="'+postEmailId+'" /></div><div><label>Nachricht: *</label></div><div><textarea cols="47" rows="5" name="message" id="message">'+postMessage+'</textarea></div><div><label>Sicherheitscode eingeben: *</label></div><div> <input size="8" name="captcha" type="text" onfocus="updateCaptcha();" id="captcha"/> <img src="create_image.php" alt=captcha height="30" id="imgCaptcha"/></div><div align="right" style="padding-top:5px;"><input type="button" name="send" id="send" value="Versenden"  onclick="sendNow();"/></div><br /></div></div></form>'
	ajaxPopup(control)
    new Draggable(control);
}

function ajaxPopup(htmlElement){
	htmlElement.style.zIndex="100";
	htmlElement.style.position="fixed";
	htmlElement.style.width="430px";
	htmlElement.style.margin='-25px 0px 0px 0px';
	htmlElement.style.backgroundColor="#F0F0F0";
	htmlElement.style.borderColor="#000000";
	htmlElement.style.left=((window.screen.width/2)-250)+'px';
	htmlElement.style.top=((window.screen.height/2)-275)+'px';
 
}

function afterpopup(popupElement)
{
  var con=document.getElementById(popupElement);
  con.style.zIndex="50";
  con.style.display='none';
  con.innerHTML='';

}

//redirect to classified page(advertisement listing)
function takeTo(catergoryId,cityId)
{
	location.href="classified.php?s="+catergoryId+"&l="+cityId;
	return false;
}

//textbox effect in search.php
function textEffect(textControl){
	if(textControl.value == 'min' || textControl.value == 'max' || textControl.value == 'Strasse' || textControl.value == 'Stadt' || textControl.value == 'Bundesland'){
	textControl.value='';
	}
	//on focus lost
	textControl.onblur=function(){
		var temp = textControl.value
		if(textControl.value == '' && textControl.value.length == 0){
			textControl.value=textControl.id
		}
		else{
			textControl.value=temp
		}
			
	}
}

//Delete the selected the advertisement
function deleteThisAd(postedId,currentPage,sKey){
	location.href="home.php?del="+postedId+"&key="+sKey+"&page="+currentPage
	return false;
}
function editThisAd(postedId,currentPage,subCategory,sKey){
	location.href="editAdvertisement.php?p="+postedId+"&page="+currentPage+"&key="+sKey+"&c="+subCategory
	return false;
}

//Delete the selected the event
function deleteThisEvent(eventId,currentPage,sKey){
	location.href="home_e.php?del="+eventId+"&key="+sKey+"&page="+currentPage
	return false;
}
function editThisEvent(eventId,sKey){
	location.href="editEvent.php?event="+eventId+"&key="+sKey
	return false;
}

function sendNow(){
	var emailID=document.getElementById('email')
	var message=document.getElementById('message')
	var captchaString=document.getElementById('captcha')
	if(isEmail((emailID),'E-Mail ')){return false;}
	if(isEmpty((message),'Bitte eine Nachricht eingeben')){return false;}
	if(isEmpty((captchaString),'Bitte den Sicherheitscode eingeben')){return false;}
	if(!isEmail(emailID,'Email') && !isEmpty(message,'Message') && !isEmpty(captchaString,'Captcha')){
		formSubmit('cForm');
		return true;
	}
//return false
	
	//alert("send now");
}


