var ajaxObjects = new Array();

function wholesaleRegister(path){

	var objResult = document.getElementById("semregister");
	objResult.innerHTML = '<img src="' + path + '/site/loading2.gif" border="0">';

	var serverSideFile = path + "/include/request.php";
	var ajaxIndex = ajaxObjects.length;

	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = serverSideFile;
	ajaxObjects[ajaxIndex].setVar('firstname',document.getElementById("firstname").value);
	ajaxObjects[ajaxIndex].setVar('lastname',document.getElementById("lastname").value);
	ajaxObjects[ajaxIndex].setVar('businessname',document.getElementById("businessname").value);
	ajaxObjects[ajaxIndex].setVar('title',document.getElementById("title").value);
	ajaxObjects[ajaxIndex].setVar('street',document.getElementById("street").value);
	ajaxObjects[ajaxIndex].setVar('city',document.getElementById("city").value);
	ajaxObjects[ajaxIndex].setVar('state',document.getElementById("state").value);
	ajaxObjects[ajaxIndex].setVar('zip',document.getElementById("zip").value);
	ajaxObjects[ajaxIndex].setVar('phone',document.getElementById("phone").value);
	ajaxObjects[ajaxIndex].setVar('email',document.getElementById("email").value);
	ajaxObjects[ajaxIndex].setVar('command',"register");
	ajaxObjects[ajaxIndex].onCompletion = function(){ wholesaleRegisterResult(ajaxIndex,objResult,path); };
	ajaxObjects[ajaxIndex].runAJAX();
}

function wholesaleRegisterResult(ajaxIndex,objResult,path){

	var request = ajaxObjects[ajaxIndex].response;

	if(request != 0){
		objResult.innerHTML = '<font color="#FFFFFF">Thank you for submition.</font>';
		setTimeout('showRegistration()', 3000);
	}else{
		objResult.innerHTML = '<font color="#C20102">You have to fill all fields correctly.</font>';
		setTimeout('restoreButton("' + path + '",0)', 3000);
	}

	ajaxObjects[ajaxIndex] = false;
}

function restoreButton(path,fl){
	var objResult = document.getElementById("semregister");
	objResult.innerHTML = '<div class="registerBtn" onclick="wholesaleRegister(\'' + path + '\');return false;">SUBMIT</div>';
	if(fl == 1)
		document.wholesaleForm.reset();
}

var popupStatus = 0;

function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus == 0){
		$("#backgroundPopup").css({
		"opacity": "0.7"
		});

		$("#backgroundPopup").fadeIn("slow");
		$("#popupContent").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContent").fadeOut("slow");

		$("#contentTitle").text(" ");
		$("#contentArea").text(" ");

		popupStatus = 0;
	}
}

function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContent").height();
	var popupWidth = $("#popupContent").width();

	//centering
	$("#popupContent").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});

	//only need force for IE6

	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

function showRegistration(){
	$("#wholesaleRegistration").toggle("slow");
}
