// JavaScript Download Validation Scripts 

function validateDownload(){
	var goFlag = true;
	var msg = "";
	var lineBreak = (document.getElementById) ? "<br />" : "\n"
	//reset error icons and messages
	initialize(10);
	
	// title
	if (Trim(document.fmEditDownload.title.value) == ""){
		document.getElementById("q1").className = "formErr";
		msg = "Please enter a title for your download." + lineBreak;
		goFlag = false;
	}
	
	// short description
	if (Trim(document.fmEditDownload.shortDescription.value) == ""){
		document.getElementById("q2").className = "formErr";
		msg += "Please enter a short description." + lineBreak;
		goFlag = false;
	}
	
	// author
	if (document.fmEditDownload.authorID.selectedIndex == 0){
		document.getElementById("q3").className = "formErr";
		msg += "Please choose an author." + lineBreak;
		goFlag = false;
	}
		
	// dev. env.
	if (!radioChecked(document.fmEditDownload.ide)){
		document.getElementById("q4").className = "formErr";
		msg += "Please choose one or more developer environments." + lineBreak;
		goFlag= false;
	}
		
	// programming language
	if (!radioChecked(document.fmEditDownload.lang)){
		document.getElementById("q5").className = "formErr";
		msg += "Please choose one or more programming languages." + lineBreak;
		goFlag= false;
	}
		
	// platform
	if (!radioChecked(document.fmEditDownload.platform)){
		document.getElementById("q6").className = "formErr";
		msg += "Please choose one or more platforms." + lineBreak;
		goFlag= false;
	}
		
	// product
	//alert(document.fmEditDownload.product.length + '\nvalue= ' + document.fmEditDownload.product.value + '\nselectedIndex= ' + document.fmEditDownload.product.selectedIndex + '\noption 5 value= ' + document.fmEditDownload.product.options[5].value + '\ntype= ' + document.fmEditDownload.product.type);
	if (Trim(document.fmEditDownload.product.value) == ""){
		document.getElementById("q7").className = "formErr";
		msg += "Please choose at least one product." + lineBreak;
		goFlag = false;
	}
		
	// OS
	if (!radioChecked(document.fmEditDownload.os_name)){
		document.getElementById("q8").className = "formErr";
		msg += "Please choose one or more operating systems." + lineBreak;
		goFlag= false;
	}
	
	// usage guide, if they entered one
	//alert(Trim(document.fmEditDownload.addin_url.value).slice(0,7));
	if ((Trim(document.fmEditDownload.addin_url.value) != "") && (Trim(document.fmEditDownload.addin_url.value).slice(0,7)) != "http://"){
		document.getElementById("q9").className = "formErr";
		msg = "It appears that your URL is in the wrong format. It should begin with 'http://'." + lineBreak;
		goFlag = false;
	}
	
	// file location (url)
	if ((document.fmEditDownload.downloadID.value == 0) && (Trim(document.fmEditDownload.url.value) == "")){
		document.getElementById("q10").className = "formErr";
		msg += "Please browse to a file to upload." + lineBreak;
		goFlag = false;
	}
	
	if (!goFlag){
		if (document.getElementById){ // for compliant browsers
			document.getElementById("dynMsg").innerHTML = msg;
			document.getElementById("dynMsg").style.visibility = "visible";
		}else{ // for legacy browsers
			alert(msg);
		}
		return false;
	}
}


function radioChecked(elem){
	var isChecked= false;
	for (var i=0; i < elem.length; i++){
  		if(elem[i].checked){
  			isChecked = true;
  		}
		}
	if (!isChecked){
		return false;
	}else{
		return true;
	}
}


function initialize(limit){
	var ge = document.getElementById; 
	// reset flags, turn off any error icons, and hide the dynamic message text
	if (document.getElementById){
		for (i = 1; i <= limit; i ++){
			tmpFld = "q" + i;
			document.getElementById(tmpFld).className = "";
		}
		document.getElementById("dynMsg").innerHTML = "";			
		document.getElementById("dynMsg").style.visibility = "hidden"; 		
	}
}


function confirmDelete(){
	var msg = 'Are you sure you want to delete this?\n\nIf you choose OK, it will be permanently removed from the system.\n\nAre you sure you want to continue?';
	if (!confirm(msg)) { return false; }
}

