// var _globalDocRoot = '/~buddiLive/';
var _globalDocRoot = '/';
function checkOfferFrmFld(){
	var msg = "";	
	if(document.offerForm.oName.value == ''){
		msg +="Name\n";
		document.offerForm.oName.focus();
	}
			
	if(document.offerForm.oEmail.value==''){
		msg +="Email\n";
		document.offerForm.oEmail.focus();
	}
	
	if (checkEmail(document.offerForm.oEmail.value)==false) {
		msg +="Enter a valid email id.\n";
		document.offerForm.oEmail.focus();
	}
	
	if(document.offerForm.oTelNo.value==''){
		msg +="Telephone No.\n";
		document.offerForm.oTelNo.focus();
	}
			
	if(msg != "") {
		msg = "The following mandatory fields have not been filled\n\n" + msg;
	}							
								
	if(msg != "") {
		alert(msg);
		return false;
	}else{
		return (true);
	}
	return false;
}

$(document).ready(function() {
	$('#offerClose1,#offerClose2').click(function(e){
		
		var ajaxVar = new ajaxVars();
		ajaxVar.incModule = 'popupForm';
		ajaxVar.vars['action'] = "runFunction";
		ajaxVar.vars['func'] = "updateOfferDB";	
		ajaxVar.vars['nosplit'] = 1;	
		ajaxVar.AjaxPostSQL();
		
		e.preventDefault(); // Prevent the hash link from jumping up to the page
		// Fade out the popup
		$('#offerFormDiv').fadeOut(500);
	});
	
	$('#offerProceed').click(function(e){
		
		if(checkOfferFrmFld()){
			var docObj = document;
			var objName = docObj.getElementById('oName');
			var objEmail = docObj.getElementById('oEmail');
			var objTelNo = docObj.getElementById('oTelNo');
			var objAddress = docObj.getElementById('oAddress');
			var objComments = docObj.getElementById('oComments');
			
			var ajaxVar = new ajaxVars();
			ajaxVar.incModule = 'popupForm';
			ajaxVar.vars['oName'] = objName.value;
			ajaxVar.vars['oEmail'] = objEmail.value;
			ajaxVar.vars['oTelNo'] = objTelNo.value;
			ajaxVar.vars['oAddress'] = objAddress.value;
			ajaxVar.vars['oComments'] = objComments.value;
			ajaxVar.vars['action'] = "runFunction";
			ajaxVar.vars['func'] = "saveOfferDB";	
			ajaxVar.vars['nosplit'] = 1;	
			ajaxVar.AjaxPostSQL();
			
			e.preventDefault(); // Prevent the hash link from jumping up to the page
			// Fade out the popup
			$('#offerFormDiv').fadeOut(500);
		}
		
	});
});



function check_popup(){
	
	document.getElementById('offerFormDiv').style.visibility='hidden';
	document.getElementById('offerFormDiv').style.display='none';
	
	var ajaxVar = new ajaxVars();
	ajaxVar.incModule = 'popupForm';
	ajaxVar.vars['action'] = "runFunction";
	ajaxVar.vars['func'] = "fetchOfferDB";	
	ajaxVar.gotoFunction = 'fnpopupStatus';
	ajaxVar.session = 0;
	ajaxVar.AjaxPostSQL();
}

function fnpopupStatus(data){
	if(data == 1){
		setTimeout("view_popup()",20000);
	}
}

function view_popup(){
	// Get the width and height of the popup
	var height = $('#offerFormDiv').height();
	var width = $('#offerFormDiv').width();
	
	// Calculate left and top positions for the popup
	var leftPos = (width / 2)+ 'px';
	var topPos = (height / 2)+ 'px';
	
	// Show the popup
	$('#offerFormDiv').css({left:leftPos, top:topPos}).show();
	
	DisplayCenter('offerFormDiv');
}

function DisplayCenter(DivId){
	var DivObj = document.getElementById(DivId);	
	var bodyObj = (navigator.userAgent.indexOf('Chrome')>-1)?document.body:document.documentElement;
	var winH = bodyObj.offsetHeight;
	var winW = bodyObj.offsetWidth;
	var FormW = DivObj.offsetWidth;
	var FormH = DivObj.offsetHeight;
	DivObj.style.position='absolute';
	DivObj.style.visibility='visible';
	DivObj.style.display='block';
	DivObj.style.left= (parseInt(winW)/2)- (parseInt(FormW)/2)+'px';
}

function getInnerPageSize(){  // Function to get actual screen width and height
	var gInnerPageWidth;
	var gInnerPageHeight;
	if(self.scrollHeight){ // Standards compliant browsers
		gInnerPageWidth = self.scrollWidth;
		gInnerPageHeight = self.scrollHeight;
	}else if(document.documentElement && document.documentElement.scrollHeight){ // IE6
		gInnerPageWidth = document.documentElement.scrollWidth;
		gInnerPageHeight = document.documentElement.scrollHeight;
	}else if(document.body){ // Older versions of IE
		gInnerPageWidth = document.body.scrollWidth;
		gInnerPageHeight = document.body.scrollHeight;
	}
	return [gInnerPageWidth,gInnerPageHeight];
}

//function to check the email
function checkEmail(fieldValue){
	var chkAt = fieldValue.indexOf("@");
	var chkDot= fieldValue.indexOf(".");
	var chkHack= fieldValue.indexOf("@.");
	var chkHackA= fieldValue.indexOf(".@");

	if((chkAt >= 0) && (chkDot >= 0) && (chkHack == -1) && (chkHackA == -1) ) {
		return true;
	}
	return false;
}

