// JavaScript Document

function alertUser(field){
	
   window.alert(field+" is required and should not be empty");
}

function alertUserNumber(fieldNumber){
	window.alert("Please Enter a Number for "+fieldNumber+" Field");
}

function emailEval(thisEmail){
	
	var badChars = " /,:;";
	
	for(var k = 0;k < badChars.length; k++){
		if(thisEmail.indexOf(badChars.charAt(k)) > -1){
            return true;
		}
	}
	
	var atPos = thisEmail.indexOf("@",1);
	if(atPos == -1){
		return true;
	}
	if(thisEmail.indexOf("@",atPos+1) > -1){
		return true;
	}
	
	var dotPos = thisEmail.indexOf(".",atPos);
	if(dotPos == -1){
		return true;
	}
	if(dotPos+3 > thisEmail.length){
		return true;
	}
	
	return false;
	
}





//== Filter required fields on registration page ==//
        //==== Finished ====//
function registration(){
	
	var allFormFields = document.forms['registerForm'].elements; //gets all the form elements in the parent form
	var allFrameFields = window.registerFormFrame.document.forms[0].elements; // gets all the form elements iframe form
	var formFieldsCounter, formFieldError= new Array(), formFieldErrorCounter=0, otherError=0;
	var formFrameCounter,mutilSelectArray = new Array(), mutilSelectArrayCounter;
	
	for(formFieldsCounter=0; formFieldsCounter < allFormFields.length; formFieldsCounter++){ // loops through all the form elements in the parent form; begin loop
		
		if(formFieldsCounter == 11 || formFieldsCounter == 12){ // checks if the current form element are the two required "select" field tag
			
			if(allFormFields[11].selectedIndex==0){ // checks the selected index of the current "select" tag; begin if
				
				formFieldError[formFieldErrorCounter] = "Property Type"; formFieldErrorCounter++;
					
			}// end if
			
			if(allFormFields[12].selectedIndex == -1){
				
				formFieldError[formFieldErrorCounter] = "Preffered Location"; formFieldErrorCounter++;
				
			}
			
/*end if*/ }else{// begin else
			
			if(allFormFields[formFieldsCounter].value==""){// checks the value of the other required fields
				
				switch(formFieldsCounter){ //assigns a code to be executed for the required fields; begin switch
					
					case 0:formFieldError[formFieldErrorCounter] = "First Name"; formFieldErrorCounter++; break;
					
					case 1:formFieldError[formFieldErrorCounter] = "Last Name"; formFieldErrorCounter++; break;
					
					case 2:formFieldError[formFieldErrorCounter] = "Email"; formFieldErrorCounter++; break;
					
					case 3:formFieldError[formFieldErrorCounter] = "Confirm Email"; formFieldErrorCounter++; break;
					
					case 4:formFieldError[formFieldErrorCounter] = "Password"; formFieldErrorCounter++; break;
					
					case 5:formFieldError[formFieldErrorCounter] = "Confirm Password"; formFieldErrorCounter++; break;
					
					case 6:formFieldError[formFieldErrorCounter] = "Telephone"; formFieldErrorCounter++; break;
					
					case 7:formFieldError[formFieldErrorCounter] = "Street"; formFieldErrorCounter++; break;
					
					case 8:formFieldError[formFieldErrorCounter] = "City"; formFieldErrorCounter++; break;
					
					case 9:formFieldError[formFieldErrorCounter] = "ZIP"; formFieldErrorCounter++; break;
					
					case 10:formFieldError[formFieldErrorCounter] = "Country"; formFieldErrorCounter++; break;
					
					case 13:formFieldError[formFieldErrorCounter] = "Bedrooms"; formFieldErrorCounter++; break;
					
					case 14:formFieldError[formFieldErrorCounter] = "Bathrooms"; formFieldErrorCounter++; break;

				}// end switch
				
			} // end if
			
		}// end else
		
	}// end loop
	
	if(allFormFields[2].value != ""){// checks the content of email field; begin if
		
		if(emailEval(allFormFields[2].value)){ // evaluates the content of the email fields through a function
			
			formFieldError[formFieldErrorCounter]="Email"; formFieldErrorCounter++;
			
		}// end if
		
	}//end if
	
	if(formFieldError.length > 0){
		
		viewError(formFieldError.join(", "),"reg_minibanner");
		
	}else{
		
		if(allFormFields[2].value != allFormFields[3].value){
			
			window.scroll(1000,0);
			document.getElementById('reg_minibanner').innerHTML="<h3>FORM ERROR......</h3><p>Email and Confirm Email fields are not equal.</p>";
			otherError++;
			
		}else{
			
			if(allFormFields[4].value != allFormFields[5].value){
				window.scroll(1000,0);
				document.getElementById('reg_minibanner').innerHTML="<h3>FORM ERROR......</h3><p>Password and Confirm Password fields are not equal.</p>";
				otherError++;
			}
			
		}
		
		
		if(otherError == 0){
			
			for(formFrameCounter=0; formFrameCounter < allFrameFields.length - 1; formFrameCounter++){
				
				if(allFormFields[formFrameCounter].tagName=="SELECT" && formFrameCounter != 12){
					
					allFrameFields[formFrameCounter].value = allFormFields[formFrameCounter].options[allFormFields[formFrameCounter].selectedIndex].value;
					
				}else if(allFormFields[formFrameCounter].tagName=="SELECT" && formFrameCounter == 12){
					
					for(mutilSelectArrayCounter = 0; mutilSelectArrayCounter < allFormFields[formFrameCounter].options.length; mutilSelectArrayCounter++){
						
						if(allFormFields[formFrameCounter].options[mutilSelectArrayCounter].selected == true){
							mutilSelectArray.push(allFormFields[formFrameCounter].options[mutilSelectArrayCounter].value);
						}
						
					}
					
					allFrameFields[formFrameCounter].value = mutilSelectArray.join(",");
					allFormFields['26'].value = mutilSelectArray.join(";");
					
					
				}else{
					
					allFrameFields[formFrameCounter].value = allFormFields[formFrameCounter].value;
					
				}
			}
			window.registerFormFrame.document.forms[0].elements[22].value='submit';
			window.registerFormFrame.document.forms[0].submit();
			
			
		}

	}

	

			window.registerFormFrame.document.forms[0].elements[22].value='submit';
			window.registerFormFrame.document.forms[0].submit();

}

//=================================================//


//===== Filter reqired fields on contact us page ==//
        //==== Finished ====//
function contactUs(){
	
	var allFieldsContactUs = document.formContactUs.elements;
	var allFieldsFrame = window.contactUsFrame.document.forms[0].elements;
	var ContactUsCounter, frameFormCounter,ErrorCounter=0;
	var ContactUsErrors = new Array(), multiArrayCounter, multiSelectArray = new Array();
	
		for(ContactUsCounter = 0; ContactUsCounter < allFieldsContactUs.length; ContactUsCounter++){
			
			if(ContactUsCounter != 11){
				
				if(allFieldsContactUs[ContactUsCounter].value == ""){
					
					switch(ContactUsCounter+1){
						
						case 1:ContactUsErrors[ErrorCounter]="First Name"; ErrorCounter++; break;
						case 2:ContactUsErrors[ErrorCounter]="Last Name"; ErrorCounter++; break;
						case 3:ContactUsErrors[ErrorCounter]="Telephone Number"; ErrorCounter++; break;
						case 4:ContactUsErrors[ErrorCounter]="Email"; ErrorCounter++; break;
						case 10:ContactUsErrors[ErrorCounter]="Price Indication"; ErrorCounter++;break;
						case 13:ContactUsErrors[ErrorCounter]="No Bedrooms"; ErrorCounter++; break;
						case 14:ContactUsErrors[ErrorCounter]="No of Bathrooms"; ErrorCounter++; break;
						case 15:ContactUsErrors[ErrorCounter]="Comment"; ErrorCounter++; break;
					}
					
				}
				
			}else{
				
				if(allFieldsContactUs[ContactUsCounter].selectedIndex == -1){
					
					ContactUsErrors[ErrorCounter]="Preffered Location"; ErrorCounter++;
					
				}
				
			}
			
		}
		
		
		if(allFieldsContactUs[3].value != ""){
			 if(emailEval(allFieldsContactUs[3].value)){
					   ContactUsErrors[ErrorCounter]="Email";
			 }
		}
		
		
		if(ContactUsErrors.length > 0){
			
			viewError(ContactUsErrors.join(", "),"reg_minibanner");
			
		}else{
			
			for(frameFormCounter=0; frameFormCounter < allFieldsFrame.length - 1; frameFormCounter++){
				switch(frameFormCounter){
					case 11:  for(multiArrayCounter = 0; multiArrayCounter < allFieldsContactUs[11].options.length; multiArrayCounter++){
								if(allFieldsContactUs[11].options[multiArrayCounter].selected == true){
									multiSelectArray.push(allFieldsContactUs[11].options[multiArrayCounter].value);
								}
							 }
							
							allFieldsFrame[frameFormCounter].value = multiSelectArray.join(',');
							allFieldsContactUs[19].value = multiSelectArray.join(';');
							
							break;
					default: allFieldsFrame[frameFormCounter].value = allFieldsContactUs[frameFormCounter].value;
				}
			}
			
			window.contactUsFrame.document.forms[0].submit();
			
		}
		
}

//=================================================//

//===== Filter reqired fields on Inquiry page ==//
         //==== Finished ====//
function propertIquiry(){
	
	var allFields = document.formPropertyEnquiry.elements;
	var frameAllFields = window.propertyEnquiry.document.forms[0].elements;
	var inquiryErrorCounter = 0;
	var i,j,inquiryError = new Array();
	

	for(i =0; i<allFields.length; i++){
		
		if(allFields[i].value == ""){
			
			switch(i+1){
				case 1:inquiryError[inquiryErrorCounter]="First Name"; inquiryErrorCounter++; break;
				case 2:inquiryError[inquiryErrorCounter]="Last Name"; inquiryErrorCounter++; break;
				case 3:inquiryError[inquiryErrorCounter]="Telephone Number"; inquiryErrorCounter++; break;
				case 4:inquiryError[inquiryErrorCounter]="Email Address"; inquiryErrorCounter++; break;
				case 9:inquiryError[inquiryErrorCounter]="Comment"; inquiryErrorCounter++; break;
			}
			
		}
		
	}
	
	if(allFields[3].value != ""){
		
		if(emailEval(allFields[3].value)){
			
			inquiryError[inquiryErrorCounter]="Email Address";
			
		}
		
	}
	
	if(inquiryError.length > 0){
		
		viewError(inquiryError.join(", "),"reg_minibanner");
		
	}else{
		
		for(j=0;j < frameAllFields.length-1; j++){
			
			if(j < 15){
				
				switch(j){
					case 7: frameAllFields[j].value = allFields[7].options[allFields[7].selectedIndex].value; break;
					default: frameAllFields[j].value = allFields[j].value;
				}
				
			}else{
				
				continue;
				
			}
			
		}
		
		window.propertyEnquiry.document.forms[0].submit();
		
	}
}
  
//=================================================//

//= Filter reqired fields on Property Upload page =//
        //==== Finished ====//
function propertyUpload(){
	
	var allFormsFields = document.forms['uploadPropertyForm'].elements;
	var errorCounter=0, collectError = new Array();
	var uploadCounter,frameFieldCounter;
	var allFormFrame = window.salesforceData.document.forms[0].elements, allFormFrameCounter;
	
	for(uploadCounter=0; uploadCounter < allFormsFields.length; uploadCounter++){
		
		if(uploadCounter == 2 || uploadCounter == 6 || uploadCounter == 19){
			
			if(allFormsFields[uploadCounter].selectedIndex==0){
				
				switch(uploadCounter){
					
					case 2: collectError[errorCounter] = "Property Location"; errorCounter++; break;
					case 19: collectError[errorCounter] = "Country"; errorCounter++; break;
					case 6: collectError[errorCounter] = "Type"; errorCounter++; break;
					
				}
				
			}
			
		}else{
			
			if(allFormsFields[uploadCounter].value == ""){
				
				switch(uploadCounter){
					
					case 0: collectError[errorCounter] = "Property Name"; errorCounter++; break;
					case 1: collectError[errorCounter] = "Summary"; errorCounter++; break;
					case 3: collectError[errorCounter] = "No of Reception"; errorCounter++; break;
					case 4: collectError[errorCounter] = "No of Bedrooms"; errorCounter++; break;
					case 5: collectError[errorCounter] = "No of Bathrooms"; errorCounter++; break;
					case 15: collectError[errorCounter] = "Email"; errorCounter++; break;
					case 16: collectError[errorCounter] = "Street"; errorCounter++; break;
					case 17: collectError[errorCounter] = "City"; errorCounter++; break;
					case 18: collectError[errorCounter] = "ZIP"; errorCounter++; break;
					
				}
				
			}
			
		}
		
	}
	
	if(allFormsFields[15].value != ""){
		
		if(emailEval(allFormsFields[15].value)){
			
			collectError[errorCounter] = "Email"; 
			errorCounter++;
			
		}
		
	}
	
	if(collectError.length > 0){
		
		viewError(collectError.join(", "),"upload_minibanner");
		
	}else{
		
	
	
		for(allFormFrameCounter = 0; allFormFrameCounter < allFormFrame.length - 5; allFormFrameCounter++){
			
			
			if(allFormsFields[allFormFrameCounter].tagName == "SELECT"){
				allFormFrame[allFormFrameCounter].value = allFormsFields[allFormFrameCounter].options[allFormsFields[allFormFrameCounter].selectedIndex].value;
			}else{
				allFormFrame[allFormFrameCounter].value = allFormsFields[allFormFrameCounter].value;
			}
			
			
		} 
		
		
		document.forms['uploadPropertyForm'].target='propertyUploadFrame';
		document.forms['uploadPropertyForm'].submit();
		
		
		
		
	}
	
}

//=================================================//


function viewError(fields,banner_id){
	var parentDiv = document.getElementById(banner_id);
		
	   window.scroll(1000,0);
	   parentDiv.innerHTML = "<h3>FORM ERROR......</h3><p>Please make sure that <font color=Red><b>"+fields+"</b></font>&nbsp;fields have been filled out correctly.<br /><br /></p>";
}


function mutiSelect(currentSelect){
			var previousValues = document.getElementById('pref_loc').value, newValue, newValueArray = new Array(), newValueArrayCounter;
			var previousValueArray = new Array(), previousValueArrayCounter;
			var repetitionCount = 0, higlighted = new Array(), higlightedCounter;
			var splitArray1 = new Array(),splitArray2 = new Array(), allSplitArray = new Array(), splitArrayNumber; 
			var selectedCounter;
			
			if(previousValues != ""){// Check if there are previous values
				
				previousValueArray = previousValues.split(","); // converts the values to an array 
				
				for(previousValueArrayCounter = 0; previousValueArrayCounter < previousValueArray.length; previousValueArrayCounter++){ //loops through the previously converted array
					
					if(previousValueArray[previousValueArrayCounter] == currentSelect.selectedIndex){ // check if the user click the same highlighted selection
						splitArrayNumber = previousValueArrayCounter; // gets the present count of the loop when the condition became true
						repetitionCount++; // increment this variable if condition is true
						break;
					}
					
				}
				
				
				if(repetitionCount == 0){ // if the user has clicked a different selection
					newValue = previousValues+","+currentSelect.selectedIndex; // concatinates the selected value to the previous value
					
				}else{ // if the user has clicked the same highlighted selection
					if(currentSelect.selectedIndex > 0){
						currentSelect.options[currentSelect.selectedIndex].selected = false; //remove higlight from that selection
					}
					
					if(splitArrayNumber == 0){// checks the location of the clicked highlighted selection in the array
						allSplitArray = previousValueArray.slice(1); //
						newValue = allSplitArray.join(",");          //
					}else{
						splitArray1 = previousValueArray.slice(0,splitArrayNumber);
						splitArray2 = previousValueArray.slice(splitArrayNumber + 1,previousValueArray.length);
						allSplitArray = splitArray1.concat(splitArray2); // joins all the arrays
						newValue = allSplitArray.join(","); // converts the array to string
					}
					
					
				}
				
				document.getElementById('pref_loc').value = newValue; // put the string to the hidden input
				
				higlighted = newValue.split(",");
				
					for(splitArrayNumber = 0; splitArrayNumber < higlighted.length; splitArrayNumber++){
						if(higlighted[splitArrayNumber] != ""){
							currentSelect.options[higlighted[splitArrayNumber]].selected = true;
						}else{
							break;
						}
					}
				
				
			}else{
				
				document.getElementById('pref_loc').value = currentSelect.selectedIndex;
				
			}
			
}
