// HRISTO HRISTOV - Basic JS Scripts

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

function trigger_effect(src_element,div_id, effect, first_img, second_img){
		Effect.toggle(div_id, effect, {duration: 0.4, beforeFinish:function(){
		image = src_element.getElementsByTagName('img')[0];
		image.src = image.src.match(first_img) ? second_img : first_img;
		}});
		return true;
}

function trim ( string ) {
	var i, numFirstChar = -1, numLastChar = -1;
	for ( i = 0; i < string.length; i ++ ) {
		if ( string.substring ( i, i + 1 ) != ' ' ) {
			numFirstChar = i;
			break;
		}
	}
	if ( numFirstChar == -1 )
		return "";
	for ( i = string.length; i >= 0; i -- ) {
		if ( string.substring ( i - 1, i ) != ' ' ) {
			numLastChar = i;
			break;
		}
	}
	return string.substring ( numFirstChar, numLastChar );
}
function isEmail(email) {
	var valid_email_address = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return valid_email_address.test(email)
}
function isPhoneNumber(phone) {
	var valid_phone_number=/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/;
	return valid_phone_number.test(phone)
}


function pcase(str) {
	strlen = str.length
	jj = str.substring(0,1).toUpperCase()
	jj = jj + str.substring(1,strlen).toLowerCase()
	for (i = 2; i <= strlen; i++) {
		if (jj.charAt(i)==" ") {
			lefthalf = jj.substring(0,i+1)
			righthalf = jj.substring(i+1,strlen)
			righthalf = righthalf.substring(0,1).toUpperCase()+righthalf.substring(1,strlen)
			jj=lefthalf+righthalf
		}
	}
	return jj
}

function AutoResizeIFrm(ifrmobj) {
	if (document.getElementById) {
		var success = 0;
		var oFrame = ifrmobj; //document.getElementById("ifrm");
		if(oFrame.style.display=='none') {
			oFrame.style.height='0px';
			oFrame.style.display='inline';
		}
		var oLoading = document.getElementById('loading');
		if(oLoading) {
			oLoading.style.display='none';
		}
		// Mozilla solution
		if (oFrame.contentDocument) {
//			alert("id='" + oFrame.id + "' height='" + oFrame.contentDocument.height + "'");
			var height = oFrame.contentDocument.height;
//			if (height > 100) {
				oFrame.style.height = (height+2) + "px";
				success = 1;
//			}
			// IE solution
		} else if (navigator && navigator.userAgent && navigator.userAgent.indexOf("MSIE") != -1 && ifrmobj) {
			var oBody = ifrmobj.contentWindow.document.body;
			var height = oBody.scrollHeight + (oBody.offsetHeight - oBody.clientHeight);
//			alert("id='" + oFrame.id + "' height='" + ifrmobj.contentWindow.document.body.scrollHeight + "'");
//			if (height > 100) {
				oFrame.style.height = (height+2) + "px";
				success = 1;
//			}
		}
	}
	if(window.frameElement) {
		//alert("window.frameElement.id='"+window.frameElement.id+"'");
		parent.AutoResizeIFrm(window.frameElement);
	}
}

function clear_field(field,default_value) {
	if (field.title) {
		if (default_value=='reset') {
			if (field.value=='') {
				field.value=field.title;
			}
		} else {
			if (field.value==field.title) {
				field.value="";
			}
		}
	} else {
		if (field.value==default_value) {
			field.value="";
		}
	}
}

function validate_footer_form(obj_form) {
	obj_form.email.value=trim(obj_form.email.value)
	
	if (obj_form.email.value=="") {
		alert("Email is a required.");
		obj_form.email.focus();
		return false;
	} else if (!isEmail(obj_form.email.value)) {
		alert("Email must be a valid email address.");
		obj_form.email.focus();
		return false;
	}
	
	return true;
}

function hideShareBox() {
    document.getElementById('shareMenu').style.visibility = 'hidden';
}

function showShareBox() {
	document.getElementById('shareMenu').style.visibility = 'visible'; 
}

function facebookShare() {
    u = location.href;
    t = document.title.split('|');
    triggerShareOmnitureEvent('Facebook',t[0],'event21');
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

function friendFeedShare() {
    u = location.href;
    t = document.title.split('|');
    triggerShareOmnitureEvent('Friend Feed',t[0],'event22');
    window.open('http://friendfeed.com/?url=' + encodeURIComponent(u) + '&title=' + encodeURIComponent(t));
    return false;
}

function twitterShare(t) {
    u = location.href;
//    t = document.title.split('|');
    triggerShareOmnitureEvent('Twitter',t,'event20');
    window.open('http://twitter.com/home?status=' + encodeURIComponent(t) + ' ' + encodeURIComponent(u));
    return false;
}

function myspaceShare(imgURL) {
    u = location.href;
    t = document.title.split('|');
    /* NOT USING, PASSING ENTIRE PATH var imagePath = baseThumbURL + imgURL; */
    triggerShareOmnitureEvent('MySpace',t[0],'event23');
    imageTag = '<img src="' + imgURL + '" />';
    window.open('http://www.myspace.com/Modules/PostTo/Pages/?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t) + '&c=' + imageTag);
    return false;
}

function deliciousShare() {
    u = location.href;
    t = document.title.split('|');
    triggerShareOmnitureEvent('delicious',t[0],'event25');
    var shareURL = 'http://delicious.com/save?jump=yes&v=4&noui&url=' + encodeURIComponent(u)
			+ '&title=' + encodeURIComponent(t);
    window.open(shareURL);
    return false;
}

function diggShare(shareDescription) {
    u = location.href; //will not work with IP
    t = document.title.split('|');
    triggerShareOmnitureEvent('digg',t[0],'event24');
    var shareURL = 'http://digg.com/remote-submit?phase=2&url=' + encodeURIComponent(u)
			+ '&title=' + encodeURIComponent(t)
			+ '&bodytext=' + encodeURIComponent(shareDescription)
			+ '&topic=health';
    window.open(shareURL);
    return false;
}

function triggerShareOmnitureEvent(site, product, eventName) {
/*
    s.pagename = '1' + site;
    s.channel = 'Social Network Links';
    s.prop1 = product;
    s.prop2 = '';
    s.prop3 = '';
    s.events = eventName;

    s.linkTrackVars = 'events';
    s.linkTrackEvents = eventName;
    void (s.tl(this, 'o', 'Social Bookmark - ' + site + ' Link'))
*/
}

