// <![CDATA[
/******************************************************************************************
* Module: siworks.extention.js
* Version: 1.0
* Created: 20081118
* Description: We are now going to further extend the DOM via prototype.js much
* more effective way of doing things, we bind everything together this way
* @copyright		2009 SI-Works, All rights reserved
* @author			$Author: peter $
* @email			<support@siworks.co.za>
* @link				http://www.siworks.co.za
* @version			$Revision: 148 $
* $Id: siworks.entention.js 148 2009-03-19 08:36:42Z peter $
* 
* Copyright:  SI Works internet var year = new Date(); year.getFullYear();
* 
* Email:  support@siworks.co.za
* Author: Greg Shiers, Jarratt Ingram
******************************************************************************************/
/**
* These methods add custom effects through the why trade in website
**/
/**
* Validates the adding and editing of a new dealer registration
* @param ( e ) which form to process this validation through
* @return N/A
* @author Greg Shiers
* @version 1.0
*/
function submitAjax ( page , form) {
	new Ajax.Request(page, {
			method: 'post',
			postBody: $(form).serialize(),
			onLoading : function () {
				$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Please wait...</div>');
			},
			onComplete: function( transport ) {
				$("dialog_main_content").update(transport.responseText);
				setTimeout(function( ){
					if ( $('login_good') ) {
						location.href='/';
					}
				}, 1500)

			},
			onFailure : function () {
				alert('Ajax call failed.');
				return false;
			}
	});
}
function validateRegistration(){
	// Set the variables for the script
	var focus_el = null, 
	    msg = '', 
	    prefix = "There have been some errors with your input:\n";
	
	// Make sure that the first name is not empty
	if ( SI_Works_Validation.emptyString ( 'register_name' ) ) {
		msg += error[21];
		focus_el = focus_el || $('register_name');
	}
	// Make sure that the surname is not empty
	if ( SI_Works_Validation.emptyString ( 'register_surname' ) ) {
		msg += error[22];
		focus_el = focus_el || $('register_surname');
	}
	// Make sure the dealership name is not empty
	if ( SI_Works_Validation.emptyString ( 'register_dealership' ) ) {
		msg += error[23];
		focus_el = focus_el || $('register_dealership');
	}
	// Make sure that telephone field is filled in
	if ( SI_Works_Validation.emptyString( 'register_telephone' ) ) {
		msg += error[1];
		focus_el = focus_el || $('register_telephone');
	}
	// Make sure that phone number is correctly formatted
	if ( !SI_Works_Validation.emptyString( 'register_telephone' ) ) {
		if ( SI_Works_Validation.phoneNumber ( 'register_telephone' ) ) {
			msg += error[2];
			focus_el = focus_el || $('register_telephone');
		}
	}
	if ( SI_Works_Validation.emptyString( 'register_email' ) ) {
		msg += error[3];
		focus_el = focus_el || $('register_email');
	}

	if ( !SI_Works_Validation.emptyString ( 'register_email' ) ) {
		// Make sure that the email address is valid
		if ( !SI_Works_Validation.isEmailAddress ( 'register_email' ) ) {
			msg += error[4];
			focus_el = focus_el || $('register_email');
		}
	}
	// Make sure that the dealers address is filled in
	if ( SI_Works_Validation.emptyString('register_address' ) ) {
		msg += error[24];
		focus_el = focus_el || $('register_address');
	}
	if ( msg != '' ) {
		if ( $('new_error') ) {
			$('new_error').update(msg);
			$('new_error').scrollTo();
		}
		else {
			$('register_form').insert({before: '<div class="error" id="new_error">'+msg+'</div>'});
			$('new_error').scrollTo();
		}
		if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	else { 
		if ($('new_error')) {
			$('new_error').remove();
		}
		new Ajax.Request('/register/ajax/', {
				method: 'post',
				postBody: $('register_form').serialize(),
				onLoading : function () {
					$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Sending information...</div>');
				},
				onComplete: function( transport ) {
					$("dialog_main_content").update(transport.responseText);
					setTimeout(function(){
						Element.removeCoverSheet();
					},3000)
				},
				onFailure : function () {
					alert('Ajax call failed.');
					return false;
				}
		});
		$('dialog_main_content').update('Sending information.... Ajax call is performed here...not completed yet');
		return false; // If there are no errors, go ahead and send the form through.
	}
}



/**
* Validates the adding of the dealer via registration
* @param 
* @return N/A
* @author Greg Shiers
* @version 1.0
*/
function validateContact(){
// Set the variables for the script
	var focus_el = null, 
	    msg = '';
	
	if ( SI_Works_Validation.emptyString( 'contact_name' ) ) {
		msg += error[0];
		focus_el = focus_el || $('contact_name');
	}
	// Make sure that the telephone number is filled in

	if ( SI_Works_Validation.emptyString ('contact_phone' ) ) {
		msg += error[1];
		focus_el = focus_el || $('contact_phone');
	}
	// Make sure that phone number is correctly formatted
	if ( !SI_Works_Validation.emptyString( 'contact_phone' ) ) {
		if ( SI_Works_Validation.phoneNumber ( 'contact_phone' ) ) {
			msg += error[2];
			focus_el = focus_el || $('contact_phone');
		}
	}
	// Make sure that the email address is filled in
	if ( SI_Works_Validation.emptyString ( 'contact_emaill' ) ) {
		msg += error[3];
		focus_el = focus_el || $('contact_emaill');
	}
	// If the email address is filled in
	if ( !SI_Works_Validation.emptyString( 'contact_emaill' ) ) {
		// Make sure that the email address is valid
		if ( !SI_Works_Validation.isEmailAddress( 'contact_emaill' ) ) {
			msg += error[4];
			focus_el = focus_el || $('contact_emaill');
		}
	}
	if ( SI_Works_Validation.emptyString( 'contact_message' ) ) {
		msg += error[5];
		focus_el = focus_el || $('contact_message');
	}
	if ( msg != '' ) {
		if ( $('new_error') ) {
			$('new_error').update(msg);
			$('new_error').scrollTo();
		}
		else {
			$('fin_request_form').insert({before: '<div class="error" id="new_error">'+msg+'</div>'});
			$('new_error').scrollTo();
		}
		if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	else { 
		if ($('new_error')) {
			$('new_error').remove();
		}
		new Ajax.Request('/contact/ajax/', {
				method: 'post',
				postBody: $('contact_form').serialize(),
				onLoading : function () {
					$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Sending information...</div>');
				},
				onComplete: function( transport ) {
					$("dialog_main_content").update(transport.responseText);
					setTimeout(function(){
						Element.removeCoverSheet();
					},5000);
				},
				onFailure : function () {
					alert('Ajax call failed.');
					return false;
				}
			});
		//$('dialog_main_content').update('Sending information.... Ajax call is performed here...not completed yet');
		return false; // If there are no errors, go ahead and send the form through.
	}
}
function validateDealerSearch() {

		new Ajax.Request('/search/ajax/', {
				method: 'post',
				postBody: $('dealer_search_form').serialize(),
				onLoading : function () {
					$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Searching vehicles...</div>');
				},
				onComplete: function( transport ) {
					$("dialog_main_content").update(transport.responseText);
				},
				onFailure : function () {
					alert('Ajax call failed.');
					return false;
				}
			});
		//$('dialog_main_content').update('Sending information.... Ajax call is performed here...not completed yet');
		return false; // If there are no errors, go ahead and send the form through.
}
/**
* Validates the private finance form
* @param 
* @return N/A
* @author Greg Shiers
* @version 1.0
*/
function validateMakeOffer(page){
	// Set the variables for the script
	var focus_el = null, 
	    msg = '';
	
	if ( SI_Works_Validation.emptyString( 'offer_amount_from' ) ) {
		msg += error[51];
		focus_el = focus_el || $('offer_amount_from');
	}
	// Make sure that the telephone number is filled in

	if ( SI_Works_Validation.emptyString ('offer_amount_to' ) ) {
		msg += error[52];
		focus_el = focus_el || $('offer_amount_to');
	}
	// Check that the input of the offer prices are numbers
	if ( !SI_Works_Validation.emptyString ( 'offer_amount_from' ) ) {
		if ( SI_Works_Validation.numericWithSpaces ( 'offer_amount_from' ) ) {
			msg += error[53];
			focus_el = focus_el || $('offer_amount_from');
		}
	}
	// Check that the input of the offer prices are numbers
	if ( !SI_Works_Validation.emptyString ( 'offer_amount_to' ) ) {
		if ( SI_Works_Validation.numericWithSpaces ( 'offer_amount_to' ) ) {
			msg += error[54];
			focus_el = focus_el || $('offer_amount_to');
		}
	}

	
	// Check if the to value is more than the from value
	if ( parseInt($('offer_amount_to').value) < parseInt($('offer_amount_from').value) ) {
		msg += error[55];
		focus_el = focus_el || $('offer_amount_to');
	}
	// Make sure that notes field is filled in
/*	if ( SI_Works_Validation.emptyString ( 'offer_notes' ) ) {
		msg += error[56];
		focus_el = focus_el || $('offer_notes');
	}*/


	if ( msg != '' ) {
		if ( $('new_error') ) {
			$('new_error').update(msg);
			$('new_error').scrollTo();
		}
		else {
			$('dealer_offer_form').insert({before: '<div class="error" id="new_error">'+msg+'</div>'});
			$('new_error').scrollTo();
		}
		if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	else { 
		if ($('new_error')) {
			$('new_error').remove();
		}
		new Ajax.Request(page, {
				method: 'post',
				postBody: $('dealer_offer_form').serialize(),
				onLoading : function () {
					$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Making offer, please wait...</div>');
				},
				onComplete: function( transport ) {
					$("dialog_main_content").update(transport.responseText);
					$('dealer_offer_form').scrollTo();
					setTimeout(function(){
						Element.removeCoverSheet();
					},5000);
				},
				onFailure : function () {
					alert('Ajax call failed.');
					return false;
				}
			});
		return false; // If there are no errors, go ahead and send the form through.
	}
}

/**
* Validates the private finance form
* @param 
* @return N/A
* @author Greg Shiers
* @version 1.0
*/
function validatePrivateFinance(){
	// Set the variables for the script
	var focus_el = null, 
	    msg = '';
	
	if ( SI_Works_Validation.emptyString( 'name' ) ) {
		msg += error[61];
		focus_el = focus_el || $('name');
	}
	// Make sure that the telephone number is filled in

	if ( SI_Works_Validation.emptyString ('phone' ) ) {
		msg += error[62];
		focus_el = focus_el || $('phone');
	}
	// Make sure that phone number is correctly formatted
	if ( !SI_Works_Validation.emptyString( 'phone' ) ) {
		if ( SI_Works_Validation.phoneNumber ( 'phone' ) ) {
			msg += error[63];
			focus_el = focus_el || $('phone');
		}
	}
	// Make sure that the email address is filled in
	if ( SI_Works_Validation.emptyString ( 'email' ) ) {
		msg += error[64];
		focus_el = focus_el || $('email');
	}
	// If the email address is filled in
	if ( !SI_Works_Validation.emptyString( 'email' ) ) {
		// Make sure that the email address is valid
		if ( !SI_Works_Validation.isEmailAddress( 'email' ) ) {
			msg += error[65];
			focus_el = focus_el || $('email');
		}
	}
	
	if ( msg != '' ) {
		if ( $('new_error') ) {
			$('new_error').update(msg);
			$('new_error').scrollTo();
		}
		else {
			$('fin_request_form').insert({before: '<div class="error" id="new_error">'+msg+'</div>'});
			$('new_error').scrollTo();
		}
		if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	else { 
		if ($('new_error')) {
			$('new_error').remove();
		}
		new Ajax.Request('/private_vehicle_finance/ajax/', {
				method: 'post',
				postBody: $('fin_request_form').serialize(),
				onLoading : function () {
					$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Sending information...</div>');
				},
				onComplete: function( transport ) {
					$("dialog_main_content").update(transport.responseText);
					setTimeout(function(){
						Element.removeCoverSheet();
					},5000);
				},
				onFailure : function () {
					alert('Ajax call failed.');
					return false;
				}
			});
		//$('dialog_main_content').update('Sending information.... Ajax call is performed here...not completed yet');
		return false; // If there are no errors, go ahead and send the form through.
	}
}

function validateListVehicle () {
	// Set the variables for the script
	var focus_el = null, 
	    msg = '';
	// vehiclebrand selectbox
	if ( SI_Works_Validation.selected ( 'vehiclebrand' ) ) {
		msg += error[34];
		focus_el = focus_el || $('vehiclebrand');
	}
	// vehiclemodel input box
	if ( SI_Works_Validation.emptyString ( 'vehiclemodel' ) ) {
		msg += error[35];
		focus_el = focus_el || $('vehiclemodel');
	}
	// vehicleyear select box
	if ( SI_Works_Validation.selected ( 'vehicleyear' ) ) {
		msg += error[36];
		focus_el = focus_el || $('vehicleyear');
	}
	
	
	// vehiclemileage input box
	if ( SI_Works_Validation.emptyString ( 'vehiclemileage' ) ) {
		msg += error[38];
		focus_el = focus_el || $('vehiclemileage');
	}
	
	
	
	// vehiclecolour input box
	if ( SI_Works_Validation.emptyString ( 'vehiclecolour' ) ) {
		msg += error[40];
		focus_el = focus_el || $('vehiclecolour');
	}
	
	// vehiclecolour input box
	if ( SI_Works_Validation.radio ( 'hasaccident' ) ) {
		msg += error[43];
	}
	// vehiclecolour input box
	if ( SI_Works_Validation.radio ( 'hassettlement' ) ) {
		msg += error[43];
	}
	
	
	// ownername input box
	if ( SI_Works_Validation.emptyString ( 'ownername' ) ) {
		msg += error[21];
		focus_el = focus_el || $('ownername');
	}
	// ownersurname input box
	if ( SI_Works_Validation.emptyString ( 'ownersurname' ) ) {
		msg += error[22];
		focus_el = focus_el || $('ownersurname');
	}
	// ownertelephone input box
	if ( SI_Works_Validation.emptyString( 'ownertelephone' ) ) {
		msg += error[1];
		focus_el = focus_el || $('ownertelephone');
	}
	// ownertelephone input box
	if ( !SI_Works_Validation.emptyString( 'ownertelephone' ) ) {
		// ownertelephone check if number
		if ( SI_Works_Validation.phoneNumber ( 'ownertelephone' ) ) {
			msg += error[2];
			focus_el = focus_el || $('ownertelephone');
		}
	}
	// owneremail input box
	if ( SI_Works_Validation.emptyString( 'owneremail' ) ) {
		msg += error[3];
		focus_el = focus_el || $('owneremail');
	}

	if ( !SI_Works_Validation.emptyString ( 'owneremail' ) ) {
		// owneremail check if email
		if ( !SI_Works_Validation.isEmailAddress ( 'owneremail' ) ) {
			msg += error[4];
			focus_el = focus_el || $('owneremail');
		}
	}
	// vehiclebrand selectbox
	if ( SI_Works_Validation.selected ( 'ownerregion' ) ) {
		msg += error[32];
		focus_el = focus_el || $('ownerregion');
	}
	// owneraddress input box
	if ( SI_Works_Validation.emptyString( 'owneraddress' ) ) {
		msg += error[33];
		focus_el = focus_el || $('owneraddress');
	}
	
	
	if ( msg != '' ) {
		if ( $('new_error') ) {
			$('new_error').update(msg);
			$('new_error').scrollTo()
		}
		else {
			$('add_vehicle_form').insert({before: '<div class="error" id="new_error">'+msg+'</div>'});
			$('new_error').scrollTo()
		}
		if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	else { 
		if ($('new_error')) {
			$('new_error').remove();
		}
		//$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Sending information...</div>');
		//alert($('ajaxFrame').contentWindow.document.body.innerHTML);
		//$("dialog_main_content").update($('ajaxFrame').contentWindow.document.body.document.getElementById('holder').innerHTML);
		//	setTimeout(function(){
		//		Element.removeCoverSheet();
		//	},10000);
		//return false; // If there are no errors, go ahead and send the form through.
	}
}

/**
* Validates the private finance form
* @param 
* @return N/A
* @author Greg Shiers
* @version 1.0
*/
function validateAccount(){
	// Set the variables for the script
	var focus_el = null, 
	    msg = '';
	
	if ( SI_Works_Validation.emptyString( 'settings_name' ) ) {
		msg += error[71];
		focus_el = focus_el || $('settings_name');
	}
	if ( SI_Works_Validation.emptyString( 'settings_surname' ) ) {
		msg += error[72];
		focus_el = focus_el || $('settings_surname');
	}
	if ( SI_Works_Validation.emptyString( 'settings_dealership' ) ) {
		msg += error[73];
		focus_el = focus_el || $('settings_dealership');
	}
	
	// Make sure that the telephone number is filled in

	if ( SI_Works_Validation.emptyString ('settings_telephone' ) ) {
		msg += error[74];
		focus_el = focus_el || $('settings_telephone');
	}
	// Make sure that phone number is correctly formatted
//	if ( !SI_Works_Validation.emptyString( 'settings_telephone' ) ) {
//		if ( !SI_Works_Validation.phoneNumber ( 'settings_telephone' ) ) {
//			msg += error[75];
//			focus_el = focus_el || $('settings_telephone');
//		}
//	}
	// Make sure that the email address is filled in
	if ( SI_Works_Validation.emptyString ( 'settings_address' ) ) {
		msg += error[76];
		focus_el = focus_el || $('settings_address');
	}
	
	if ( msg != '' ) {
		if ( $('new_error') ) {
			$('new_error').update(msg);
			$('new_error').scrollTo();
		}
		else {
			$('settings_form').insert({before: '<div class="error" id="new_error">'+msg+'</div>'});
			$('new_error').scrollTo();
		}
		if ( focus_el.focus ) { focus_el.focus(); }
		return false; // If there are errors, dont submit the form.
	}
	else { 
		if ($('new_error')) {
			$('new_error').remove();
		}
		new Ajax.Request('/settings/ajax/', {
				method: 'post',
				postBody: $('settings_form').serialize(),
				onLoading : function () {
					$("dialog_main_content").update('<div class="pr10 pl10 pt10 pb10"><img src="/images/icons/progress.gif" class="icor" />Sending information...</div>');
				},
				onComplete: function( transport ) {
					$("dialog_main_content").update(transport.responseText);
					
				},
				onFailure : function () {
					alert('Ajax call failed.');
					return false;
				}
			});
		//$('dialog_main_content').update('Sending information.... Ajax call is performed here...not completed yet');
		return false; // If there are no errors, go ahead and send the form through.
	}
}

// ]]>

