//Regular expression to check e-mail address validity
var emailRegExp = new RegExp("[\.\-_a-zA-Z]+@[\.\-_a-zA-Z]+\.[a-zA-Z]{2,4}");


function validateRMAForm(aForm) {


	
	if(aForm.elements['distributor_name'].value == "") {
		alert('Please fill in the Distributor Name');
		return false;
	}
	
	if(aForm.elements['distributor_contact'].value == "") {
		alert('Please fill in the Distributor Contact Person');
		return false;
	}

	if(aForm.elements['distributor_address'].value == "") {
		alert('Please fill in the Distributor Address');
		return false;
	}
	if(aForm.elements['distributor_city'].value == "") {
		alert('Please fill in the Distributor City/ZIP');
		return false;
	}
	if(aForm.elements['distributor_country'].value == "") {
		alert('Please fill in the Distributor Country');
		return false;
	}			
	if(aForm.elements['distributor_phone'].value == "") {
		alert('Please fill in the Distributor Phone/Fax Number');
		return false;
	}		
	if(aForm.elements['distributor_email'].value == "") {
		alert('Please fill in the Distributor E-Mail Address');
		return false;
	} else {

		if (!aForm.elements['distributor_email'].value.match(emailRegExp)) {
			alert("Please enter a valid distributor e-mail address");
			return false;
		}		
	}
	if(aForm.elements['consumer_email'].value != "") {
		if(!aForm.elements['consumer_email'].value.match(emailRegExp)) {
			alert("Please enter a valid consumer e-mail address");
			return false;		
		}
	}
	
	if(aForm.elements['confirm_email'].value == "") {
		alert("Please confirm your E-Mail Address");
		return false;
	}

	if(aForm.elements['distributor_email'].value != aForm.elements['confirm_email'].value){
		alert("Please check the confirmation of the e-mail address");
		return false;
	}
	
	if(aForm.elements['probDescArea'].value == "" || aForm.elements['probDescArea'].value == "enter problem description here") {
		alert('Please describe your problem!');
		return false;
	}	

	return true;
}

function validateReturnForm(aForm) {
	if(aForm.elements['rma_id'].value == "") {
		alert('Please fill in the RMA #');
		return false;
	}
	
	//Check RMA# validity
	var rmaRe = new RegExp("RP[-]{0,1}[0-9]{2}-[0-9]{3,4}");
	if(!aForm.elements['rma_id'].value.match(rmaRe)) {
		alert('The RMA # you have entered is invalid');
		return false;
	}
	
	if(aForm.elements['shipping_name'].value == "") {
		alert('Please fill in the Shipping Name');
		return false;
	}	
	if(aForm.elements['shipping_contact'].value == "") {
		alert('Please fill in the Shipping Contact Person');
		return false;
	}
	if(aForm.elements['shipping_address'].value == "") {
		alert('Please fill in the Shipping Address');
		return false;
	}
	if(aForm.elements['shipping_city'].value == "") {
		alert('Please fill in the Shipping City/ZIP');
		return false;
	}
	if(aForm.elements['shipping_country'].value == "") {
		alert('Please fill in the Shipping Country');
		return false;
	}			
	if(aForm.elements['shipping_phone'].value == "") {
		alert('Please fill in the Shipping Phone');
		return false;
	}		
	if(aForm.elements['probDescArea'].value == "" || aForm.elements['probDescArea'].value == "enter problem description here") {
		alert('Please describe your problem!');
		return false;
	}

	//Check Billing Address	
	if(!aForm.elements['billing_switch'].checked) {
		if(aForm.elements['billing_name'].value == "") {
			alert('Please fill in the Billing Name');	
			return false;
		}	
		if(aForm.elements['billing_contact'].value == "") {
			alert('Please fill in the Billing Contact Person');
			return false;
		}
		if(aForm.elements['billing_address'].value == "") {
			alert('Please fill in the Billing Address');
			return false;
		}
		if(aForm.elements['billing_city'].value == "") {
			alert('Please fill in the Billing City/ZIP');
			return false;
		}
		if(aForm.elements['billing_country'].value == "") {
			alert('Please fill in the Billing Country');
			return false;
		}			
		if(aForm.elements['billing_phone'].value == "") {
			alert('Please fill in the Billing Phone');
			return false;
		}
		

		

	}
	
	if(aForm.elements['return_email'].value == "") {
		alert('Please fill in a vaild e-mail address');
		return false;
	} else {
		if (!aForm.elements['return_email'].value.match(emailRegExp)) {
			alert("The e-mail address you have entered is not vaild, please check your input");
			return false;
		}	
	}
	
	return true;
}

function clearTextArea(aTextArea) {

	if(aTextArea.value == "enter problem description here" || aTextArea.value == "please specify") {
		aTextArea.value = "";
	}
}

function toggleFields_Return(aCheckBox) {
	var bElements=new Array("billing_name","billing_contact","billing_country","billing_address","billing_city","billing_phone");
	var bgColor;
		
	if(aCheckBox.checked) {
		bgColor = "#eaeaea";
	} else {
		bgColor = "white";
	}
	
	for(i=0;i<bElements.length;i++) {
		aCheckBox.form.elements[bElements[i]].disabled = aCheckBox.checked;
		aCheckBox.form.elements[bElements[i]].style.backgroundColor = bgColor;
	}
}

/**
 * This function sets the disabled state to the state of another element (currently only checkboxes)
 */
function toggleDisableState(anElement,elementToDisable) {
	var state, type;
	
	//Get the state
	if(anElement.type == "checkbox") {
		state = !anElement.checked;
	}
	
	//Set the state
	type = anElement.form.elements[elementToDisable].type;
	
	if(type == "select-one" || type == "select-multiple") {
		element = anElement.form.elements[elementToDisable];
		element.disabled = !anElement.checked;
	}
	else {
		for(i = 0; i < anElement.form.elements[elementToDisable].length; i++) {
			element = anElement.form.elements[elementToDisable][i];
			element.disabled = !anElement.checked;
		}
	}
	
}

function openLinkInNewWindow(aLink, windowName) {
	window.open(aLink,windowName,'width='+screen.width *0.99+',height='+screen.height*0.9+',left=0,top=0');
}

function setDisabledState(elementToDisable,state) {
	elements = document.getElementsByName(elementToDisable);
	for (i = 0; i < elements.length; i++) {
		elements[i].disabled = state;
	}
}

