//Controle de la page d'INSCRIPTION
function hidePaymentInfo() {
	document.getElementById('paymentInfo1').style.display = "none";
	document.getElementById('paymentInfo2').style.display = "none";
}
	
function displayPayment(control){
	document.getElementById(control).style.display = "";
	
	if ( document.getElementById(control).id == "paymentInfo1" ) {
		document.getElementById('paymentInfo1').style.display = "";
		document.getElementById('paymentInfo2').style.display = "none";
	}
	else {
		document.getElementById('paymentInfo1').style.display = "none";
		document.getElementById('paymentInfo2').style.display = "";
	}
}


//Controle de la page d'ADMINISTRATION
// ADMINISTRATION
function hideRow(hideButton) {
	document.getElementById(hideButton).disabled = true;
}

function activateNotPaid(){
	document.getElementById('bNotPaid').disabled = true;
	document.getElementById('bPaid').disabled = false;
	document.getElementById('bAll').disabled = false;
	document.getElementById('cmd').value = "bNotPaid";
	infoDisplay.submit();
}

function activatePaid(){
	document.getElementById('bNotPaid').disabled = false;
	document.getElementById('bPaid').disabled = true;
	document.getElementById('bAll').disabled = false;
	document.getElementById('cmd').value = "bPaid";
	infoDisplay.submit();
}

function activateAll(){
	document.getElementById('bNotPaid').disabled = false;
	document.getElementById('bPaid').disabled = false;
	document.getElementById('bAll').disabled = true;
	document.getElementById('cmd').value = "bAll";
	infoDisplay.submit();
}

function showRow(row, column, div) {
	document.getElementById(column).colSpan = "7" ;
	document.getElementById(column).width = "100%" ;
	document.getElementById(column).align = "center";
	if (navigator.appName == "Microsoft Internet Explorer") {
		if (document.getElementById(row).style.display == 'block') {
			document.getElementById(div).style.backgroundColor = "";
			document.getElementById(row).style.display = 'none'; 
		}
		else {	
			document.getElementById(div).style.backgroundColor = "#DCE4E8";	
			document.getElementById(row).style.display = "block";
		}
	} else {
		if (document.getElementById(row).style.display == 'table-row') {
			document.getElementById(div).style.backgroundColor = "";	
			document.getElementById(row).style.display = 'none'; 
		}
		else {
			document.getElementById(div).style.backgroundColor = "#DCE4E8";
			document.getElementById(row).style.display = "table-row";
		}
	}
}


//Controle de la page des comptes
// INFORMATIONS
function loadPage() {

	/* Disable la form des informations personnels */
	document.getElementById('modif').checked = false;
	document.getElementById('[content]').style.border = "1px solid red";
	document.getElementById('user_registration_email').disabled = true;
	document.getElementById('user_registration_email').style.color = "gray";
	document.getElementById('user_registration_password').disabled = true;
	document.getElementById('user_registration_password').style.color = "gray";
	document.getElementById('user_registration_confirm').disabled = true;
	document.getElementById('user_registration_confirm').style.color = "gray";
	document.getElementById('user_registration_fname').disabled = true;
	document.getElementById('user_registration_fname').style.color = "gray";
	document.getElementById('user_registration_lname').disabled = true;
	document.getElementById('user_registration_lname').style.color = "gray";
	document.getElementById('user_registration_company').disabled = true;
	document.getElementById('user_registration_company').style.color = "gray";
	document.getElementById('user_registration_address').disabled = true;
	document.getElementById('user_registration_address').style.color = "gray";
	document.getElementById('user_registration_city').disabled = true;
	document.getElementById('user_registration_city').style.color = "gray";
	document.getElementById('user_registration_country').disabled = true;
	document.getElementById('user_registration_country').style.color = "gray";
	document.getElementById('user_registration_province').disabled = true;
	document.getElementById('user_registration_province').style.color = "gray";
	document.getElementById('user_registration_postalCode').disabled = true;
	document.getElementById('user_registration_postalCode').style.color = "gray";
	document.getElementById('user_registration_phone').disabled = true;
	document.getElementById('user_registration_phone').style.color = "gray";
	document.getElementById('paymentMethods1').disabled = true;
	document.getElementById('user_registration_paymentInfo1').disabled = true;
	document.getElementById('user_registration_paymentInfo1').style.color = "gray";
	document.getElementById('paymentMethods2').disabled = true;
	document.getElementById('user_registration_paymentInfo2').disabled = true;
	document.getElementById('user_registration_paymentInfo2').style.color = "gray";
	document.getElementById('infoAction').style.visibility = "hidden";
	
	document.getElementById('addupload').disabled = true;
	document.getElementById('addupload').style.color = "red";
	
	disableWebsite();
}

function allowModif() {
	if (document.getElementById('modif').checked == true) {
		/* Enable la form des informations personnels */
		document.getElementById('[content]').style.border = "1px solid #BDC7CD";
		document.getElementById('user_registration_email').disabled = false;
		document.getElementById('user_registration_email').style.color = "black";
		document.getElementById('user_registration_password').disabled = false;
		document.getElementById('user_registration_password').style.color = "black";
		document.getElementById('user_registration_confirm').disabled = false;
		document.getElementById('user_registration_confirm').style.color = "black";
		document.getElementById('user_registration_fname').disabled = false;
		document.getElementById('user_registration_fname').style.color = "black";
		document.getElementById('user_registration_lname').disabled = false;
		document.getElementById('user_registration_lname').style.color = "black";
		document.getElementById('user_registration_company').disabled = false;
		document.getElementById('user_registration_company').style.color = "black";
		document.getElementById('user_registration_address').disabled = false;
		document.getElementById('user_registration_address').style.color = "black";
		document.getElementById('user_registration_city').disabled = false;
		document.getElementById('user_registration_city').style.color = "black";
		document.getElementById('user_registration_country').disabled = false;
		document.getElementById('user_registration_country').style.color = "black";
		document.getElementById('user_registration_province').disabled = false;
		document.getElementById('user_registration_province').style.color = "black";
		document.getElementById('user_registration_postalCode').disabled = false;
		document.getElementById('user_registration_postalCode').style.color = "black";
		document.getElementById('user_registration_phone').disabled = false;
		document.getElementById('user_registration_phone').style.color = "black";
		document.getElementById('paymentMethods1').disabled = false;
		document.getElementById('user_registration_paymentInfo1').disabled = false;
		document.getElementById('user_registration_paymentInfo1').style.color = "black";
		document.getElementById('paymentMethods2').disabled = false;
		document.getElementById('user_registration_paymentInfo2').disabled = false;
		document.getElementById('user_registration_paymentInfo2').style.color = "black";
		document.getElementById('infoAction').style.visibility = "visible";
		
		document.getElementById('addupload').disabled = false;
		document.getElementById('addupload').style.color = "#1A7E9B";
	
		enableWebsite();
	}
	else {
		window.location.href='compte_informations.php';
	}
}


// COMPTES STATISTIQUES + COMPTES ADMINS
//   STATISTIQUE
function hideDivPreselect() {
	document.getElementById('select').checked = true;
	document.getElementById('divPreselect').style.display = 'none';
	document.getElementById('cmd').value = 2;
	document.getElementById('divSelect').style.display = 'inline' ;
}

function hideDivSelect() {
	document.getElementById('preselect').checked = true;
	document.getElementById('divPreselect').style.display = 'inline';
	document.getElementById('cmd').value = 1;
	document.getElementById('divSelect').style.display = 'none' ;
}

function adjustPreselect() {
	if (document.getElementById('divPreselect').style.display == 'none') {
		document.getElementById('divPreselect').style.display = 'inline';
		document.getElementById('cmd').value = 1;
		document.getElementById('divSelect').style.display = 'none';
	}
}

function adjustSelect() {
	if (document.getElementById('divSelect').style.display == 'none') {
		document.getElementById('divPreselect').style.display = 'none';
		document.getElementById('divSelect').style.display = 'inline';
		document.getElementById('cmd').value = 2;
	}
}


function showUser(uid){
	document.getElementById('cmdUser').value = uid;
	document.statAffilie.submit();
}

function paypal(business, item_name, shipping, currency_code, returnValue, cancel_return, uid, row){
	var rowMat = row.split("_"); 
	var returnV = returnValue;
	
	document.getElementById('business').value = business;
	document.getElementById('item_name').value = item_name;
	document.getElementById('shipping').value = shipping;
	document.getElementById('currency_code').value = currency_code;
	document.getElementById('cancel_return').value = cancel_return;
	document.getElementById('amount').value = parseFloat(document.getElementById(rowMat['0']+'selectedPrice').innerHTML.replace(/ $/, ""));
	
	//GET THE "to_idcustomerorder" AFFILIATED MEMBER COMMISSION TO BE ABLE TO SET THIS COMMISSION TO PAID !
	var nbrComm = row + '_nbrComm';
	var iNbrComm = document.getElementById(nbrComm).value;
	
	var removedID = new Array();
	var id = "";
	
	var i = 0;
	var j = 0;
	while (i < iNbrComm)
		{
			i++;
			currentRow = row+'_'+i;
			
			if (document.getElementById(currentRow).checked == true){
				id = 'id_' + row + '_' + i;
				removedID[j] = document.getElementById(id).innerHTML;
				j++;
			}
		}

	var stringRemovedID = removedID.join("_");
	
	returnV = returnV + '&removedID=' + stringRemovedID;
	returnV = returnV + '#row' + uid;
	
	document.getElementById('return').value = returnV;
	
	// Unquote to Debug - Show the return URL with UID to remove and # of row to go
	//alert(document.getElementById('return').value);
	document.fPaypal.submit();
}

function setPaid(returnV, uid, row){
	//GET THE "to_idcustomerorder" AFFILIATED MEMBER COMMISSION TO BE ABLE TO SET THIS COMMISSION TO PAID !
	var nbrComm = row + '_nbrComm';
	var iNbrComm = document.getElementById(nbrComm).value;
	
	var removedID = new Array();
	var id = "";
	
	var i = 0;
	var j = 0;
	while (i < iNbrComm)
		{
			i++;
			currentRow = row+'_'+i;
			
			if (document.getElementById(currentRow).checked == true){
				id = 'id_' + row + '_' + i;
				removedID[j] = document.getElementById(id).innerHTML;
				j++;
			}
		}

	var stringRemovedID = removedID.join("_");
	
	returnV = returnV + '&removedID=' + stringRemovedID;
	returnV = returnV + '#row' + uid;
	
	window.location.href=returnV;
}

function selectAll(row){
	var rowMat = row.split("_"); 
	
	var tousClickCmd = row + '_tousClickCmd';
	var nbrComm = row + '_nbrComm';
	
	var priceVar;
	var priceTotal = parseFloat(document.getElementById(rowMat['0']+'selectedPrice').innerHTML.replace(/ $/, ""));
	var num;
	
	var iNbrComm = document.getElementById(nbrComm).value;
	
	if (document.getElementById(tousClickCmd).value == 0){
		document.getElementById(tousClickCmd).value = '1';
		var i = 0;
		while (i < iNbrComm)
			{
				i++;
				currentRow = row+'_'+i;
				
				if (document.getElementById(currentRow).checked == false){
					document.getElementById(currentRow).checked = true;
					priceVar = 'price_' + row + '_' + i;
					num = parseFloat(document.getElementById(priceVar).innerHTML);
					priceTotal = priceTotal + num;
				}
			}
	}
	else {
		document.getElementById(tousClickCmd).value = '0';
		var i = 0;
		while (i < iNbrComm)
			{
				i++;
				currentRow = row+'_'+i;

				if (document.getElementById(currentRow).checked == true){
					document.getElementById(currentRow).checked = false;
					priceVar = 'price_' + row + '_' + i;
					num = parseFloat(document.getElementById(priceVar).innerHTML);
					priceTotal = priceTotal - num;
				}
			}
	}
	
	/*var valueToAdd = parseFloat(document.getElementById(rowMat['0']+'selectedPrice').innerHTML.replace(/ $/, ""));
	var valueFinal = 0;
	valueFinal = priceTotal + valueToAdd;*/
	
	document.getElementById(rowMat['0']+'selectedPrice').innerHTML = priceTotal.toFixed(2);
}


var max = 0; // maximum # of attachments allowed
var currentUploads = 0; // current # of attachment sections on the web page
var nameWebsite = ''; // Name property for the Description Input field
var nameFile = ''; // Name property for the File Input field
var scrollPosVert = 0; // stores the current scroll position on the form
// for some reason when a div is taken out, the form
// will scroll to the top on both Firefox and IE

// SCROLL FUNCTIONS
function saveScrollPos(offset){
	scrollPosVert=(document.all)?document.body.scrollTop:window.pageYOffset-offset;
}

function setScrollPos(){
	window.scrollTo(0, scrollPosVert);
	setTimeout('window.scrollTo(0, scrollPosVert)',1);
}

// This function adds a new attachment section to the form
// It is called when the user clicks the "Attach a file" button...
// It takes three arguments:
// maxUploads - the maximum number of attachments allowed
// descFieldName - the field name for the Description Input field
// fileFieldName - the field name for the File Input field
function addUpload(maxUploads, websiteFieldName){
	currentUploads = parseInt(document.getElementById('nbrWebsite').value);
	nameWebsite=websiteFieldName;
	max = Number(maxUploads);
	currentUploads++;
	if (currentUploads>max) {return};

	if (currentUploads>0)
	document.getElementById('addupload').value='Ajouter un site';

	if (currentUploads==max) document.getElementById('addupload').style.visibility='hidden';

	// First, clone the hidden attachment section
	var newFields = document.getElementById('attachment').cloneNode(true);
	newFields.id = '';

	// Make the new attachments section visible
	newFields.style.display = 'block';

	// loop through tags in the new Attachment section
	// and set ID and NAME properties
	//
	// NOTE: the control names for the Description Input
	// field and the file input field are created
	// by appending the currentUploads variable
	// value to the nameFile and nameDesc values
	// respectively
	//
	// In terms of Xaraya, this means you'll need to name your
	// DD properties will need names like the following:
	// "AttachmentDesc1"
	// "AttachmentFile1"
	// "AttachmentDesc2"
	// "AttachmentFile2"
	// "AttachmentDesc3"
	// "AttachmentFile3"
	// et cetera...

	var newField = newFields.childNodes;

	for (var i=0;i<newField.length;i++){
		if (newField[i].name==nameWebsite){
				newField[i].id=nameWebsite+currentUploads;
				newField[i].name=nameWebsite+currentUploads;
		}
		if (newField[i].id=='dropcap'){
				newField[i].id='dropcap'+currentUploads;
				newField[i].childNodes[0].data=currentUploads;
		}
	}

	// Insert our new Attachment section into the Attachments Div
	// on the form...
	var insertHere = document.getElementById('attachmentmarker');
	insertHere.parentNode.insertBefore(newFields,insertHere);

	document.getElementById('nbrWebsite').value = document.getElementById('attachments').getElementsByTagName('input').length / 2; 
}

// This function removes an attachment from the form
// and updates the ID and Name properties of all other
// Attachment sections
function removeFile(container, item){
	currentUploads = parseInt(document.getElementById('nbrWebsite').value);
	// get the ID number of the upload section to remove
	var tmp = item.getElementsByTagName('input')[0];

	var basefieldname = '';
	var nameWebsite = 'user_registration_website';
	if (tmp.type=='text') basefieldname = nameWebsite;

	var iRemove=Number(tmp.id.substring(basefieldname.length, tmp.id.length));

	// Shift all INPUT field IDs and NAMEs down by one (for fields with a
	// higher ID than the one being removed)
	var x = document.getElementById('attachments').getElementsByTagName('input');
	for (i=0;i<x.length;i++){
		if (x[i].type=='text') basefieldname=nameWebsite;

		var iEdit = Number(x[i].id.substring(basefieldname.length, x[i].id.length));
		if (iEdit>iRemove){
			x[i].id=basefieldname+(iEdit-1);
			x[i].name=basefieldname+(iEdit-1);
		}
	}

	// Run through all the DropCap divs (the number to the right of the attachment
	// section) and update that number...
	x=document.getElementById('attachments').getElementsByTagName('div');
	for (i=0;i<x.length;i++){
		// Verify this is actually the "dropcap" div
		if (x[i].id.substring(0, String('dropcap').length)=='dropcap'){
			ID = Number(x[i].id.substring(String('dropcap').length, x[i].id.length));

			// check to see if current attachment had a higher ID than the one we're
			// removing (and thus needs to have its ID dropped)
			if (ID>iRemove){
				x[i].id='dropcap'+(ID-1);
				x[i].childNodes[0].data=(ID-1);
			}
		}
	}

	currentUploads--;
	saveScrollPos(0);
	container.removeChild(item);
	setScrollPos();
	document.getElementById('addupload').style.visibility='visible';
	if (currentUploads==0)
	document.getElementById('addupload').value='Ajouter un site';

	document.getElementById('nbrWebsite').value = document.getElementById('attachments').getElementsByTagName('input').length / 2; //DIVIDED BY 2 because there is 2 Input in the DIV "Attachment"
}

function recalculateTotal(row){
	var rowMat = row.split("_");
	
	var valueTotalCurrent = 0;
	var valueTotalCurrent = parseFloat(document.getElementById(rowMat['0']+'selectedPrice').innerHTML.replace(/ $/, ""));
	
	var valueTotalFinal = 0;
	
	var priceRow = 'price_' + row;
	
	if (document.getElementById(row).checked == true){
		valueTotalFinal = valueTotalCurrent + parseFloat(document.getElementById(priceRow).innerHTML);
		document.getElementById(rowMat['0']+'selectedPrice').innerHTML = valueTotalFinal.toFixed(2);
	}
	else if (document.getElementById(row).checked == false){
		valueTotalFinal = valueTotalCurrent - parseFloat(document.getElementById(priceRow).innerHTML);
		document.getElementById(rowMat['0']+'selectedPrice').innerHTML = valueTotalFinal.toFixed(2);
	}
}

