// JavaScript Document
//New Admin Beta

//PRESETS
confirmImage1 = '../images/ok_16_hot.png'; //shows up when ajax returns successfully 
confirmImage2 = '../images/ok_16_hot.gif'; //shows up when ajax returns successfully for IE 6 

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------- CALL AND CALL BACK FUNCTIONS ---------------------------------------------
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// ----------- Check SubDomain Availability -------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function checkSubDomain(formAddress,proceed){
	document.getElementById('subSpinMsgContent').style.display = "block";
	//divHide('subAvailableMsg','subAvailableMsgContent','none');
	document.getElementById('subAvailableMsg').style.display = "none";
	//divHide('subNotAvailableMsg','subNotAvailableMsgContent','none');
	document.getElementById('subNotAvailableMsg').style.display = "none";
	document.getElementById('checkButton').disabled = true;
	document.getElementById('proceedButton').disabled = true;
	var Obj = {"sendingObj": {"method": "checkSubDomainCF", "subdomain": formAddress.subdomain.value, "proceed": proceed}};
	DWREngine._execute("manage_signup.cfc", null, 'checkSubDomainCF', Obj, checkSubDomain_Result);
}  
// call back function
function checkSubDomain_Result(ReceivingObj) {
	//ALL VARIABLES RECIEVED FROM SERVER ARE IN LOWER CASE - so change these vars to lower case
	if(!ReceivingObj.error){ //was there an error
		if(ReceivingObj.domainavailable == 1){
			divHide('subSpinMsgContent','','none');
			//document.getElementById('subAvailableMsgDomain').innerHTML = ReceivingObj.subdomain+'.ifp3.com';
			//slideTween('subAvailableMsg','subAvailableMsgContent',52);
			document.getElementById('subAvailableMsg').style.display = "block";
			document.getElementById('checkButton').disabled = false;
			//document.getElementById('proceedButton').disabled = false; 
			//domainIsValid = true;
		}else{
			divHide('subSpinMsgContent','','none');
			//document.getElementById('subNotAvailableMsgDomain').innerHTML = ReceivingObj.subdomain+'.ifp3.com';
			//slideTween('subNotAvailableMsg','subNotAvailableMsgContent',52);
			document.getElementById('subNotAvailableMsg').style.display = "block";
			document.getElementById('checkButton').disabled = false;
			//document.getElementById('proceedButton').disabled = false;
			//domainIsValid = false;
		}
	}else{ //there was an error
		alert(ReceivingObj.errormessage);
		divHide('subSpinMsg','subSpinMsgContent','none');
		document.getElementById('checkButton').disabled = false;
		document.getElementById('proceedButton').disabled = false;
	}
}


// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// ----------- Create Account -------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function createAccount(formAddress,subdomain){
	var Obj = {"sendingObj": {"method": "createAccountCF", "promocode": formAddress.promoCode.value, "name": formAddress.name.value, "companyname": formAddress.companyname.value, "username": formAddress.username.value, "email": formAddress.email.value, "password": formAddress.password.value, "activationcode": formAddress.activationcode.value, "subdomain": subdomain.value, "designName": formAddress.designName.value}};
	DWREngine._execute("manage_signup.cfc", null, 'createAccountCF', Obj, createAccount_Result);
}
// call back function
function createAccount_Result(ReceivingObj) {
	//ALL VARIABLES RECIEVED FROM SERVER ARE IN LOWER CASE - so change these vars to lower case
	if(!ReceivingObj.error){ //was there an error
		if(document.getElementById('signupErrorMsg').style.display != "none"){
			slideTween('signupErrorMsg','signupErrorMsgContent',60);
		}
		document.getElementById('createAccountSpinMsg').style.display = "none";
		document.getElementById('showAdminLink').style.display = "block";
		document.getElementById('adminLink').href = "http://admin.ifp3.com?LUN="+ReceivingObj.lun+"&LPW="+ReceivingObj.lpw;
		location.replace("http://admin.ifp3.com?LUN="+ReceivingObj.lun+"&LPW="+ReceivingObj.lpw);
	}else{ //there was an error
		var alertError = true;
		if(ReceivingObj.errormessage == "subdomainexists"){
			document.getElementById('subNotAvailableMsg').style.display = "block";
			alertError = false;
		}
		if(ReceivingObj.errormessage == "usernameexists"){
			document.getElementById('errorUsernameUnAval').style.display = "block";
			alertError = false;
		}
		if(ReceivingObj.errormessage == "promoerror"){
			document.getElementById('errorPromo').style.display = "block";
			alertError = false;
		}
		document.getElementById('signupErrorMsg').style.display = "none";
		document.getElementById('signupErrorMsgContent').style.display = "none";
		slideTween('signupErrorMsg','signupErrorMsgContent',60);
		window.scroll(0,200);
		if(alertError){
			alert(ReceivingObj.errormessage);
		}
		document.getElementById('submit').style.display = "block";
		document.getElementById('createAccountSpinMsg').style.display = "none";
	}
} 

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// ----------- Create Ticket -------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

function createTicket(formAddress){
	var Obj = {"sendingObj": {"method": "createTicketCF", "client_name": formAddress.Client_Name.value, "support_category": formAddress.Support_Category.value, "comments": formAddress.Comments.value, "client_email": formAddress.Client_Email.value}};
	DWREngine._execute("/company/manage_company.cfc", null, 'createTicketCF', Obj, createTicket_Result);
}
// call back function
function createTicket_Result(ReceivingObj) {
	//ALL VARIABLES RECIEVED FROM SERVER ARE IN LOWER CASE - so change these vars to lower case
	if(!ReceivingObj.error){ //was there an error
		//slideTween('contactFormWrap','contactFormWrapContent',600);
		document.getElementById('contactFormWrap').style.display = "none";
		document.getElementById('ticketCreatedWrap').style.display = "block";
		//slideTween('ticketCreatedWrap','ticketCreatedWrapContent',300);
	}else{ //there was an error
		alert(ReceivingObj.errormessage);
		document.getElementById('submit').style.display = "block";
		document.getElementById('createTicketSpinMsg').style.display = "none";
	}
}

// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// ----------- CHANGE A TEXT WINDOW VALUE ON A PAGE -------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//NOTE - try to set all sending Vars for AJAX to have no capitals - then you won't get hosed when they come back with no capitals.

function textWindowOnChange(formAddress,idToChange){
	divHide('updateAjax','','block');	
	var theForm = document.getElementById(formAddress);
	var isOn =  theForm.textOnSelect.options[theForm.textOnSelect.selectedIndex].value;
	var Obj = {"sendingObj": {"method": "pageRename", "ison": isOn, "idtochange": idToChange, "appendelement": formAddress} };
	//make the call   (location of cfc file, null, CF function name, sending obj, callback function) 
	DWREngine._execute("ajaxPages.cfc", null, 'textWindowOnCF', Obj, textWindowOn_Result);
}

// call back function
function textWindowOn_Result (ReceivingObj) {
	if(!ReceivingObj.error){ //was there an error
		divHide('','updateAjax','block');
		ajaxVisualConfirm(ReceivingObj.appendelement);
	}else{ //there was an error
		divHide('','updateAjax','block');
		alert(ReceivingObj.errormessage);
	}
}
// Appends dom to show confirmation next to element that was updated
function ajaxVisualConfirm(appendElement){
	var img = document.createElement("img");
	if(!window.XMLHttpRequest){
		img.src = confirmImage2;
		img.className = 'ajaxCheckIE6';
	}else{
		img.src = confirmImage1;
	}
	document.getElementById(appendElement).appendChild(img);
}


// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------- MANIPULATION FUNCTIONS ---------------------------------------------
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function loadNewPage(choices,loadLocation,pageName){
	switchOnOff(choices,"unhide");
	ajaxpage(pageName, loadLocation);
}

// WH 9/13/07: this chages the 'more' link to close and visa versa
function changeMoreLink(theID){
	document.getElementById(theID).innerHTML = (document.getElementById(theID).innerHTML != 'close') ? 'close' : 'more...';
}

//allows divs to be hidden and revealed
function divHide(on, off, style) {
	if (style != "" && on != "") {
		styles = style.split(",");
		ons = on.split(",");
		for (i=0;i<ons.length;i++) {	
			document.getElementById(ons[i]).style.display = styles[i];
		}
	}
	if (off != "") {
		offs = off.split(",");
		for (i=0;i<offs.length;i++) {		
			document.getElementById(offs[i]).style.display = "none";
		}
	}
}

//Not using this at the moment - will be used to grab and change form element info
function setUpRename(choices,useScrollHiding,rowIDToChange,pageName,pageID) {
	switchOnOff(choices,useScrollHiding);
	document.getElementById('renameInput').value = pageName;
	document.getElementById('renameID').value = pageID;
}

//when using hide and unhide var values you will make it use scroll hiding. Sometimes you don't want this so don't include it.
function switchOnOff(choices,useScrollHiding) {
		if(useScrollHiding == "unhide"){
			saveScrollCoordinates();
		}
		var choice = choices.split(",");
		for (i=0;i<choice.length;i++) {
			if(!document.getElementById(choice[i]).offsetWidth) {
				document.getElementById(choice[i]).style.display = "block";
				if (document.documentElement.scrollTop) {
					toTop = document.documentElement.scrollTop;
				}
				else {
					toTop = document.body.scrollTop;
				}
				if (document.getElementById(choice[i]).offsetHeight+findposY(document.getElementById(choice[i])) > toTop+screenHeight()) {
					scrollY = document.getElementById(choice[i]).offsetHeight+findposY(document.getElementById(choice[i]))-screenHeight();
					window.scrollTo(0, scrollY);
				}
			}
			else {
				document.getElementById(choice[i]).style.display = "none";
			}
		}
		if(useScrollHiding == "hide"){
			scrollToCoordinates();
		}
}

scrollY1 = 0 ;//preset
//allows the window to scroll back to the point the user was at before running the function 
function scrollToCoordinates() {
  window.scrollTo(0, scrollY1);
  //alert("scrolling to y = "+scrollY1);
}
function saveScrollCoordinates() {
  if(document.all){
	 scrollY = document.documentElement.scrollTop;
  }else if(document.body && document.body.scrollTop){ //DOM compliant
	 scrollY = document.body.scrollTop;
  }else{ //mozilla engines
	  scrollY = window.pageYOffset;
  }
  scrollY1 = scrollY;
 // alert("Saving y = "+scrollY1 + "  document.body.scrollTop = "+document.body.scrollTop);
}

function findposY(obj) {
	var curtop = 0;
	curtop += parseInt(getCSSProp(obj,"border-top-width"));
	padding = parseInt(getCSSProp(obj,"padding-top"));
	if (padding) {
		curtop += padding;
	}
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
		else if(obj.y)
			curtop += obj.y;
	return curtop;
}

function getCSSProp(element, prop) {
	if (element.style[prop]) {
		// inline style property
		return element.style[prop];
	} else if (element.currentStyle) {
		// external stylesheet for Explorer
		return element.currentStyle[prop];
	} else if (document.defaultView && document.defaultView.getComputedStyle) {
		// external stylesheet for Mozilla and Safari 1.3+
		prop = prop.replace(/([A-Z])/g,"-$1");
		prop = prop.toLowerCase();
		return document.defaultView.getComputedStyle(element,"").getPropertyValue(prop);
	} else {
		// Safari 1.2
		return null;
	}
}

function screenWidth() {
	if (window.innerWidth) {
		return window.innerWidth;
	}
	else  if (document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	}
	else {
		return document.body.clientWidth;
	}
}
function screenHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	}
	else  if (document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	}
	else {
		return document.body.clientHeight;
	}
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ----------------------------------------- TWEEN CONTROLS ---------------------------------------------
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function slideTween(panelDiv,contentDiv,panelHeight){
	if(document.getElementById(panelDiv).style.display == "none"){ 
		switchOnOff(panelDiv);
		var t1 = new Tween(document.getElementById(panelDiv).style,'height',Tween.strongEaseOut,0,panelHeight,.7,'px');
		t1.onMotionFinished = function(){
			//alert( 'onMotionFinished' );
			switchOnOff(contentDiv);
			document.getElementById(panelDiv).style.height ='auto'; //let this lay where is should lay not what we forced it ot - will then allow the box to naturally grow when othere elements expand within it
		};
		t1.start();
	}else{
		switchOnOff(contentDiv);
		var t2 = new Tween(document.getElementById(panelDiv).style,'height',Tween.strongEaseIn,panelHeight,0,.2,'px');
		t2.onMotionFinished = function(){
			//alert( 'onMotionFinished' );
			switchOnOff(panelDiv);
		};
		t2.start();
	}
}

//extra stuff
//get viewable window size - redundant right now
function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return; 
  window.alert( 'Width = ' + myWidth );
  window.alert( 'Height = ' + myHeight );
}

 // AC 8/14/07: disable submit button
 function disableButton(id){
	document.getElementById(id).disabled = true;
	document.getElementById(id).style.color= "#555555";
	document.getElementById(id).style.backgroundcolor= "#555555";
	document.getElementById(id).style.border= "1px solid #555555";
 }
 
 // AC 8/14/07: disable submit button
  function enableButton(id){
	document.getElementById(id).disabled = false;
	document.getElementById(id).style.color= "#ffffff";
	document.getElementById(id).style.backgroundcolor= "#FFCC33";
	document.getElementById(id).style.border= "1px solid #FFCC33";
 }