﻿function format_check (object, format) {

//  if (object.value != "") {
    var p = new RegExp(format);

    if (!p.test(object.value)) {
      object.className = "error";
      return false;
    }
    else {
      object.className = "form";
      return true;
    }
//  }
}

function checked_check (object) {
    if (!object.checked) {
      object.className = "error";
      return false;
    }
    else {
      object.className = "form";
      return true;
    }
}

function cross_check (object, name) {

  var res = "", l = ["input", "textarea"];

  if (object.value != "") {
    for (var i = 0; i < l.length; i++) {
      var elements = document.getElementsByTagName(l[i]);
      for (var j = 0; j < elements.length; j++) {
        var e = elements[j];
        if (e.name == name) {
          if (object.value != e.value) {
            object.className = "error";
            return false;
          }
          else {
            object.className = e.className;
            return true;
          }
        }
      }
    }
  }
}

function validateform (form, err_message) {

  var res = "", l = ["input", "textarea"];

  for (var i = 0; i < l.length; i++) {

    var els = form.getElementsByTagName(l[i]);

    for (var j = 0; j < els.length; j++) {

      var e = els[j];

      if (e.onblur) {
        if (!e.onblur()) {
          if (e.attributes.getNamedItem("label") != null)
            res += "- " + e.attributes.getNamedItem("label").value + "\n";
          else
            res += "- " + "-" + "\n";
        }
      }
    }
  }

  if (res != "") {
    alert(err_message + ": \n" + res);
    return false;
  }

  return true;
}

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_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 showimg (imgpath, target)
{
	var i = document.getElementById(target);

	if (i) i.src = imgpath;
}

function slideshow (imgarray, target, time)
{
  var a = imgarray.split("|");
  var i = a.shift();
	var t = document.getElementById(target);
  var n = imgarray;

  if (t)
  {
    if (t.onload != null)
    {
      t.onload = null;
    } else {
//      alert("---"+i+"---"+t.id+"---");
      ssw.transition(t.id, t, i, '1', '');
    }
    n = a.join("|") + "|" + i;
  }
  setTimeout ("slideshow ('" + n + "', '" + target + "', " + time + ")", time);
}

// Cross fade object
var ssw = {
  'clock'      : new Array(),
  'count'      : new Array(),
  'obj'        : new Array(),
  'src'        : new Array(),
  'type'       : new Array(),
  'newimg'     : new Array(),
  'length'     : new Array(),
  'resolution' : new Array()
};

//transition setup function
ssw.transition = function (object_id, target_obj, swap_src, interval, alt_tag) {
	//if the timer is not already going
	if(ssw.clock[object_id] == undefined) ssw.clock[object_id] = null;

  if(ssw.clock[object_id] == null) {
		//reset the counter
    ssw.count[object_id] = 1;

		//copy the target image object
		ssw.obj[object_id] = target_obj;

		//copy the image src argument
		ssw.src[object_id] = swap_src;

		//store the supported form of opacity
		if(typeof ssw.obj[object_id].style.opacity != 'undefined') {
			ssw.type[object_id] = 'w3c';
		} else if(typeof ssw.obj[object_id].style.MozOpacity != 'undefined') {
			ssw.type[object_id] = 'moz';
		} else if(typeof ssw.obj[object_id].style.KhtmlOpacity != 'undefined') {
			ssw.type[object_id] = 'khtml';
		} else if(typeof ssw.obj[object_id].filters == 'object') {
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			ssw.type[object_id] = (ssw.obj[object_id].filters.length > 0 && typeof ssw.obj[object_id].filters.alpha == 'object' && typeof ssw.obj[object_id].filters.alpha.opacity == 'number') ? 'ie' : 'none';
		} else {
			ssw.type[object_id] = 'none';
		}

		//change the image alt text if defined
		if(typeof alt_tag != 'undefined' && alt_tag != '') {
			ssw.obj[object_id].alt = alt_tag;
		}

		//if any kind of opacity is supported
		if(ssw.type[object_id] != 'none') {
    
      if(ssw.newimg[object_id] != 'undefined' && ssw.newimg[object_id] != null)
        ssw.newimg[object_id].parentNode.removeChild(ssw.newimg[object_id]);

			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			ssw.newimg[object_id] = (typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img');

			//set positioning classname
			ssw.newimg[object_id].className = 'idupe';

			//set src to new image src
			ssw.newimg[object_id].src = ssw.src[object_id];

      //append new img to the document
      document.getElementsByTagName('body')[0].appendChild(ssw.newimg[object_id]);

			//move it to superimpose original image
			ssw.newimg[object_id].style.left = ssw.getRealPosition(ssw.obj[object_id], 'x') + 'px';
			ssw.newimg[object_id].style.top = ssw.getRealPosition(ssw.obj[object_id], 'y') + 'px';

			//copy and convert fade duration argument 
			ssw.length[object_id] = parseInt(interval, 10) * 1000;

			//create fade resolution argument as 20 steps per transition
			ssw.resolution[object_id] = parseInt(interval, 10) * 20;

			//start the timer
			ssw.clock[object_id] = setInterval('ssw.crossfade("' + object_id + '")', ssw.length[object_id]/ssw.resolution[object_id]);
		}

		//otherwise if opacity is not supported
		else {
			//just do the image swap
			ssw.obj[object_id].src = ssw.src[object_id];
		}
	}
};

//crossfade timer function
ssw.crossfade = function(object_id) {
	//decrease the counter on a linear scale
	ssw.count[object_id] -= (1 / ssw.resolution[object_id]);

	//if the counter has reached the bottom
	if(ssw.count[object_id] < (1 / ssw.resolution[object_id])) {
		//clear the timer
		clearInterval(ssw.clock[object_id]);
		ssw.clock[object_id] = null;

		//reset the counter
		ssw.count[object_id] = 1;

		//set the original image to the src of the new image
		ssw.obj[object_id].src = ssw.src[object_id];
	} else {
    //set new opacity value on both elements using whatever method is supported
//  ssw.setOpacity (ssw.type[object_id], ssw.obj[object_id], ssw.count[object_id]);
    ssw.setOpacity (ssw.type[object_id], ssw.newimg[object_id], (1 - ssw.count[object_id]));

    //now that we've gone through one fade iteration  we can show the image that's fading in
    ssw.newimg[object_id].style.visibility = 'visible';

    //keep new image in position with original image in case text size changes mid transition or something
    ssw.newimg[object_id].style.left = ssw.getRealPosition(ssw.obj[object_id], 'x') + 'px';
    ssw.newimg[object_id].style.top = ssw.getRealPosition(ssw.obj[object_id], 'y') + 'px';
  }
	//if the counter is at the top, which is just after the timer has finished
	if(ssw.count[object_id] == 1) {
		//remove the duplicate image
//		ssw.newimg[object_id].parentNode.removeChild(ssw.newimg[object_id]);
	}
};

ssw.setOpacity = function(type, obj, value) {
  //set new opacity value on both elements using whatever method is supported
  switch(type) {
    case 'ie'   : obj.filters.alpha.opacity = value * 100; break;
    case 'khtml': obj.style.KhtmlOpacity = value; break;
    case 'moz'  : obj.style.MozOpacity = (value == 1) ? 0.99 : value; break;
    default     : obj.style.opacity = (value == 1) ? 0.99 : value;
  }
};

//get real position method
ssw.getRealPosition = function() {
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null) {
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}

	return this.pos;
};

