﻿// JavaScript Document

function switchAuctionDiv() {
	document.getElementById('i_encheres').style.display='none';
	document.getElementById('i_encheres_2').style.display='block';
}

function newCfwindow(nameWin,titleWin,sourceWin,heightWin,widthWin,isClosable) {
	if(isClosable==''){isClosable='true';}
	ColdFusion.Window.create(nameWin, titleWin, sourceWin, {height:heightWin,width:widthWin,modal:true,closable:isClosable,draggable:true,resizable:false,center:true,initshow:true});
	ColdFusion.Window.onHide(nameWin, cfwindowOnHide );
	return true;
}
function cfwindowOnHide(name) {
	ColdFusion.Window.destroy(name,true);
	return true;
}

function loginLocation(formName,basketAction) {
	if(formName=='newAccountForm') {
		window.top.location="/boutique/index.cfm?basketAction="+escape(basketAction);
		ColdFusion.Window.destroy('loginScreen',true);
	}
	if(formName=='loginForm') {
		ColdFusion.Ajax.submitForm(formName,'/_cfc/cBasket.cfc?method=loginAccountCFwin',successLogin,errorResponse,'POST');
	}
}
function errorResponse(text) {alert(text);}
function successLogin(text) {
	var decodeText=text;
	var stringToTest="http://";
	var pos=decodeText.match(stringToTest);
	if(pos != stringToTest) {alert(decodeText);}
	else {
		window.top.location=decodeText;
		ColdFusion.Window.destroy('loginScreen',true);
	}
}

function postForm(formName,lang,isFromCompteSpace) {
	var result = new Object();
	var cBasket = new _cfc.cBasket();
	var cwindowName='';
	cBasket.setHTTPMethod("post");
	cBasket.setForm(formName);
	switch (formName) { 
		case 'formEncherir':
			result=cBasket.overbid();
			alert(result.MESSAGE);
			if(result.ERRORNUM==0 && isFromCompteSpace=='false') {
				$('#i_encheres_2').load('/'+lang+'/i_encheres_2.cfm?idstock='+result.IDMACHINE+'&amp;cache='+Math.random());
			} 
			else {
				if(result.ERRORNUM==0 && isFromCompteSpace=='true') {
					ColdFusion.Window.destroy('overbidWindows',true);
					refresh();
				}
			}
			break;
		case 'formMachinesSuivies':
			switch (lang) {
				case 'fr':
					cwindowName='Identification';
					break;
				case 'gb':
					cwindowName='Sign in';
					break;
				case 'sp':
					cwindowName='Identificación';
					break;
			}
			result=cBasket.addToMyFollowedMachines();
			if(result.ERRORNUM!=2) {alert(result.MESSAGE);}
			if(result.ERRORNUM==0) {refresh();}
			if(result.ERRORNUM==2) {newCfwindow('loginScreen',cwindowName,'/boutique/cfwin_identityLoginForm.cfm','265','550');}
			break;
		case 'formDeleteMachinesSuivies':
			result=cBasket.deleteFromMyFollowedMachines();
			alert(result.MESSAGE);
			if(result.ERRORNUM==0) {refresh();}
			break;
	}
	return true;
}
function confirmAuction(lang,isFromCompteSpace) {
	var phrase='';
	var price=document.getElementById('price').value;
	switch (lang) { 
		case 'fr':
			phrase='Confirmez-vous la somme maximum de '+price+',00 € pour cette enchère ?';
			break;
		case 'gb':
			phrase='Do you confirm your maximum bid of € '+price+'.00 for this machine?';
			break;
		case 'sp':
			phrase='¿ Confirma la suma máxima de '+price+',00 € para esta puja?';
			break;
	}
	if(confirm(phrase)){postForm('formEncherir',lang,isFromCompteSpace)};
	return true;
}

function addInFollowedMachines(lang,id) {
	var phrase='';
	switch (lang) { 
		case 'fr':
			phrase='Souhaitez-vous ajouter cette machine dans la liste de vos machines suivies ?';
			break;
		case 'gb':
			phrase='Do you want to add this machine in your followed machines list?';
			break;
		case 'sp':
			phrase='¿ Desea añadir esta máquina en la lista de sus máquinas seguidas?';
			break;
	}
	if(confirm(phrase)){
		if(id!='') {
			document.getElementById('idMachine').value=id;
		}
		postForm('formMachinesSuivies',lang);
	}
	return true;
}

function refresh() {
	window.location.reload( false );
}

function deleteFM(lang,idMachine) {
	var phrase='';
	switch (lang) { 
		case 'fr':
			phrase='Souhaitez-vous supprimer cette machine de la liste de vos machines suivies ?';
			break;
		case 'gb':
			phrase='Do you want to delete this machine from your followed machine list?';
			break;
		case 'sp':
			phrase='¿ Desea suprimir esta máquina de la lista de sus máquinas seguidas?';
			break;
	}
	if(confirm(phrase)){
		document.getElementById('idMachine').value=idMachine;
		postForm('formDeleteMachinesSuivies',lang);
	}
	return true;
}
