
function dE(id_in) {
	return document.getElementById(id_in);
}
var cvtCheckInterval;
function strGoLnk(strHost) {
	return '';//<div align=center><a target="_blank" href="http://' + strHost + '"><img align=middle border=0 src="/engine/images_1/website_homepage_icon.gif"><span style="padding-left:30px;">View your new online training website!</span></a></div>';
}

var steps = {
	createPortal:1,
	addingFile:2,
	accountCreated:3,
	fileAdded:4,
	num:4,
	nStepLast:1,
	Set : function(nStepNum) {
		dE('step'+this.nStepLast).style.display='none';
		for(i=1;i<=this.num;i++)
			document.getElementById('step'+i).style.display=(i==nStepNum?'inline':'none');
	}
};

function CvtUpdateSts() {
	if(CvtJobID) { 
		cvtPgrs.location.replace("/engine/inc/ConvertStatus.asp?jobID="+CvtJobID+"&LID=1&ky="+sessID);
		dE("cvtPgrs").style.display='block';
	}
}

function OnCommand(strActionName, frmSource, objArgs) {
	switch(strActionName) {
	case 'OnPrepareUpload':
		window.frames['upldPgrs'].location.href='/engine/inc/Upload.asp?c=getPgrsID';	
		return false;
	case 'OnUploadReady':
		var frmSource=frmToUse;
		window.frames['upldPgrs'].location.href='/engine/inc/UploadSts.asp?pgrsID='+objArgs + '&d=' + (new Date()).getTime();
		
		OnUpload(true);

		frmSource.action = 'BuildLesson.asp?submitter='+frmSource.submitter.value+'&LID=1&ky='+sessID+'&iPK='+instPK+'&hrPK='+HRPK+'&coursesPK='+CoursePK+'&pgrsID='+objArgs + '&d=' + (new Date()).getTime();
		
		try {
			frmSource.submit()
		} catch (e) {
			OnFileError("Your file was not able to be successfully transferred to our servers at this time.  Please log into your account and re-try this attempt.");
		}		
		
		return false;
	case 'CrsUploadFile':
		
		frmToUse=frmSource;
			
		//if (is_mac && fl.indexOf("%22")!=-1) {			//bug {CD484728-573E-4fac-BBBA-85A7280349DF}
		//	alert("Please do not use double-quotes in your filenames.");
		//	return false;
		//}
		
		OnCommand('OnPrepareUpload');
		
		return false;	
	case 'CourseAddFileDone':
		/*
		if(objArgs.strError.length) {
			document.getElementById('oFileBox').innerHTML = objArgs.strErr;
		} else {
			document.getElementById('oFileBox').innerHTML = '<b>'+objArgs.strMungedFileName+'</b> has been uploaded.';	
		}
		document.getElementById('oUploadPgrsBox').style.display='none';
		document.getElementById('oFileBox').style.display='block';
		*/
		addFileDone();
		return false;
	case 'CourseAddImageCancel':
		OnCommand('CourseAddImageDone',null,{strError:'Your file upload has been cancelled.'});
		return false;
	default:
		alert('Invalid command \'' + strActionName + '\' to OnCommand()');
		return;
	}
}

function OnConvertStatus(feCt,cvtSuc,cvtFail) {
	if (cvtSuc+cvtFail==feCt){	
//	if(((feCt<=5) && ((cvtSuc+cvtFail)==feCt)) || ((cvtSuc+cvtFail) > 5)) {
		clearInterval(cvtCheckInterval);
		fileAdded=true;
		
		setTimeout("steps.Set(4);dE('buildContent').style.display='none';dE('buildFileAdded').style.display='block';",3000);
	}
}

var lastErrLoc=null;
function WindowShowError(errMsg, errLoc) {
	if(errLoc == 'oFileError') {
		dE('buildContent').innerHTML = '<span style="color:red;font-size:12px;font-weight:bold;">' + errMsg + '<br><br>This issue has has been e-mailed to our support team (support@mindflash.com)</span>';
		return;
	}		

	if(lastErrLoc) 
		dE(lastErrLoc).style.color='black';
		
	if(errLoc && errLoc != '') {
		dE(errLoc).style.color='red';
		lastErrLoc=errLoc;
	}
	
	if(errLoc=='oSubDomain') {
		document.getElementById('SubDomain').style.backgroundColor='white';
		document.getElementById('SubDomain').style.border='2px solid red';
	}	
	
	dE('ErrMsg').innerHTML=errMsg;
	dE('ErrMsg').style.display='block';
}

function OnPortalCreateError(errMsg, errLoc) {
	WindowShowError(errMsg, errLoc);
	ShowFreeTrialForm(true);
}

function ShowFreeTrialForm(bMode) {
/*	dE('tag_line').style.display=bMode?'block':'none'; */
	dE('info_form').style.display=bMode?'block':'none';
	dE('create_form').style.display=bMode?'none':'block';
	
	if(bMode && !bSubDomainCustomized) nSubDomainTmr = setInterval(updateSubDomain, 100);
}

function OnFileAfterUpload(CvtJobID_in) {
	
	CvtJobID = CvtJobID_in;
	CvtUpdateSts();
	cvtCheckInterval = setInterval(CvtUpdateSts,4000);
}

function OnFileError(strErrorMessage) {
	WindowShowError(strErrorMessage, 'oFileError');
	
}

function addFileDone() {
	dE('buildNoFileAdded').style.display='none';
}

function OnPortalCreated(sessID_in, instPK_in, urla, uname, pwd, hrpk, coursesPK, cName, nShowFileUpload) {
	
	CourseName=cName;
	SiteURL=urla;
	UserName=uname;
	PassWord=pwd;
	HRPK=hrpk;
	CoursePK=coursesPK;
	sessID=sessID_in;
	instPK=instPK_in;
		
	dE('websiteAddress').innerHTML='<a target="_blank" href="http://'+SiteURL+'">http://'+SiteURL+'</a>';
	dE('websiteUsername').innerHTML = UserName;
	dE('websitePassword').innerHTML = PassWord;
	dE('websiteLink').innerHTML = strGoLnk(SiteURL);
	
	var frm = document.fileForm;
	var fileAddedFirst=false;

	if(frm.file.value=='') {
		if(nShowFileUpload==0) {
			steps.Set(3);			
		}else{	
			OnUpload(false);
			steps.Set(2);
		}
	} else {
		fileAddedFirst=true;
		steps.Set(3);
		OnCommand("CrsUploadFile",frm);
	}
	
	if(nShowFileUpload>0) {
		dE('buildContent').style.display=fileAddedFirst?'block':'none';
//		dE('buildNoFileAdded').style.display=fileAddedFirst?'none':'block';
	} else {
		dE('buildContent').style.display='none';
//		dE('buildNoFileAdded').style.display='none';
	}
}

function OnPortalEnter() {
	if(SiteURL!="") {
		window.open('http://'+SiteURL);
	} else
		alert("Nowhere to go!");
}

function OnCourseInfo() {
}

function OnUpload(added) {
	
}

function OnUploadDone() {
}

function OnWindowLoad() {
	if(HRPK != '' && SiteURL != '') {
		dE('websiteAddress').innerHTML = '<a target="winPortal" href="http://'+SiteURL+'">http://'+SiteURL+'</a>';
		dE('websiteUsername').innerHTML = UserName;
		dE('websitePassword').innerHTML = '<i><a target="winPortal" href="http://'+SiteURL+'/engine/authentication/login/password.asp?LID=1&ky='+sessID+'">Click here if you forgot your password</a></i>';
		dE('websiteLink').innerHTML = strGoLnk(SiteURL);


		if(CvtJobID.length && guidJobIDCurr.length && CvtJobID==guidJobIDCurr) {
			OnFileAfterUpload(CvtJobID);			
			dE('buildFileAdded').style.display='none';
			dE('buildNoFileAdded').style.display='none';
			dE('buildContent').style.display='block';
			steps.Set(3);
		//} else if(!fileAdded && !nChild && !bCrsHasFile) {
			//steps.Set(2);
			//dE('buildNoFileAdded').style.display='block';
		} else if(fileAdded || bCrsHasFile) {
			steps.Set(4);	// file added
		}else{
			steps.Set(3);	// account created
			dE('buildFileAdded').style.display='none';
		}
		
		ShowFreeTrialForm(false);
		//dE('info_form').style.display='none';
		//dE('create_form').style.display='block';
	
	} else {
		ShowFreeTrialForm(true);
		document.newPortalForm.first.focus();
		steps.Set(1);
		
		if(nChild>0) 
			dE('trFileForm').style.display='none';
		else
			dE('trFileForm').style.display='block';
	}	
}

var frmToUse;
function addFileAfter(){
	var frm=document.fileFormAfter;

	if(frm.file.value=='') {
		alert('You must select a file');
	} else {
		steps.Set(3);
		OnCommand("CrsUploadFile", frm);
		dE('buildNoFileAdded').style.display='none';
		dE('buildContent').style.display='block';
		
		/*frm.action = "BuildLesson.asp" + 
					 "?submitter=CreatePortal" +
					 "&" + sessStr +
					 "&pgrsID="+pgrsID+
					 "&hrPK="+HRPK+
					 "&coursesPK="+CoursePK;
		window.frames['upldPgrs'].location.href='/groups/config/UploadSts.asp?pgrsID='+pgrsID;
		OnUpload(true);
		frm.submit();
		*/					
	}
}

function CreatePortal() {
	var newPortalForm = document.newPortalForm;
		
	if (isFilled(newPortalForm.first) && 
		isFilled(newPortalForm.last) && 
		isFilled(newPortalForm.email) && 
		isEmail(newPortalForm.email) && 
		isFilled(newPortalForm.InstName) && 
		isFilled(newPortalForm.CrsName) && 
		isFilled(newPortalForm.SubDomain)) {
		
		newPortalForm.SubDomain.value = munge(newPortalForm.SubDomain.value, true);
	
		steps.Set(1);
		
		ShowFreeTrialForm(false);
			
		newPortalForm.submit()
	} 


}

var i
var txt
function isFilled(txb) {
	txt=txb.value
	if (txt!='')
		for (i=0;i<txt.length;i++) {
			if (txt.charAt(i)!=' ') 
				return true
		}
	txb.focus()
	txb.select()
	alert("This field is required.")
	return false
}

function isEmail(txb)
 {
	var txt
	var i

	txt = txb.value
	if (txt != "") 
	{
   
		invalidChars = " /:,;"
		   
		for (i=0; i<invalidChars.length; i++)
		{
			badChar = invalidChars.charAt(i)
			if (txt.indexOf(badChar,0) != -1) {
				txb.focus()
				alert("Invalid characters in e-mail field")
				return false
		    }
		}
		              
		atPos = txt.indexOf("@",1)
		if (atPos == -1) {
			txb.focus()
			alert("Invalid e-mail address")
			return false             
		}
		       
		if (txt.indexOf("@",atPos+1) != -1) {
			txb.focus()
			alert("Invalid e-mail address, more than one '@' found")
			return false
		}
	
		periodPos = txt.indexOf(".",atPos)
		if (periodPos == -1) {
			txb.focus()
			alert("Invalid e-mail address")
			return false
		}
		                   
		if (periodPos+3 > txt.length) {
		    txb.focus()
		    alert("Invalid e-mail address")
		    return false
		}                                
	}
	return true
}

/*
function isSubDomain(txb)
{
	var txt;
	var i
	var charCode
	var chr
	var bHadSpaces=false;
	var strInvalidChrs='';
	
	txt = String(txb.value)
    txt = txt.replace(/ /g,'');    
    
    if(txb.value != txt) bHadSpaces=true;
    
    //make sure that the name entered is a alphanumeric   
	for (i=0; i<txt.length; i++)  {
		charCode = txt.charCodeAt(i)
		chr = txt.charAt(i)
		
		if ((charCode >= 48 && charCode <=57) || (charCode >= 65 && charCode <=90) || (charCode >= 97 && charCode <=122)) {
					
		}
		else {
			strInvalidChars+=chr;
			//alert("Invalid character [" +chr +"] found. Please note that only letters and numbers are allowed in this box.")
			//txb.focus()
			//return false
		}
	}
	
	if(strInvalidChars.length) {
		alert("The following characters are not allowed in the website address and have been removed:\n\n'+strInvalidChars)
	}
	txb.value=txt;
    return true
}
*/

function munge(txt_in, bShowAlert)
{
	var strNew = '';
	var strInvalidChars = '';
	var bHasSpaces=false;
	var i;
	var charCode;
	var chr;
	var txt = txt_in;
    txt = txt.replace(/ /g,'');
    
    if(txt_in != txt) bHasSpaces=true;
    
	//make sure that the name entered is a alphanumeric
	for (i=0; i<txt.length; i++)  {
		charCode = txt.charCodeAt(i)
		chr = txt.charAt(i)
		
		if ((charCode >= 48 && charCode <=57) || (charCode >= 65 && charCode <=90) || (charCode >= 97 && charCode <=122)) {
			strNew+=chr;
		} else {
			strInvalidChars+=chr;
		}
	}
	if(bShowAlert) {
		if(strInvalidChars || bHasSpaces) {		
			alert('The following characters are not allowed in the website address and have been removed:\n\n'+strInvalidChars+(bHasSpaces?'(space)':'')+'\n\nPress OK to continue.');
		}
	}

	//txt_in.value=strNew;
	return strNew;		
}

var strSubDomainLast;
var bSubDomainCustomized=false;
var nSubDomainTmr=null;

function updateSubDomain() {
	var strOrgName = document.getElementById('InstName').value;
	var oSubDomain = document.getElementById('SubDomain');
	
	var strNewURL =  munge(strOrgName);
	
	if(strSubDomainLast != strNewURL && strNewURL.length < 50)
		oSubDomain.value = strSubDomainLast = strNewURL;		
}

function stopSubDomain() {	
	bSubDomainCustomized=true;
	clearTimeout(nSubDomainTmr);
	nSubDomainTmr=null;
	
	var oSubDomain = document.newPortalForm.SubDomain;
	oSubDomain.value =  munge(oSubDomain.value, true);	
}
