﻿/*--Divvycast Labels Uploader Module scripts
Updated: December 7, 2006
--*/

//add track input fields on Upload page

function addFiles() {
			var ni = document.getElementById('tracks');
			var numi = document.getElementById('theValue');
			var maxLimit = document.getElementById('maxLimit').value;
			var num = (document.getElementById("theValue").value -1)+ 2;
			if (num>maxLimit && maxLimit != 0) {
				alert("Currently we allow only "+maxLimit+" files to be uploaded at one time.");
				return;
			}
			numi.value = num;
			var divIdName = "track"+num;
			var newdiv = document.createElement('div');
			newdiv.setAttribute("id",divIdName);
			newdiv.innerHTML = "<input type=file name=track"+num+" size=\"30\"> <a href=\"javascript:;\" onclick=\"removeEvent(\'"+divIdName+"\')\" class='remove'>&nbsp;Remove</a>";
			ni.appendChild(newdiv);
		}
		
		function removeEvent(divNum){
			var d = document.getElementById('tracks');
			var olddiv = document.getElementById(divNum);
			d.removeChild(olddiv);
		}
		
//error checking for login

var proceed = true;
	function checkForm() {
		proceed = true;
		var error_message = '';
		var x = window.document.login.user_name.value;
		if(x.length < 5) {
			error_message = error_message + "\nYour username should have a minimum of five characters";
			proceed = false;
		}
		
		var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>? ";	//Invalid chars
		
		var invalchar = false;
		for (var i = 0; i < x.length; i++) {
			if (iChars.indexOf(x.charAt(i)) != -1) {
				proceed = false;
				invalchar = true;
			}
		}
		if(invalchar) {
			error_message = error_message + "\nYour username contains characters which are not allowed.";
		}
		
		var x = window.document.login.password.value;
		if(x.length < 6) {
			error_message = error_message + "\nYour password should have a minimum of six characters";
			proceed = false;
		}
		
		if (proceed == false) {
			var alert_message = error_message + "\nQuestions? E-mail: support@divvycast.com";
			alert(alert_message);
			proceed = false;
		}
	}

//confirm deletion of files

function confirmDelete(formname) {  
	var myform = formname;	
	var ans = confirm('Are you sure that you want to delete this file?');	
	if(!ans) 
		{ 	return false;	
		} 
	}
	
//popup launcher

function popUp(url) {
	newwindow=window.open(url,'name','height=450,width=600,status=1,scrollbars=1');
	if (window.focus) {newwindow.focus()}
	return false;
}
	
//check for valid image file types 

var isValid = false;
		var validextensions = new Array("jpg","png","gif");
		function validateFile() { 
		// this function is used to replace the "Browse" Button of file control 
			isValid = false;
			var fname=document.uploadbutton.buttonfile.value;
			var ext="";
			
			while (fname.indexOf("\\") != -1) {
				fname = fname.slice(fname.indexOf("\\") + 1);
			}
			ext = fname.slice(fname.indexOf(".")).toLowerCase();
			
			for (var i = 0; i < validextensions.length; i++) {
				if (validextensions[i] == ext) { 
					isValid=true;
					break; 
				}
			}
			if(!isValid) {
				alert ("Please select a valid image file type. \n Supported file types are: "+(validextensions.join(" , ")));
				document.uploadbutton.buttonfile.focus();
				return false;
			}
			else{
				return true;
			}	
		}
