/*
   	------------------------------------------------------
   	FILE:	validation_reg.js
   	------------------------------------------------------
   	AUTHOR:	Cohesion/Brendon Ryniker
   	EMAIL:	brendon@actrix.co.nz
   	DATE:	Wed Apr 30 15:35:23 2008 
   	------------------------------------------------------
   	APPLICATION: newzealandluxury.com
   	------------------------------------------------------
   	DEPENDANCIES:
   	------------------------------------------------------
   	FUNCTIONALITY:
	
	Main Form Validation Functions	
	
   	------------------------------------------------------
*/

var Errors = new Array();

// ==============================
	function validate() {	
// ==============================

		Errors = new Array();
		
		validate_email("email", "Your valid email is required.");			
		if (validate_required("password","A password of 6-8 characters is required.")) {
			validate_length("password",6,8,"Your password must be 6-8 Characters long.");
		}
		validate_required("name","Your name is required.");
		validate_required("company_name","Your company name is required.");
		validate_required("address","Your address is required.");
		validate_required("city","Your city is required.");
		validate_required("country","Your country is required.");
		validate_required("phone","Your phone number is required.");
		validate_radio(document.reg_form.trade_media,"You must indicate whether you require a Trade or Media login");
		
		if(document.getElementById('trade_login').checked){
			
			validate_radio(document.reg_form.nz_wholesaler,
						   "You must indicate whether you aare a New Zealand wholesaler or inbound operator");
			
			if(document.getElementById('not_nz_wholesaler') && document.getElementById('not_nz_wholesaler').checked){
				
				validate_required("IATA","Your IATA number is required.");
			}
		}

		if(document.getElementById('media_login') && document.getElementById('media_login').checked){
			
			validate_required("website","Your website URL is required.");
			
			var el = document.getElementById('media_org');
			
			if (el.selectedIndex == -1 || !el.selectedIndex){

				Errors[Errors.length] = "Your Media Organisation is required.";
			}
		}
		if (Errors.length) {

			var pl  = Errors.length == 1 ? '' : 's';
			var err = 'Your form has the following error' + pl + '; please try again...' + "\n\n- " + Errors.join("\n- ");
			alert(err);
			return false;
		}
		
		return true;
	}

// ==============================
	function validate_select(id,message) {
// ==============================
	
        if(document.getElementById(id).options[document.getElementById(id).selectedIndex].value == ""){
			
			if(message){
					
				Errors[Errors.length] = message;
			}
			return false;
		}
		else{
			return true;
        }
	}

// ==============================
	function validate_required(id,message) {
// ==============================

		// Text exists validation

		var el = document.getElementById(id);
        if(Trim(el.value) == ""){			
			if(message){				
				Errors[Errors.length] = message;
			}
			return false;
		}
		else{
			return true;
        }
	}

// ==============================
	function validate_length(id,min_len,max_len,message) {
// ==============================
		
		document.getElementById(id).value = Trim(document.getElementById(id).value);
		var val = document.getElementById(id).value;
        if((val.length > max_len) || (val.length < min_len)){
			
			if(message){
					
				Errors[Errors.length] = message;
			}
			return false;
		}
		else{
			return true;
        }
	}

// ==============================
	function validate_email(id,message){
// ==============================

		document.getElementById(id).value = Trim(document.getElementById(id).value);
		if(!echeck(document.getElementById(id).value)){
			
			if(message){
				
				Errors[Errors.length] = message;
			}
			return false;
		}
		else{
			return true;
		}
	}

// ==============================
	function Trim(TRIM_VALUE){
// ==============================

		// String Trim Function
		// Removes spaces from the start and the end of the string
	
        if(TRIM_VALUE.length < 1){
			
			return"";
        }
        TRIM_VALUE = RTrim(TRIM_VALUE);
        TRIM_VALUE = LTrim(TRIM_VALUE);
		
        if(TRIM_VALUE==""){
			
			return "";
        }
		else{
			return TRIM_VALUE;
        }
	} 

// ==============================
	function RTrim(VALUE){
// ==============================
		
        var w_space = String.fromCharCode(32);
        var v_length = VALUE.length;
        var strTemp = "";
		if(v_length < 0){
			
			return "";
        }
        var iTemp = v_length -1;
        while(iTemp > -1){
			
			if(VALUE.charAt(iTemp) == w_space){
				
			}
			else{
				strTemp = VALUE.substring(0,iTemp +1);
				break;
			}
			iTemp = iTemp-1;
        } 
        return strTemp;

	}

// ==============================
	function LTrim(VALUE){
// ==============================
	
        var w_space = String.fromCharCode(32);
        if(v_length < 1){
			
			return "";
        }
        var v_length = VALUE.length;
        var strTemp  = '';
        var iTemp    = 0;

        while(iTemp < v_length){
			
			if(VALUE.charAt(iTemp) == w_space){
				
			}
			else{
				strTemp = VALUE.substring(iTemp,v_length);
				break;
			}
			iTemp = iTemp + 1;
        } 
        return strTemp;
	}


// ==============================
	function echeck(str) {
// ==============================

		// Email Syntax Checking Function
	
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			
			return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			
			return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			
			return false;
		}

		if (str.indexOf(at,(lat+1))!=-1){
			
			return false;
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			
	   return false;
		}

		if (str.indexOf(dot,(lat+2))==-1){
			
			return false;
		}

		if (str.indexOf(" ")!=-1){
			
			return false;
		}

		return true;
	}

// ==============================
	function validate_radio(radio_obj,message){
// ==============================
	

		myOption = -1;
		for (i=radio_obj.length - 1; i > -1; i--) {
			
			if (radio_obj[i].checked) {
				
				myOption = i;
			}
		}
		if (myOption == -1) {
			
			Errors[Errors.length] = message;
			return false;
		}
		return true;
	}

// ==============================
	function show_media(){
// ==============================
	
		for (i=document.getElementsByClassName('trade_row').length - 1; i > -1; i--) {
			document.getElementsByClassName('trade_row')[i].style.display='none';
		}

		for (i=document.getElementsByClassName('media_row').length - 1; i > -1; i--) {
			document.getElementsByClassName('media_row')[i].style.display='';
		}
		document.getElementById('IATA_row').style.display='none';
	}

// ==============================
	function show_trade(){
// ==============================
	
		for (i=document.getElementsByClassName('media_row').length - 1; i > -1; i--) {
			document.getElementsByClassName('media_row')[i].style.display='none';
		}

		for (i=document.getElementsByClassName('trade_row').length - 1; i > -1; i--) {
			document.getElementsByClassName('trade_row')[i].style.display='';
		}
		if(document.getElementById('nz_wholesaler_no').checked){
			document.getElementById('IATA_row').style.display='';
		}
	}


