var b_version = navigator.appVersion;
var isIE6 = b_version.search(/MSIE 6/i) != -1;
var isIE7 = b_version.search(/MSIE 7/i) != -1;
var isIE = b_version.search(/MSIE/i) != -1; 
var isSafari20 = b_version.search(/412.2.2/i) != -1;
var isMacintosh = b_version.search(/Macintosh/i) != -1;
var isSafari3 = b_version.search(/applewebkit\/525/i) != -1;
var isSafari = b_version.search(/applewebkit/i) != -1;
var isSafari2 = isSafari && !isSafari3;
var isFirefox = navigator.userAgent.search(/Firefox/i) !== -1;
var visulEffect;
var schmapme = {};
schmapme.maptype = "m";
schmapme.zoom = 15;
var encode;
if (typeof(encodeURIComponent) != "undefined") {
	encode = encodeURIComponent;
} else {
	encode = unescape;
}

var userUrl = '';
var sessionKey = '';
var addrEncoded = false;
var notesEncoded = false;
var faxEncoded = false;
var selectedIconIdx = 0;
function getMapType(maptype){
    schmapme.maptype = maptype.substr(0,1);
}

function clickselect(){
	this.select();
}

function getzoomlevel(zoom){
	schmapme.zoom = zoom;
}

function afterexpand() {
    showElement('faxText');
    var showmore = $('showmore');
    showmore.showed = !showmore.showed;
    if(showmore.showed){
        showmore.firstChild.innerHTML = schmapmeText.showLess;
        showmore.lastChild.src = "/images/showmore_m.png";
    }else{
        showmore.firstChild.innerHTML = schmapmeText.showMore;
        showmore.lastChild.src = "/images/showmore.png";
        hideElement('jobgroup');
        hideElement('phonegrouphidden');
    }
}

Visual = function () {
    this.interval = false;
    if(isSafari){
        this.noteBottom = 55;
    }else{
        this.noteBottom = 46;
    }
}

Visual.prototype.expandIt = function(setp, finalValue, secondfinal, timeStep, processOrder, func) {
   if(this.interval) return;
   this.step = setp;
   this.finalValue = finalValue;
   this.secondfinal = secondfinal;
   this.interval = setInterval(bind(scale,this),timeStep);
   this.after = func;
   this.distance = 0;
   this.secondProcess = 'off'
   this.whichprocess = processOrder;
   this.processOrder = processOrder;
   
}

function scale() {
    if(this.interval)
    var step = this.step;
    var finalValue = this.finalValue;
    var secondfinal = this.secondfinal
   
    var ph = $('phoneGroup').clientHeight;
    var h = $('contactDetail').clientHeight; 
    var jh = $('jobgroup').clientHeight; 
    var top = $('contactDetail').offsetTop;
    var bottom = this.noteBottom;
    this.distance += Math.abs(step);
    var flag  = step/Math.abs(step);
    if(this.whichprocess == 1){
        if(this.distance >= 50){
            if(flag == 1) jh = 58;
            else jh = 0;
            
            if(flag == 1){
                this.whichprocess = 2;
                this.distance =  step;
            }else {
                finalValue = 58;
            }
        }else {
            jh += step;
        }
        $('jobgroup').style.height = jh +'px';
    }
    if(this.whichprocess == 2){      
        if(this.distance > finalValue){
            top = top - (step - (this.distance - finalValue)*flag);
            h  = h   + (step - (this.distance - finalValue)*flag);
            ph  = ph  + (step - (this.distance - finalValue)*flag);
            bottom = bottom - (step - (this.distance - finalValue)*flag);
            if(flag == 1){
            }else { 
            this.whichprocess = 1;
            this.distance = step;
            finalValue = 58;
            }
        } else {
            top -= step;
            h += step;
            ph += step;
            bottom -= step;
        } 
        $('contactDetail').style.height = h +'px';
        $('contactDetail').style.top = top +'px';
        $('phoneGroup').style.height = ph +'px';
    }
    
    if(this.distance >= finalValue){
            clearInterval(this.interval);
            this.interval = false;
            this.after();
    } 


  
}

function showMore() {
    hideElement('faxText');
    if(!this.showed){
        showElement('jobgroup');
        showElement('phonegrouphidden');
        visulEffect.expandIt(9, 192, 18, 20, 1, afterexpand);
    }else {
        visulEffect.expandIt(-9, 192, 18, 20, 2, afterexpand);
    }
}

function getPhoneType(num) {
    var elemId = "p" + num + "type";
    var elemVal = $(elemId).value;
    return (elemVal.charAt(0));
}

function beforebuildpage(){   
    schmapme['lat'] =  window.frames['placeposition'].ipLat; 
    schmapme['long'] = window.frames['placeposition'].ipLong;
}

function satartType() {
    ctx = this;
     if(ctx.value !== '' && ctx.value !== ctx.defaultString){
        return;
    }
    ctx.value = '';
    ctx.style.color = '#797978';
    if(ctx.id == "tpwd" && !schmap.typed){
	     
		changeInputType('tpwd', 'password',true);
	}
}

function changeInputType(inputs,type,click){
	var input = $(inputs)
	if(isIE){
	var input2  = INPUT({id:inputs, 'type': type, value: input.value, 'spellcheck': 'false', 'class':'mandatoryfield'});
 	 input2.defaultString = tweetupText[inputs];
	input.parentNode.replaceChild(input2,input);
 	connect(input2, 'onfocus', satartType);
	connect(input2, 'onclick', satartType);
	connect(input2, 'onblur', endType);
	connect(input2, 'onchange', changeText);
	if(click){
	schmap.typed = true;
	callLater(0.01,function(){input2.focus()});
	
	}
	} else{
	    input.type = type;
	}
}

function endType() {
    var ctx = this;
    if(ctx.value == ''){
	    schmapme[this.id] = '';
        ctx.value = ctx.defaultString;
        ctx.style.color = '#bfbfbf';
	  if(ctx.id == "tpwd"){
	    if(ctx.type == 'password')
	    changeInputType('tpwd', 'text');
		schmap.typed = false;
	   }
	   if(ctx.id == "search"){
	       ctx.style.backgroundColor = '#FDFF92';
	   }
    }    
}
function addrPosition() { 
    window.frames['placeposition'].addrLocate(this.value,16); 
} 

function passGeoCode(geo){
    schmapme['lat'] = geo.lat();
    schmapme['long'] = geo.lng();
}

function changeText() { 
    var v = this.value.Trim();
    logDebug(v);
    if(v !== ''){
        if(this.id == 'webaddr' && !v.match(/<a.*>/)){
            v = 'http://' + v.replace('http://', '');
        }
    }
    schmapme[this.id] = encode(v);
    if (this.id == "addr") {
       addrEncoded = true;
    }
    else if (this.id == "notes") {
       notesEncoded = true;
    }
    else if (this.id == "fax") {
       faxEncoded = true;
    }
}

String.prototype.Trim = function () {   
    return   this.replace(/^\s*/g,"").replace(/\s*$/g,"");   
}
function MisterKeyPress(e) {
    if(e.key().code == 13 || e.key().code == 40){
        var s = this.scrollTop;
        this.scrollTop = s + 20; 
    } 
    if(e.key().code == 38){
        var s = this.scrollTop;
        this.scrollTop = s - 20; 
    }
}

function showSuccessScreen() {
    removeElement($('mainContainer'));
	logDebug('remove ');
    showElement('successScreen');
    var schmapme_url = $('schmapme-url');
    if (schmapme_url != null) {
        var host = document.location.host;
        var colon = host.indexOf(":");
        if (colon > 0) {
            host = host.substr (0, colon);
        }
        schmapme_url.href= 'http://' + host + '/' + userUrl;
        schmapme_url.innerHTML = host + '/' + userUrl;
        $('schmapmeURL').style.visibility = 'visible';
    }
}

function showGeneralErrorScreen() {
    removeElement($('mainContainer'));
    showElement('generalErrorScreen');
}  

function showIconsPopup(event) {
    var isVisible = $('IconsPopupDiv').style.visibility;
    if (isVisible != "visible") {
        $('IconsPopupDiv').style.visibility = "visible";
    }
    else {
        $('IconsPopupDiv').style.visibility = "hidden";
    }
    event.stopPropagation();
}

function hideIconsPopup() {
    if($('IconsPopupDiv'))
    $('IconsPopupDiv').style.visibility = "hidden";
}
