var answer = '';
function xmlhttpPost(strURL, variables) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {					
			var siteid = strURL.substr(strURL.indexOf("siteid")+7);
			
			if (self.xmlHttpReq.responseText!='no_alert') {
	            alert(self.xmlHttpReq.responseText);	
			}											
				
				
			var the_string = self.xmlHttpReq.responseText;
			if (the_string.substr(0,40) == 'The Tail Number entered is already used!') {
				document.getElementById('Tail Number').value='';
				if( window.clipboardData && clipboardData.setData )
					window.clipboardData.setData("Text", '');					
			}

			if (the_string.indexOf('was marked as Sold! It will be removed after') != -1) {
				opacity('ajax_sold_' + siteid, 0, 100, 400);	
				opacity('ajax_pending_' + siteid, 100, 0, 10);					
				opacity('mark_pending_' + siteid, 100, 0, 10);	
				opacity('undo_pending_' + siteid, 100, 0, 10);
				opacity('mark_sold_' + siteid, 100, 0, 10);
				document.getElementById('delete_button_' + siteid).style.marginRight = '0px';
				opacity('undo_sold_' + siteid, 0, 100, 400);																	
			}			

			if (the_string.indexOf('Marked as Sold was removed for the ad') != -1) {
				opacity('ajax_sold_' + siteid, 100, 0, 400);
				opacity('undo_sold_' + siteid, 100, 0, 10);	
				document.getElementById('delete_button_' + siteid).style.marginRight = '20px';								
				opacity('mark_pending_' + siteid, 0, 100, 400);		
				opacity('mark_sold_' + siteid, 0, 100, 400);									
			}				

			if (the_string.indexOf('was marked as Sale Pending!') != -1) {
				opacity('ajax_pending_' + siteid, 0, 100, 400);	
				opacity('mark_pending_' + siteid, 100, 0, 10);
				opacity('undo_pending_' + siteid, 0, 100, 400);	
			}			

			if (the_string.indexOf('The sale Pending was removed for the ad') != -1) {
				opacity('ajax_pending_' + siteid, 100, 0, 400);	
				opacity('undo_pending_' + siteid, 100, 0, 10);	
				opacity('mark_pending_' + siteid, 0, 100, 400);														
			}			
					
				
			if (self.xmlHttpReq.responseText == 'The ad (' + siteid + ') was removed from your listing!') 			
				opacity('content_div_' + siteid, 100, 0, 800);		
					
			if (self.xmlHttpReq.responseText == 'The dealers data was sent to your email account!') 			
				opacity('contact_details_' + siteid, 100, 0, 500);		
							
	        if (self.xmlHttpReq.responseText!='') {
	            answer = self.xmlHttpReq.responseText;	
			}	
								
			//document.getElementById('content_div_' + siteid).style.display = 'none';
			
			//window.location = '/index.php?section=MyAds&page=my_items&p=3&active=2';
        }
    }
    self.xmlHttpReq.send(variables);
}

function ajaxFunction(str){
	var ajaxRequest;
	var url="/index.php?section=search&page=models&manufacturer_id="+str;
	
	try{
		
		ajaxRequest = new XMLHttpRequest();
		
	} catch (e){
		
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
				x=ajaxRequest.responseText;
				
				var mytool_array=x.split("<HTML>");
				x='';
				
				string="";
				string=mytool_array[0];
				//alert('STRING_LEN:'+string.length);
				i=0;
				//alert('STRING:'+string);
				bool=0;
				bool2=0;
				array2=string.split(';');
				
				
			if(array2.length>1)	
			{
			document.getElementById('Model').options.length = 0;
				for(i=0;i<array2.length;i++)				
				{
					//alert('array2[i]:'+array2[i]);
					arr=array2[i].split('_');
					//alert('arr[i]'+arr[0]);
					if(arr[0]!=''){
						document.getElementById("Model").options[i] = new Option(arr[1],arr[0]);}
				}
			}	 
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
}
//function type_in_manufacturer(e_field,selected_value)
//{
//	if(selected_value==0)
	//{
//	 document.getElementById("e_115_1").disabled=false;
	 //document.getElementById("e_115_1").focus();
	//}
	//else{
	//	document.getElementById("e_115").disabled=true;
	//}
//}
var user_type=true;
function value_text_title2(title_category,text_name,catname)
{
  if(user_type==true)
  {

      var tag_id=document.itemForm.getElementsByTagName('input');

	  for (var i=0; i <= document.itemForm.elements.length-1;i++) 
	  {
	    var x=document.itemForm.elements[i].id;
	    
	    //add by beni-for create link 
	    if((catname=='planes')) {
	    if(x=='Model')
	      { model=document.itemForm.elements[i].value;}
	     if(x=='Type')
		   {
		      type=document.itemForm.elements[i].options[document.itemForm.elements[i].selectedIndex].text;

		    }  
	      
	    }
	    
	    //end add by beni
	    
	    
	    
		if((catname=='Cars') || (catname=='RVs') )
		{
	    if(x=='Model')
	      {model=document.itemForm.elements[i].value;}
	    if(x=='Engine')
	      {engine=document.itemForm.elements[i].value;}
		if(x=='Make')
		{
		make=document.itemForm.elements[i].options[document.itemForm.elements[i].selectedIndex].text;
		}
	   if(x=='Fuel Type')
	      {
 		   fuel_type=document.itemForm.elements[i].options[document.itemForm.elements[i].selectedIndex].text;   
		}
       }
       if(catname=='Homes')
        {
         
		  if(x=='Type')
		   {
		      type=document.itemForm.elements[i].options[document.itemForm.elements[i].selectedIndex].text;

			  }
		  if(x=='City') 
		   {
		     city=document.itemForm.elements[i].value;

			 }
		  if(x=='State') 
		   {
		      state=document.itemForm.elements[i].options[document.itemForm.elements[i].selectedIndex].text;

			}
		  if(x=='Zip') 
		   {
		     zip=document.itemForm.elements[i].value;
			}
		   
		}
	  }

	  var title=eval('document.itemForm.'+title_category);      
	      title.value='';
   	if((catname=='Cars') || (catname=='RVs'))  
		{title.value=make+' '+model+' '+' '+fuel_type+' '+engine;}
       if(catname=='Homes')
        {
		   title.value=type;
		   if(city!='')
		    {
		      if(type!='')
			   title.value+=', '+city
			  else
			   title.value+=' '+city;
			}
		   if(state!='') 
		    {
		      if(city!='')
				   title.value+=', '+state
				else   
					title.value+=' '+state
			 }
		   if(zip!='') 
		     {
				   title.value+=' '+zip;
				   
			 }
	 }

  }
}


function validate_add_form()
{

 var input=document.itemForm.getElementsByTagName("input");
 for(i=0;i<input.length;i++)
  {   
    if(input.item(i).name=='e_4')
      {
	    if(input.item(i).value>2006)
	       {alert('Please enter a valid year format,(ex.1998)'); return false;}
	     else
		  {return true;}
	  }
	if(input.item(i).name=='e_102')  
	 {
	    if(input.item(i).value>2006)
	       {alert('Please enter a valid year format,(ex.1998)'); return false;}
	     else
		  {return true;}
	   
	}
	if(input.item(i).name=='e_52')  
	 {
	    if(input.item(i).value>1000)
	       {alert('Please enter a smaller age ');return false;}
	     else
		  {return true;}
	   
	}	
  }
}

function iVal(val)
{
  var sTmp = "";
  var sAllow = "1234567890";
  for(i=0;i<val.length;i++) if(sAllow.indexOf(val.charAt(i) )>=0) sTmp+=val.charAt(i) ;
  return sTmp;
}

function rowYourBoat()
{

 var test=0;
 var checkboxes = document.getElementsByTagName("input");
   for(i=0;i<checkboxes.length;i++) 
    {
      if(checkboxes.item(i).attributes["type"].value == "checkbox")
	   {
         if(checkboxes.item(i).checked)
           {
		    	test=1;
		   }
		  
      }
   } 
  if(test==1)
	{
	 if(confirm('Are you sure?')) 
	   return true;
	  else
	   return false;
	}
    else
     {
     alert('YOU HAVE NOT SELECTED ANY ADS.');
		return false;
     }
}
function match_passwords()
{

var pass1=document.MyProfile.password.value;
var pass2=document.MyProfile.password2.value;
if(pass1==pass2)
  {return true;}
 else{
   alert("Passwords don't match! Please retype the passwords.");
   return false;
   }
}
function PGallery(id){
	window.open('/index.php?section=MyAds&page=photo_gallery&siteid='+id,'_ics','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=850,height=650');
}

function PVideos(id){
	window.open('/index.php?section=MyAds&page=videos_gallery&siteid='+id,'_ics','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=850,height=650');
}


function PDocuments(id){
	window.open('/index.php?section=MyAds&page=documents_gallery&siteid='+id,'_ics','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=850,height=650');
}

function openWin2(URL){ 
	
   aWindow=window.open(URL,"Large","toolbar=no,width=550,height=150,status=no,scrollbars=no,menubars=no");
}

function checkEmail(myForm) {
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(myForm.username.value))
{
return true;
}
else
{
alert("Invalid E-mail Address! Please re-enter.")
return false;
}
}
function checkEmail2(myForm) {
if ( (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(myForm.rec_email.value)))
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(myForm.sender_email.value))
			{return true;}
		else
		 {
		  alert("Invalid E-mail Address! Please re-enter-Sender-email.")
		  return false;
		 }	
}
else
{
alert("Invalid E-mail Address! Please re-enter-Receiver-email.")
return false;
}
}

function checkEmail3(myForm) {
if ( (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(myForm.epost.value)))
{
  return true;
}
else
{
alert("Invalid E-mail Address! Please re-enter.")
return false;
}
}

function openWindow(url)
{
	window.open(url,"leaddetail","menubar=no,location=no,resizable=no,scrollbars=yes,status=no,width=600,height=500");
}
function SelectAll_checkbox()
{
  for(i=0; i<document.forms["MyWishList"].elements.length; i++){
    if (document.forms["MyWishList"].elements[i].type == "checkbox"){
       if (document.forms["MyWishList"].elements[i].checked==false)
	       document.forms["MyWishList"].elements[i].checked=true;
	   }
  }
}		
function unSelectAll_checkbox()
{
  for(i=0; i<document.forms["MyWishList"].elements.length; i++){
    if (document.forms["MyWishList"].elements[i].type == "checkbox"){
       if (document.forms["MyWishList"].elements[i].checked==true)
	       document.forms["MyWishList"].elements[i].checked=false;
	   }
  }
}		

function onblurupper(evt, obiect) {
	var valoare=obiect.value;
	var val = valoare.toUpperCase();
	val = val.split(' ').join('');	
			
	obiect.value = val;		
			
	xmlhttpPost('/new_action.php?new_action=verify_tail&tailnr=' + val, '');
}

function onpasteupper(evt, obiect) {
	var valoare=window.clipboardData.getData("Text");	
	var val = valoare.toUpperCase();
	val = val.split(' ').join('');
	
	if( window.clipboardData && clipboardData.setData )
		window.clipboardData.setData("Text", val);
			
	xmlhttpPost('/new_action.php?new_action=verify_tail&tailnr=' + val, '');
}

function upperCase(evt, obiect)
      {	  	
         var charCode = (evt.which) ? evt.which : event.keyCode
		 if (charCode == 32) return false;
   	     if ((charCode >= 97 && charCode <= 122) || (charCode >= 65 && charCode <= 90)) {
			 var stringul = String.fromCharCode(charCode);		 
			 stringul = stringul.toUpperCase();
			 obiect.value = obiect.value + stringul;
		    if (obiect)
			if (obiect.value!='') {
				xmlhttpPost('/new_action.php?new_action=verify_tail&tailnr=' + obiect.value, '');
        		//if (answer != 'no_alert') obiect.value = '';
			}
			//if (answer != 'no_alert') obiect.value = obiect.value.substr(0,obiect.value.length-1); 
			return false;
		 }	 
		 
		 if (obiect)
		 if (obiect.value!='') {
			 xmlhttpPost('/new_action.php?new_action=verify_tail&tailnr=' + obiect.value, '');			
			// if (answer != 'no_alert') obiect.value = '';
		 }
		 //obiect.value = obiect.value.substr(0,obiect.value.length-1); 
		 return true;
      }
function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
		 if ((charCode != 44) && (charCode != 46))
			 if (charCode > 31 && (charCode < 48 || charCode > 57))
           		 return false;

         return true;
      }
function go(link)
{
	openWin2(link);
}
function generate_csv()
{
	document.getElementById('leads').action='/index.php?section=MyAds&page=leads&p=5&active=2&generate_csv=on';
	//document.getElementById('leads').submit();	
	document.getElementById('filter').click();	
}