function reload_captcha(len) {
	if(!len)	len = 5;

	$('cap').src = '/ads/getCaptcha/'+len+'/?'+new Date().getTime()+'.jpg';
}


function addOnClickForModel_1(){

	for (var i = 0; i < $('cars_model_id').options.length; i++) {

		if($('cars_model_id').options[i].attachEvent){
			// IE
		   $('cars_model_id').options[i].attachEvent('onclick', 'checkSelectedLength("cars_model_id", 3)');
		} else {
			// firefox
		   $('cars_model_id').options[i].setAttribute('onclick', 'checkSelectedLength("cars_model_id", 3)'); 
		}
	}
}


function addOnClickForModel(){

	setTimeout("addOnClickForModel_1()",2000);
}



function carpic() {

	var method = getRadioValue('pic_type');
	
	var uploadpic = $('uploadpic');
	
	if (method == 'default') {

		show('defaultpic');		
		if (uploadpic != null) {hide('uploadpic');}		
		hide('systempic');
	}

	else if (method == 'upload') {

		show('uploadpic');
		hide('systempic');
		hide('defaultpic');
	}

	else if(method == 'system') {

		hide('defaultpic');
		if (uploadpic != null) {hide('uploadpic');}
		show('systempic');

		getImages('cars_brand');
		getImages('cars_model');
		getImages('cars_year');
	}
}


function select_validator(s) {
	r = s+'_error'
	sel = $(s);
	
	if(sel.options[sel.selectedIndex].value == "0") {
	   $(r).innerHTML = '*';
	   return false;
	} else {
		$(r).innerHTML = '';
		return true
	}
}





function sell() {

	method = getRadioValue('is_cash');

	if (method == 1) {
		show('cash');
		hide('installments');
	}

	else if (method == 0) {
		hide('cash');
		show('installments');
	}
	
	else if (method == 2) {
		hide('cash');
		hide('installments');
	}
}



function get_status() {

	return getRadioValue('status');
}



function text_validator(s, m) {

	r = s+'_error'
	sta = get_status();

	if (s == 'country_id' || s == 'cars_brand_id' || s == 'cars_model_id') {err = '0'}
	else {err = ''}

	if ((m == 'r' && sta == 'o') || m != 'r') {

		if(document.getElementsByName(s)[0].value == err) {

		   $(r).innerHTML = '*';
		   return false;
		} else {

			if(!$(r))	return false;
			$(r).innerHTML = '&nbsp;'; return true;
		}
	} else{$(r).innerHTML = '&nbsp;';}	
}





function text_validator_co(s, m) {

	r = s+'_error'

	// company state, coupons state and stars state 

	if (s == 'state_id' || s == 'data[Star][state_id]') {err = ''; err1 = '0'}
	else {err = ''; err1 = ''}

	if(document.getElementsByName(s)[0].value == err || document.getElementsByName(s)[0].value == err1) {

	   $(r).innerHTML = '*';
	   return false;
	} else{$(r).innerHTML = '&nbsp;'; return true}
}



function email_validator(s) {

	r = s+'_error'

	mail = document.getElementsByName(s)[0].value;

	if(!validateEmail(mail)) {

	   $(r).innerHTML = "*";
	   return false;
	}

	else{$(r).innerHTML = "&nbsp;"; return true}
}



function validateEmail(email){	

	var emailStr = email;
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(')|(")/; // not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid

	if (!(!emailReg1.test(emailStr) && emailReg2.test(emailStr))) {// if syntax is valid

		return false;
	}
	return true;
}


function checkbox_validator(s){

	r = s+'_error'

	if($(s).checked == false) {
	   $(r).innerHTML = "*";
	   return false;
	}

	else{$(r).innerHTML = "&nbsp;"; return true}
}


function check_listing(m){
	if (m == 0) hide('mileage');
	else if(m == 1) show('mileage');
}


function check_status(v) {

	var arr = new Array();

	arr[0] = "cars_model_id";
	arr[1] = "car_year";

	if (v == 'r') {

		for(i=0; i<arr.length; i++) {$(arr[i]+'_error').innerHTML = "&nbsp;";}

	} else if (v == 'o') {

				for(i=0;i<arr.length;i++) {
					text_validator(arr[i]);
				}	
			}
}


function company_check_status(v) {

	var arr = new Array();

	arr[0] = "cars_model_id";
	arr[1] = "car_year";

	if (v == 'n'){
		
		for(i=0; i<arr.length; i++) {$(arr[i]+'_error').innerHTML = "&nbsp;";}
		
		hide('payment_method');
		hide('prices');
		show('rent_prices');
		
	} else if (v == 'o') {

		for(i=0;i<arr.length;i++) {
			text_validator(arr[i]);
		}
				
		show('payment_method');
		show('prices');
		hide('rent_prices');
		
	}
}


function search_check_status(v) {

	if (v == 'r') {

		show('payment_method');
		show('prices');
		hide('rent_prices');

	} else if (v == 'n'){
		
		hide('payment_method');
		hide('prices');
		show('rent_prices');
		
	} else if (v == 'o') {
				
		show('payment_method');
		show('prices');
		hide('rent_prices');
	}
}



function check_form_pass(){
	
	if ($F('password').blank() || $F('newpassword').blank() || $F('renewpassword').blank()){
		alert('من فضلك إملاء جميع الحقول');
		return false;
	}
	
	if ($F('newpassword') != $F('renewpassword')){
		alert('كلمات المرور غير متطابقة.');
		return false;
	}
	
	return true;	
}



function check_form_invite() {
	
	$('sub_but').disabled = true;
	
	var error = 0;
	
	for(i = 0; i < 5 ; i++) {

		var id = 'StarsGuestEmail'+i;

		if (!$F(id).empty()) {

			var mail = 1;

			if (!validateEmail($F(id))) {

				show('mail_error_'+i);
				error = 1;

			} else {hide('mail_error_'+i);}
		}else {hide('mail_error_'+i);}
	}

	if (error == 1) {$('sub_but').disabled = false;return false;}
	if (mail == 1) {return true;} else {alert('يجب ان تكتب بريد واحد على الأقل'); $('sub_but').disabled = false; return false;}
}



function check_form_co() {

	var error = 0
	var pass = $('validatepass');

	if (pass != null) {
		if (!text_validator_co('data[Company][password]') || !text_validator_co('data[Company][repassword]')){error = 1}
		
		if ($F('CompanyPassword') != $F('CompanyRePassword')){
			alert("كلمات المرور غير متطابقة");
			error = 1;
		}
	}

	var cap = $('data[Company][captcha]');

	if (cap != null) {
		if (!text_validator_co('data[Company][captcha]')){error = 1}
	}

	
	if (!text_validator_co('data[Company][name]') || !email_validator('data[Company][email]') || !text_validator_co('data[Company][phone]') || !text_validator_co('data[Company][country_id]')) {error = 1}
	
	if (error == 1) {show('error_tr'); return false;}
}




function check_form_journal() {

	var error = 0
	var pass = $('validatepass');

	if (pass != null) {
		if (!text_validator_co('data[Journal][password]') || !text_validator_co('data[Journal][repassword]')){error = 1}
		
		if ($F('JournalPassword') != $F('JournalRePassword')){
			alert("كلمات المرور غير متطابقة");
			error = 1;
		}
	}

	var cap = $('data[Journal][captcha]');

	if (cap != null) {
		if (!text_validator_co('data[Journal][captcha]')){error = 1}
	}

	
	if (!text_validator_co('data[Journal][name]') || !email_validator('data[Journal][email]') || !text_validator_co('data[Journal][phone]') || !text_validator_co('data[Journal][country_id]')) {error = 1}
	
	if (error == 1) {show('error_tr'); return false;}
}




function check_form_used() {

	var error = 0
	var pass = $('validatepass');

	if (pass != null) {
		if (!text_validator_co('data[Used][password]') || !text_validator_co('data[Used][repassword]')){error = 1}
		
		if ($F('UsedPassword') != $F('UsedRePassword')){
			alert("كلمات المرور غير متطابقة");
			error = 1;
		}
	}

	var cap = $('data[Used][captcha]');

	if (cap != null) {
		if (!text_validator_co('data[Used][captcha]')){error = 1}
	}

	
	if (!text_validator_co('data[Used][cat_id]') || !text_validator_co('data[Used][name]') || !email_validator('data[Used][email]') || !text_validator_co('data[Used][phone]') || !text_validator_co('data[Used][country_id]')) {error = 1}
	
	if (error == 1) {show('error_tr'); return false;}
}



function check_form_coupons() {

	error = 0

	if (!text_validator_co('name') || !text_validator_co('owner_name')) {error = 1}
	if (!validateEmail($F('email'))) {error = 1}
	if (error == 1) {show('error_tr'); return false;}
}



function check_form_stars() {

	var error = 0
	var pass = $('validatepass');

	if (pass != null) {if (!text_validator_co('data[Star][password]')){error = 1}}

	var cap = $('captcha');

	if (cap != null) {if (!text_validator_co('data[Star][captcha]')){error = 1}}

	if (!text_validator_co('data[Star][name]') || !email_validator('data[Star][email]') || !text_validator_co('data[Star][phone]') || !text_validator_co('data[Star][country_id]')) {error = 1}

	if (error == 1) {show('error_tr'); return false;}
}



function check_form_staff() {

	var error = 0

	var name = $('name');
	if (name != null) {if (!text_validator('name')){error = 1}}
		
	var captcha = $('captcha');

	if (captcha != null) {if (!text_validator('captcha')){error = 1}}
	if (!text_validator('phone') || !text_validator('country_id') || !text_validator('cars_brand_id')) {error = 1}
	if (get_status() == "o"){if (!text_validator('cars_model_id') || !text_validator('car_year')) {error = 1}}
	if (error == 1) {show('error_tr'); return false;}

	return true;
}


// personal add and edit advertisment page
function check_form() {

	var error = 0

	var name = $('name');
	if (name != null) {
		if (!text_validator('name')){error = 1}
	}
	
	var pass = $('password');
	if (pass != null && $F('validatepass') == "yes") {
		if (!text_validator('password')){error = 1}
	}

	var captcha = $('captcha');
	if (captcha != null) {
		if (!text_validator('captcha')){error = 1}
	}

	if (!$F('email').empty()){
		if (!email_validator('email'))	error = 1
	}

	if (!text_validator('phone') || !text_validator('country_id') || !text_validator('cars_brand_id') || !text_validator('cars_model_id') || !text_validator('car_year')) {error = 1}
		
	var accept = $('accept');
	if (accept != null) {
		if (!checkbox_validator('accept')){error = 1}
	}


	if (error == 1) {
		show('error_tr');
		return false;
	}

	return true;
}


// personal add and edit request advertisment page
function check_request_form() {

	var error = 0

	var captcha = $('captcha');
	if (captcha != null) {if (!text_validator('captcha')){error = 1}}
	
	var email = $('email');
	if (email != null) {
		if ($F('email')){
			if (!email_validator('email'))	error = 1
		}
	}
	
	var phone = $('phone');
	if (phone != null) {
		if (!text_validator('phone')){error = 1}
	}
	
	var country = $('country_id');
	if (country != null) {
		if (!text_validator('country_id')){error = 1}
	}
	

	if (!text_validator('cars_brand_id') || !text_validator('request_models[]') || !text_validator('request_years[]')) {error = 1}
	
	var accept = $('accept');
	if (accept != null) {if (!checkbox_validator('accept')){error = 1}}
	
	var paymethod = getRadioValue('is_cash');
	
	if (paymethod == 0){
		if ($F('max_inst_amount') < $F('min_inst_amount')){
			error = 1;
			alert('لا يجب أن يكون الحد الأدنى للقسط أكبر من الحد الأقصى');
		}
	} else if (paymethod == 1){
		if ($F('max_price') < $F('min_price')){
			error = 1;
			alert('لا يجب أن يكون الحد الأدنى للسعر أكبر من الحد الأقصى');
		}
	} else if (paymethod == 2){
		$('max_price').value = 0;
		$('min_price').value = 0;
		$('max_inst_amount').value = 0;
		$('min_inst_amount').value = 0;
	}

	if (error == 1) {
		show('error_tr');
		return false;
	}

	return true;
}


// company add and edit advertisment page
function check_company_form() {

	var error = 0

	var name = $('name');
	if (name != null) {
		if (!text_validator('name')){error = 1}
	}
	
	if (!text_validator('country_id') || !text_validator('cars_brand_id')) {error = 1}

	if (get_status() == "o" || get_status() == "n"){
		if (!text_validator('cars_model_id') || !text_validator('car_year'))	error = 1
	}
	
	if (error == 1) {
		show('error_tr');
		return false;
	}

	return true;
}


// agency add and edit advertisment page
function check_agency_form() {

	var error = 0

	var name = $('name');
	if (name != null) {
		if (!text_validator('name')){error = 1}
	}
	
	if (!text_validator('cars_brand_id')) {error = 1}

	if (get_status() == "o" || get_status() == "n"){
		if (!text_validator('cars_model_id') || !text_validator('car_year'))	error = 1
	}
	
	if (error == 1) {
		show('error_tr');
		return false;
	}

	return true;
}


// journal add and edit advertisment page
function check_journal_form() {

	var error = 0
	
	
	
	if (!text_validator('country_id') || !text_validator('cars_brand_id')) {error = 1}

	if (error == 1) {
		show('error_tr');
		return false;
	}

	return true;
}


function checkComment(){

	if (!text_validator('name') || !email_validator('email') || !text_validator('comment') || !text_validator('captcha')){
	
		$('error_div').innerHTML = "من فضلك أكمل البيانات المطلوبة";
		Modalbox.resizeToContent();
		return false;
	} 
	
	else {return true;}
}

function checkSold(){

	if (!text_validator('name') || !email_validator('email') || !text_validator('phone') || !text_validator('captcha')){
	
		$('error_div').innerHTML = "من فضلك أكمل البيانات المطلوبة";
		Modalbox.resizeToContent();
		return false;
	} 
	
	else {return true;}
}


<!-- ajax scripts of custom image upload -->

// update field with the result of the ajax request 

function update(area, output) {

	$(area).innerHTML = output;

}

// hide edit panel and show waiting image 

function wait(area) {

	$(area).innerHTML = $('loading_clown').innerHTML;

}



var handleResponse = function(o){

	if(o.responseText == 1)	$(o.argument.updated_area).innerHTML = $('file_inpt').innerHTML;

	else					$(o.argument.updated_area).innerHTML = o.argument.original_area;

};

var deleteImage_callback =

{

  upload:handleResponse,

  success:handleResponse,

  argument: { updated_area:"", original_area:"" }

};







var handleImgUpload = function(o){

	updatedArea = 'pics_'+o.argument.ids[o.tId];

	updateImages(updatedArea, o.responseText);

};

var showImgs_callback =

{

  upload:handleImgUpload,

  success:handleImgUpload,

  argument: { ids: new Array() }

};

//============================ images functions ========================//

function _checkSelectChanges(module) {

	if(module != 'cars_year')	$('pics_cars_model').innerHTML = '';

	$('pics_cars_year').innerHTML = '';

}

function showImages(module) {



	// for years module only

	var model_id = 0;

	model_id = $('cars_model_id').value;

	if(module == 'cars_year' && model_id == 0)		return;	// user must select a model 



	// if nothing is selected for this module , return

	var id = $(module+'_id').value;

	if(!id)		return;

	

	// function to make sure not to select a sub category with it's parent not selected

	_checkSelectChanges(module);

	

	wait('pics_'+module);	

	var cObj = YAHOO.util.Connect.asyncRequest('GET', '/'+module+'s/show_images/'+id+'/'+model_id+'?rand='+new Date().getTime(), showImgs_callback);	

	

	// associate current request id to the current model, used if there are concurrent ajax requests

	showImgs_callback.argument.ids[cObj.tId] = module;

}





function updateImages(area, output) {

	parseJS(output);

	update(area, output);

}



function getImages(module) {

	if($('system').checked == true)		showImages(module);

}

// get currency

var handleCurrencyResponse = function(c){

	if (c.responseText.blank()) return false;

	

	arr = c.responseText.split(';');	// typical c.responseText may = USD;american dollar;2	(name;description;id)

	$('currency1').innerHTML = arr[0]+'-'+arr[1];		// cash span	

	$('currency2').innerHTML = arr[0]+'-'+arr[1];		// installment span
	
	var currency3 = $('currency3');
	if (currency3 != null) {
		$('currency3').innerHTML = arr[0]+'-'+arr[1];		// rent span
	}
	
	$('currency_id').value = arr[2];				// hidden currency_id field

};



var getcurrency_callback =

{

  upload:handleCurrencyResponse,

  success:handleCurrencyResponse,

  argument: { empty:"", selVal:"" }

};



function getCurrency(country) {

	var url = '/countries/getcurrency/'+country;	
	var curObj = YAHOO.util.Connect.asyncRequest('GET', url, getcurrency_callback);

	return false;	
}


function getPhoneCode(country){
	
	var url = '/countries/getphonecode/'+country;

	new Ajax.Request(url, {
		method: 'post',
		onSuccess: function(transport) {
			$('phone_code').innerHTML = transport.responseText;
			
			var fax = $('fax_code');
			if (fax != null) {
				$('fax_code').innerHTML = transport.responseText;
			}
		}
	});
}


function getAdvertismentBySystemId(SysId){
	
}

//---------------------------------------------------------------------------------------

function add_to_wishList(id) {
	wait('wishlist_top');
	
	var url = '/ads/fav_add/'+id;
	hide('wish_add_'+id);
	
	new Ajax.Request(url, {
			method: 'post',
			onSuccess: function(transport) {
				new Ajax.Updater('wishlist_top', '/ads/refreshwl');
				show('wish_del_'+id);
			}
		});	
}


function del_wishList(id) {
	wait('wishlist_top');
	
	var url = '/ads/fav_del/'+id;
	hide('wish_del_'+id);	
	
	new Ajax.Request(url, {
			method: 'post',
			onSuccess: function(transport) {
				new Ajax.Updater('wishlist_top', '/ads/refreshwl');
				show('wish_add_'+id);
			}
		});	
}