/* 
 * PHP File Uploader with progress bar Version 1.20
 * Copyright (C) Raditha Dissanyake 2003
 * http://www.raditha.com

 * Licence:
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Initial Developer of the Original Code is Raditha Dissanayake.
 * Portions created by Raditha are Copyright (C) 2003
 * Raditha Dissanayake. All Rights Reserved.
 * 
 */
 
var postLocation="pgbar.php";

//trim a string
function trim($str) { return $str.replace(/^\s+|\s+$/g, ''); }

var qsParm = new Array();

function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			qsParm[key] = val;
//			alert('qsParm[' + key + '] = ' + val);
		}
	}
}

//prevent form from being submitted by the enter key
function keydown(){
	if (window.event.keyCode == 13) {
//		alert('cancelling'); 
	    event.returnValue=false; 
	    event.cancel = true;
	}
}

/**
 * this method will check the filename with a
 * given list of banned extension. If the
 * extension doesn't match, an alert will be popped up and the
 * upload will not continue;
 */
function check_filetypes() {
	if(!document.forms['uploadform'].uploadfile.value){
		alert('Please select a file to upload');
		return false;
	}
	myRE = new RegExp("\.mp3$", "i");
	if(!document.forms['uploadform'].uploadfile.value.match(myRE)){
		alert('We are unable to upload this file. Please upload an MP3, preferably in PC format with a ".mp3" extension');
		return false;
	}
	//TODO check if file exists
	return true;
}

function popUP(mypage, myname, w, h, scroll, titlebar){
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function postIt(){
	alphanumRe = new RegExp("^[A-Za-z0-9\-\_ ]+$")
	phoneRe = new RegExp("^[\+]?[0-9]+$")

	//validate upload file
	if(check_filetypes() == false){
		return false;
	}

	//validate artist
	var artist=trim(document.forms['uploadform'].artist.value);
	if ((artist==null)||(artist=="")){
		alert("Please enter Artist name");
		document.forms['uploadform'].artist.focus();
		return false;
	}
	if(!artist.match(alphanumRe)){
		alert("Invalid characters in Artist name");
		document.forms['uploadform'].artist.focus();
		return false;
	}
	
	//validate track
	var track=trim(document.forms['uploadform'].track.value);
	if ((track==null)||(track=="")){
		alert("Please enter Track name");
		document.forms['uploadform'].track.focus();
		return false;
	}
	if(!track.match(alphanumRe)){
		alert("Invalid characters in Track name");
		document.forms['uploadform'].track.focus();
		return false;
	}
	
	//validate phone
	var phone=trim(document.forms['uploadform'].phone.value);
	if((phone!=null)&&(phone!="") && !phone.match(phoneRe)){
		alert("Invalid characters in Phone");
		document.forms['uploadform'].phone.focus();
		return false;
	}
	
	//validate style
	var style=trim(document.forms['uploadform'].style.value);
	if ((style==null)||(style=="")){
		alert("Please enter Music style");
		document.forms['uploadform'].style.focus();
		return false;
	}
	if(!style.match(alphanumRe)){
		alert("Invalid characters in Music style");
		document.forms['uploadform'].style.focus();
		return false;
	}
	
	//validate email address
	var emailID=trim(document.forms['uploadform'].email.value);
	if ((emailID==null)||(emailID=="")){
		alert("Please enter your email address");
		document.forms['uploadform'].emailID.focus();
		return false;
	}
	if (echeck(emailID)==false){
		document.forms['uploadform'].emailID.focus();
		return false;
	}

	//everything is OK - proceed
	baseUrl = postLocation;
	sid = document.forms['uploadform'].sessionid.value;
	iTotal = escape("-1");
	qs();
	baseUrl += "?iTotal=" + iTotal;
	baseUrl += "&iRead=0";
	baseUrl += "&iStatus=1";
	baseUrl += "&sessionid=" + sid;
	if(qsParm['c']!=undefined){
		baseUrl += "&c=" + qsParm['c'];
	}
	if(qsParm['bg']!=undefined){
		baseUrl += "&bg=" + qsParm['bg'];
	}
	if(qsParm['sb']!=undefined){
		baseUrl += "&sb=" + qsParm['sb'];
	}
	popUP(baseUrl,"Uploader",360,210,false,false);
	document.forms['uploadform'].submitbut.disabled = true;
	document.forms['uploadform'].submit();
}
/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail address")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   alert("Invalid E-mail address")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	   alert("Invalid E-mail address")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   alert("Invalid E-mail address")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	   alert("Invalid E-mail address")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	   alert("Invalid E-mail address")
	    return false
	 }

	return true					
}

	

