var expDays = 1;
var expd = new Date(); 
expd.setTime(expd.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset)
 {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) 
{  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) 
		{    
			var j = i + alen;    
			if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
			i = document.cookie.indexOf(" ", i) + 1;    
			if (i == 0) break;   
		}  
	return null;
}

function SetCookie (name, value) 
{  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : null;  
	var domain = (argc > 4) ? argv[4] : null;  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
}


function DeleteCookie (name) 
{  
	var expd = new Date();  
	expd.setTime (expd.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + expd.toGMTString();
}

function CookiesEnabled()
{
	SetCookie("FSBOCookieTest", "1", expd);
	if (GetCookie("PBCookieTest") != null)
		{
			DeleteCookie("FSBOCookieTest");
			if (navigator.appName !="Netscape")
			{
				if (navigator.cookieEnabled==true) return true; 
				else return false;
			}
			else return true;
		}
	else
		return false;		
}
	
function AlertCookieTest()
{
		var result;
		result = CookiesEnabled();
		if(!result) {
			alert("This section of the site requires that cookies are enabled.");
		}
}
/* opens a popup window */
function openPopupWindow(theURL,winName,features) { 
	var s = window.open(theURL,winName,features);
	if(s) {s.focus()};
}

function Manage(what,wname){
	displayWindow(what, wname, 650, 550, "resize=yes,scrollbars=yes,resizable=yes");
}

function OpenPopup(what,wname,width, height){
	displayWindow(what, wname, width, height, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowDescription(code,fid){
	displayWindow('plan_description.php?code='+code+'&fid='+fid, 'Description', 500, 300, "resize=yes,scrollbars=yes,resizable=yes");
}

function ShowWaitWindow(){
	displayWindow('common/wait.html', 'Wait', 300, 100, "resize=no,scrollbars=no,resizable=no");
}

function ShowAdminWaitWindow(){
	displayWindow('../common/wait.html', 'Wait', 300, 100, "resize=no,scrollbars=no,resizable=no");
}


function CloseWaitWindow()
{
 	if  (Wait != null)
 	{
 		if (Wait.opened)
 		{
 			Wait.close();
 		}
 	}	
}

function windowClose(){
	window.close()
}

function displayWindow(theURL,winName,intWidth,intHeight,features) {
    var window_width = intWidth;
    var window_height = intHeight;
    var newfeatures = features;
    var window_top = (screen.height-window_height)/2-20;
    var window_left = (screen.width-window_width)/2;
	newWindow = window.open(theURL,winName,''+'width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',' + newfeatures + '');
    newWindow.focus();
}

function OpCancel()
{
	window.close();
	return true;
}

function CloseWnd()
{
	if(opener != null)
		opener.location.href = opener.location.href;
	window.close();
}


function validatedelete()
{
	return confirm("Please confirm delete!");
}

function validate_delete_custom(message)
{
	return confirm(message);
}

function enableschedule(thisForm)
{
	if (sale2.house_is_opened.checked == true)
	{
                document.sale2.house_schedule.disabled = false;
	}
	else
	{
	        document.sale2.house_schedule.disabled = true;
	}
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function removeCurrency( strValue ) 
{
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function rewritePage() 
{
        var plan, price, rlength = 2;
        
              
        if (saving.price.length==0)
        {
                price = 0;
        }
        else if (!checknumber(removeCurrency(saving.price.value)))
        {
                alert('Invalid number!');
                saving.price.value = '';
                return;
        }
        else
        {
                price = removeCurrency(saving.price.value);
        }
        
        for (i=0; i<saving.paymentplan.length; i++)
        {
                if(saving.paymentplan[i].checked)
                {
                        plan = saving.paymentplan[i].value;
                }
        }
	saving.price.value=addSeparatorsNF(price, '.', '.', ',');
        saving.realtor.value=addSeparatorsNF(Math.round((price*0.06)*Math.pow(10,rlength))/Math.pow(10,rlength), '.', '.', ',');
        saving.savings.value=addSeparatorsNF(Math.round((price*0.06-plan)*Math.pow(10,rlength))/Math.pow(10,rlength), '.', '.', ',');
}

function ShowEmailForm(tip, id)
{
	displayWindow('business_contact.php?tip=' + tip + '&id=' + id, 'contact', 450, 300, "resize=yes,scrollbars=yes,resizable=yes");
}



function ShowMoneySave()
{
	displayWindow('money_save.php', 'money', 390, 230, "resize=yes,scrollbars=yes,resizable=yes");
}

function ChooseDate(prefix)
{
	var v = null;
	displayWindow("../../html_common/calendar.html?obj=" + prefix, "Calendar", 250, 250, "scrollbars=yes,menubar=no,resize=no");
}

function ChooseDateTmp(prefix)
{
	var v = null;
	displayWindow("../../html_common/calendar_tmp.html?obj=" + prefix, "Calendar", 250, 250, "scrollbars=yes,menubar=no,resize=no");
}

function UploadFile(control, id)
{
	displayWindow('upload.php?obj=' + control + '&id=' + id, 'upload', 400, 250, 'scrollbars=yes,menubar=no,resize=no');
}

function setcontrol(obj, value)
{
    	var myObj;
	if (window.opener)
	{
		myObj = window.opener.document.getElementById(obj);
		myObj.value = value;
	}
}

function edit(code, id, intWidth, intHeight)
{
	if (intWidth == null){
		intWidth = 400;
	}
	
	if (intHeight == null){
	intHeight = 400;
	} 
	displayWindow('edit.php?code=' + code + '&id=' + id, 'edit', intWidth, intHeight, 'scrollbars=yes,menubar=no,resize=no');
}

function add(id)
{
	displayWindow('add.php?id=' + id, 'edit', 300, 200, 'scrollbars=yes,menubar=no,resize=no');
}

function changepassword(id)
{
	displayWindow('changepassword.php?id=' + id, 'changepassword', 350, 230, 'scrollbars=yes,menubar=no,resize=no');
}

function uploadpictures(strPageUrl, id)
{
	displayWindow('uploadpictures.php?id=' + id, 'uploadpictures', 500, 350, 'scrollbars=yes,menubar=no,resize=no');
}

function viewpicture(picture)
{
	displayWindow( picture, 'viewpicture', 450, 320, 'scrollbars=yes,menubar=no,resize=no');
}

function refreshandclose()
{
        opener.location.reload();
        window.close();
}

function changeandclose(address)
{
        window.opener.location.href=address;        
        window.close();
}

var dom = document.getElementById ? true : false;
var all = document.all ? true : false;

function GetField(fieldID)
{
	if (dom || all)
	{
		return document.getElementById(fieldID);
	}
	return null;
}

function next(thisField, maxLength, target)
{
	if (thisField.value.length == maxLength)
	{
		var obj = GetField(target);
		if(obj) obj.focus();
	}
}

function setShippingInformations(strForm, strFirstName, strLastName, strPhone, strAddress, strCity, strZip, strState, strCountry, strOtherCity)
{
        if (document[strForm].same.checked)
        {
                document[strForm].shipping_first_name.value = strFirstName;
                document[strForm].shipping_last_name.value = strLastName;
                document[strForm].shipping_phone.value = strPhone;
                document[strForm].shipping_address.value = strAddress;
                document[strForm].shipping_city.value = strCity;
                document[strForm].shipping_zip.value = strZip;
                document[strForm].shipping_state.value = strState;
                document[strForm].shipping_country.value = strCountry;
        }
        else
        {
                document[strForm].shipping_first_name.value = '';
                document[strForm].shipping_last_name.value = '';
                document[strForm].shipping_phone.value = '';
                document[strForm].shipping_address.value = '';
                document[strForm].shipping_city.value = '';
                document[strForm].shipping_zip.value = '';
                document[strForm].shipping_state.value = '';
                document[strForm].shipping_country.value = '??';				      
        }
}

function copyFormFields(strForm)
{
	if (document[strForm].same.checked)
	{
                document[strForm].shipping_first_name.value = document[strForm].first_name.value;
                document[strForm].shipping_last_name.value = document[strForm].last_name.value;
                document[strForm].shipping_company.value = document[strForm].company.value;
                document[strForm].shipping_phone.value = document[strForm].phone.value;
                document[strForm].shipping_address.value = document[strForm].address.value;
                document[strForm].shipping_alternate_address.value = document[strForm].alternate_address.value;
                document[strForm].shipping_city.value = document[strForm].city.value;
                document[strForm].shipping_zip.value = document[strForm].zip.value;
                document[strForm].shipping_state.value = document[strForm].state.value;
                document[strForm].shipping_country.value = document[strForm].country.value;
                
                getShippingStates(document[strForm].country.value, document[strForm].state.value);
                getShippingCities(document[strForm].country.value, document[strForm].state.value, document[strForm].city.value);
                
                if (document[strForm].chkOtherCity.checked) {
					document[strForm].chkOtherShippingCity.checked = true;
					txt = document.getElementById('o_shipping_city');
					if (!txt) {
						toggleOtherShippingCity(document[strForm].chkOtherShippingCity);
					}
					txt.setAttribute('value', document[strForm].o_city.value);                	
                }
                else {
                	document[strForm].chkOtherShippingCity.checked = false;
                	toggleOtherShippingCity(document[strForm].chkOtherShippingCity);
                }
	}

	return;
}

// pt edit zone rates//
function displayAddForm()
{
	document.forms['ShippingRates']['max_weight'].value = '';
	document.forms['ShippingRates']['zone1'].value = '';
	document.forms['ShippingRates']['zone2'].value = '';
	document.forms['ShippingRates']['zone3'].value = '';
	document.forms['ShippingRates']['zone4'].value = '';
	document.getElementById('AddForm').style.display='block';
	return;
}

function showStates1(StateCode){
	if (StateCode.toUpperCase() == 'RO')
	{
		document.getElementById('ROStates').style.display = 'block';
		document.getElementById('USStates').style.display = 'none';
	}
	else
		if (StateCode.toUpperCase() == 'US')
		{
			document.getElementById('USStates').style.display = 'block';
			document.getElementById('ROStates').style.display = 'none';
		}
		else
		{
			document.getElementById('USStates').style.display = 'none';
			document.getElementById('ROStates').style.display = 'none';
		}
	return
}

/* Ajax 
	var request = makeObject();
	
	function makeObject(){
		var x;
		var browser = navigator.appName;
		if(browser == "Microsoft Internet Explorer"){
			x = new ActiveXObject("Microsoft.XMLHTTP");
		}
			else{
				x = new XMLHttpRequest();
			}
		return x;
	}
	
	function parseCitiesInfo(){
	if(request.readyState == 1){
	document.getElementById('cityBox').innerHTML = 'Loading...';
	}
	if(request.readyState == 4){
	
	var answer = request.responseText;
	
	document.getElementById('cityBox').innerHTML = answer;
	}
	}
	
	function parseShCitiesInfo(){
	if(request.readyState == 1){
	document.getElementById('shipping_cityBox').innerHTML = 'Loading...';
	}
	if(request.readyState == 4){
	
	var answer = request.responseText;
	
	document.getElementById('shipping_cityBox').innerHTML = answer;
	}
	}
	
	function parseStatesInfo(){
	if(request.readyState == 1){
	document.getElementById('stateBox').innerHTML = 'Loading...';
	}
	if(request.readyState == 4){
	
	var answer = request.responseText;
	
	document.getElementById('stateBox').innerHTML = answer;
	}
	}
	
	function parseShStatesInfo(){
	if(request.readyState == 1){
	document.getElementById('shipping_stateBox').innerHTML = 'Loading...';
	}
	if(request.readyState == 4){
	
	var answer = request.responseText;
	
	document.getElementById('shipping_stateBox').innerHTML = answer;
	}
	}
	
	function showCity(name, country, state){
		request.open('get', '../../location.php?country=' + country + '&state=' + state + '&name=' + name);
		
		request.onreadystatechange = parseCitiesInfo;
		
		request.send('');
	}
	
	function showShippingCity(name, country, state){
		request.open('get', '../../location.php?country=' + country + '&state=' + state + '&name=' + name);
		
		request.onreadystatechange = parseShCitiesInfo;
		
		request.send('');
	}
	
	function showState(name, country){
		request.open('get', '../../location.php?country=' + country + '&name=' + name);
		
		request.onreadystatechange = parseStatesInfo;
		
		request.send('');
	}
	
	function showShippingState(name, country){
		request.open('get', '../../location.php?country=' + country + '&name=' + name);
		
		request.onreadystatechange = parseShStatesInfo;
		
		request.send('');
	}

 /Ajax */

function toggleCompanyInfoView()
{
	if (document.forms['addeditclient']['is_company'][1].checked)
	{
		document.getElementById('persoana_fizica').style.display = 'none';
		document.getElementById('persoana_juridica').style.display = 'block';
	}
	else
	{
		document.getElementById('persoana_juridica').style.display = 'none';
		document.getElementById('persoana_fizica').style.display = 'block';
	}
}