
var myPage = "home";
var __netCheck;// string "x" if all data has been collected from .NET
var __round = 0;// counts the number of times OnUpdate is called before __netCheck is set to "x"
var __fn;

var __toUrl = "";
var __toUrlMessage = "";
var __toUrlConfirm = true;

var __singleLimit = 75;

var FormControl;
var EditCollection;

onload = function() {
	window.setTimeout("Init()", 100);	
}

function Init() {
	/*
	EditCollection = new Array(new EditInfo("No Editing", "none", 0), 
								new EditInfo("Proofreading", "proof", 35), 
								new EditInfo("Package 1", "package1", 85),
								new EditInfo("package 2", "package2", 95),
								new EditInfo("package 3", "package3", 110),
								new EditInfo("package 4", "package4", 120));
	*/

	/*
	EditCollection = new Array(new EditInfo("Single Message", "single", 0),
							   new EditInfo("Multiple Messages", "none", 50), 
								new EditInfo("Multiple Messages with Proofreading", "proof", 74));
	
	*/
	
	EditCollection = new Array(new EditInfo("Single Message", "basic", 0),
							   new EditInfo("Single User", "single", 50),
							   new EditInfo("Multiple Extension User", "multiple", 50));
	
	
	EditCollection.GetEditInfo = function(val) {
		var info = new EditInfo("", "", 0);
		var len = this.length;
		for (var i=0; i < len; i++) {
			if (this[i].value == val) {
				info = this[i];
				break;
			}
		}
		return info;
		
	}
	

	FormControl = Get("PageForm");
	MyInit();
	ControlInit();
	Build();	
	
	
}

function MyInit() {}
function ControlInit() {}
function Build() {}


function AlertError(val) {
	alert(val);
}

function EditProfile() {
	if (myPage == "" || myPage == null) {
		GoToUrl("index.aspx?contentValue=profileEdit");
	} else {
		GoToUrl("index.aspx?contentValue=profileEdit&retUrl="+myPage);
	}
	
}

function EditTitle() {
	GoToUrl("index.aspx?contentValue=nameProduction&retUrl="+myPage);
}

function GoToUrl(url) {

	
}

function GetPrevPage() {
	var que = window.location.search;
	var start = que.indexOf("retUrl=");
	var end;
	var retVal = "";
	if (start > 0) {
		end = que.indexOf("&", start);
		if (end > 0) {
			retVal = que.substring(start+7, end);
		} else {
			retVal = que.substring(start+7);
		}
	}
	return retVal;	
}

//call this function after calling all .net functions to ensure all your information is retreived
//make sure .NET has a Validate method that returns "x"
function Validate(fn) {
	__fn = fn;
	VMProduction.MainPage.ValidateAjaxCall(Validate_CallBack);
	window.setTimeout("CallOnUpdate()", 500);
	
}

function Validate_CallBack(response) {
	if (response.error != null) {
		AlertError(response.error);
		return;
	}
	__netCheck = response.value;

}

//keep calling CallOnUpdate() until __netCheck is set to "x" or __round > 5
function CallOnUpdate() {
	if (__netCheck != "x") {
		__round += 1;
		if (__round < 30) {
			window.setTimeout("CallOnUpdate()", 200);
		} else {
			AlertError("process timed out...\r\nrefresh your browser and try again.");
		}
	} else {
		__netCheck = null;
		__round = 0;
		eval(__fn).call();
	}	
}

function UpdateCalc() {
	var baseMin = production.GetBasicMinutes();
	//var musicMin = production.GetMusicMinutes();
	var baseMinTxt = "Minutes";
	if (baseMin == 1) {
		baseMinTxt = "Minute";
	}
	
	Get("proWords").innerHTML = production.words;
	/*
	if (production.words == 1) {
		Get("proWords").innerHTML = 0;
	} else {
		Get("proWords").innerHTML = production.words;
	}
	*/
	
	Get("proVoice").innerHTML = "$"+production.GetVoicePrice();
	
	Get("proMusic").innerHTML = "$"+production.GetMusicPrice();
		
	//Get("proMinutes").innerHTML = production.GetBasicMinutes()+" "+baseMinTxt;
	
	
	if (production.editType == "single" || production.editType == "multiple") {
		Get("proMinutes").innerHTML = production.GetBasicMinutes()+" "+baseMinTxt;
		
	} else {
		Get("proMinutes").innerHTML = __singleLimit+" Words";
		//Get("proTotal").innerHTML = "$"+(production.editPrice+production.basicPrice).toFixed(2);
	}
	
	Get("proTotal").innerHTML = "$"+production.GetTotalPrice();
	
	Get("proName").innerHTML = "( ID: "+production.id+" ) "+production.title;
	
}

function GetUser() {
	
	VMProduction.MainPage.GetUserInfo(user.id.toString(), GetUser_CallBack);
}


function GetUser_CallBack(response) {
	if (response.error != null) {
		AlertError(response.error);
		return;
	}
	
	
	var info = response.value.Rows[0];
	
	//user.id = Number(info.userID);
	if (info != null) {
		user.companyName = info.companyName;
		user.firstName = info.firstName;
		user.lastName = info.lastName;
		user.email = info.email;
		user.phone = info.phone;
		user.type = info.userType;
		user.city = info.city;
		user.address = info.address;
		user.state = info.state;
		user.zip = info.zip;
		OnUser();
	}
	
}

function OnUser() {}


function GetProduction() {
	VMProduction.MainPage.GetProduction(user.id.toString(), production.id.toString(), GetProduction_CallBack);
}

function GetProduction_CallBack(response) {
	if (response.error != null) {
		AlertError(response.error);
		return;
	}

	var info = response.value.Rows[0];
	production.title = info.title;
	production.editType = info.editType;
	production.status = info.statusName;
	
	if (info.voiceName != null) {
		production.voiceName = info.voiceName;
	}
	if (info.voiceRef != null || info.voiceRef != "") {
		production.voiceRef = info.voiceRef;
	}

	if (info.isResident == null || info.isResident == "Y") {
		production.isResVoice = true;
	} else {
		production.isResVoice = false;
	}
	if (Number(info.voicePrice) > 0) {
		production.voicePrice = Number(info.voicePrice);
	}
	
	if (info.musicName != null && info.musicCode != null) {
		production.musicName = info.musicName;
		production.musicCode = info.musicCode;
	}

	if (Number(info.wordCount) > 0) {
		production.words = Number(info.wordCount);
	}
	production.musicCount = Number(info.musicCount);

	production.amountPaid = Number(info.amountPaid);
	production.statusID = Number(info.statusID);
	if (production.amountPaid > 0) {
		//production.minute = info.minute;
		production.basicPrice = info.basicPrice;
		production.editPrice = info.editPrice;
		production.musicPrice = info.musicPrice;		
	} else {
		production.editPrice = EditCollection.GetEditInfo(production.editType).price;
	}
	OnProduction();
}

function OnProduction() {}





function User() {
	this.id = "";
	this.userType = "user";
	this.companyName = ""
	this.firstName = "";
	this.lastName = "";
	this.email = "";
	this.phone = "";
	this.password = "";
}

function Billing() {
	this.id = 0;
	this.firstName = "";
	this.lastName = "";
	this.address = "";
	this.city = "";
	this.state = "";
	this.zip = "";
	this.phone = "";	
}


function Production() {
	
	this.minute = 155;
	this.basicPrice = 50;
	this.editPrice = 0;
	this.musicPrice = 50;
	this.amountPaid = 0;
	this.voicePrice = 150;
	this.isResVoice = true;
	
	//this.scriptMinutes = 0;
	//this.musicMinutes = 0;
	
	this.words = 1;
	this.musicCount = 0;

	this.id = 0;
	this.musicName = "";
	this.musicCode = "";
	this.voiceName = "";
	this.voiceRef = "";
	this.editType = "none";
	
	this.status = "";
	this.statusID = 0;
	this.title = "";
	
}

Production.prototype.SetWords = function(val) {
	if (val > 0) {
		this.words = val;
	} else {
		this.words = 1;
	}
}


Production.prototype.GetBasicMinutes = function() {
	/*
	var mins = this.words/this.minute;

	var baseMins = 0;
	if (mins > 1) {
		var num = mins;
		var rndNum = Math.round(mins);
		if (rndNum < num) {
			baseMins = rndNum+0.50;
		} else {
			baseMins = rndNum;
		}
	} else {
		baseMins = Math.ceil(mins);
	}
	//var minWords = Math.ceil(this.words/this.minute);
	return baseMins;
	*/
	
	
	var totalMins = this.words/this.minute;
	var mins = 1;
	
	if (totalMins > 1) {
		var cycle = Math.floor(totalMins);
		var remain = (totalMins) - cycle;
		var diff = this.words-this.minute;
		
		if (remain <= 75/155 && remain > 0) {
			mins = cycle+0.50;
		} else {
			mins = Math.ceil(totalMins);
		}
	}
	
	return mins;
	
	
}

/*
Production.prototype.GetMusicMinutes = function() {
	var musMins = this.musicWords/this.minute;
	var baseMins = 0;
	if (musMins > 1) {
		var num = musMins;
		var rndNum = Math.round(musMins);
		if (rndNum < num) {
			baseMins = rndNum+0.50;
		} else {
			baseMins = rndNum;
		}
	} else {
		baseMins = Math.ceil(musMins);
	}
	//var musWords = Math.ceil(this.musicWords/this.minute);
	return baseMins;
}
*/
Production.prototype.GetBasicPrice = function() {

	return (this.GetBasicMinutes()*this.basicPrice)+(this.GetBasicMinutes()*this.editPrice);
}


Production.prototype.GetMusicPrice = function() {
	if (this.musicCount > 0) {
		return this.musicPrice;
	}
	return 0;
}

Production.prototype.GetVoicePrice = function() {
	if (!this.isResVoice) {
		return this.voicePrice;
	} else {
		return 0;
	}
}


Production.prototype.GetTotalPrice = function() {
	return (this.GetBasicPrice()+this.GetMusicPrice()+this.GetVoicePrice()).toFixed(2);
}

function EditInfo(name, val, amount) {
	this.title = "";
	this.price = 0;
	this.value = "";
	if (name != undefined) {
		this.title = name;
	}
	if (amount != undefined) {
		this.price = amount;	
	}
	if (val != undefined) {
		this.value = val;
	}
}


/*
Production.prototype.SetScriptWords = function(val) {
	this.scriptWords = val;
}

Production.prototype.SetMusicWords = function(val) {
	this.musicWords = val;
}
*/


function rollOver(val) {
	var lnk = Get(val);
	lnk.childNodes[0].src="images/but_"+val+"_hover.gif";
}

function rollOut(val) {
	var lnk = Get(val);
	lnk.childNodes[0].src="images/but_"+val+".gif";
}













