// JavaScript Document
// V 0.01 beta

var loaderko = '<img src="../js/ajax-loader.gif">';

function E(id) {
	var a = document.getElementById(id)
	return a
}

// initiate at first load
function $I(I) {	
	if (window.addEventListener) { //FF //Safari 
		window.addEventListener("load", I, false); 
	} else if (window.attachEvent) { // EI
		window.attachEvent("onload", I);
	}
}

var B = {
	K: function(x) { return x },
	E: function (id) { 
		var a = document.getElementById(id)
		return a
	},
	IE:     !!(window.attachEvent && !window.opera),
  Opera:  !!window.opera,
  WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
  Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
   MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
		  
};

var I  = {
	E : function(target, theFunction, tasktype){ 
		var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
		if (target.addEventListener)
			target.addEventListener(tasktype, theFunction, false)
		else if (target.attachEvent)
			target.attachEvent(tasktype, theFunction)
}	
	
};

// 

var Img =  {
	swap: function (a,b,c,d) {		
		p = [];
		p[1] = new Image();
		p[1].src = c;
		p[2] = new Image();
		p[2].src = d;

		if(document.images) {
			return document.images[a].src = p[b].src;
		}
		
	}
};
// 	

var Win = {
	
	popwin: function (target,name,width, height,scrollbars) {
			win = window.open(target, name, "width="+width+",height="+height+",scrollbars="+scrollbars+",resizable=no");
			win.focus(); 
		
	},
	
	topPos: function (el){
		yPos = B.E(el).offsetTop; tempEl = B.E(el).offsetParent;
		while (tempEl != null) {
			yPos += tempEl.offsetTop; tempEl = tempEl.offsetParent;
		}
		return yPos;
	},
	leftPos: function (el){
		xPos = B.E(el).offsetLeft; tempEl = B.E(el).offsetParent;
		while (tempEl != null) {
			xPos += tempEl.offsetLeft; tempEl = tempEl.offsetParent;
		}
		return xPos;
	},
	widthSize: function () {	
		if (window.innerWidth)
			w=window.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth)
			w=document.documentElement.clientWidth;
		else if (document.body)
			w=document.body.clientWidth;
		
			return w;
	}, 
	heightSize: function () {
		if (window.innerHeight)
			h=window.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight)
			 h=document.documentElement.clientHeight;
		else if (document.body)
			 h=document.body.clientHeight;
		return h;
	},
	topScroll : function () {
		if(B.IE) {a = document.body.scrollTop; }
		else {a = window.pageYOffset;}
		return a;
	},
	leftScroll : function () {
		if(B.IE) { a = document.body.scrollLeft;}
		else{a = window.pageXOffset;}
		return a;
	},
	getScrollXY : function () {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
	},

	pageWithScroll: function () {
		if (window.innerHeight && window.scrollMaxY) {// Firefox
			yWithScroll = window.innerHeight + window.scrollMaxY;
			xWithScroll = window.innerWidth + window.scrollMaxX;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			yWithScroll = document.body.scrollHeight;
			xWithScroll = document.body.scrollWidth;
		} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
			yWithScroll = document.body.offsetHeight;
			xWithScroll = document.body.offsetWidth;
			}
		arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
		return arrayPageSizeWithScroll;
	}, 
	centerdiv : function (e) {
		ap = this.getScrollXY();
		
		scrolY = ap[1]; // scroll height
		scrolX = ap[0]; // scroll width
		
		lapad = parseInt(B.E(e).offsetWidth);// object width
		taas = parseInt(B.E(e).offsetHeight); // object height
		
		screenWidth = this.widthSize(); // size of screen width
		screenHeight = this.heightSize(); // size of the scroll height
		
		
		
		if(taas > screenHeight) {
				t = 10; 
				l = ((screenWidth -lapad)/2);
				
		}else {
			if(scrolY == 0) {
				t = ((screenHeight -taas)/2); 
				l = ((screenWidth -lapad)/2); 
			}else {
				t = ((screenHeight -taas)/2) + scrolY; 
				l = ((screenWidth -lapad)/2) + scrolX;
			}
		}
		
	return size = new Array(t,l);
	}
	
};

var myCommand = {
	copy : function(e) {
	
		var a = B.E(e)
		a.focus()
		a.select()
		b = a.createTextRange()
		b.clipboard.autocopy
		b.execCommand('Copy');
	},
	cut : function(e) {

		var a = B.E(e)
		a.focus()
		a.select()
		b = a.createTextRange()
		b.execCommand('Cut');
	},
	paste : function(e) {
		var a = B.E(e)
		a.focus()
		a.select()
		b = a.createTextRange()
		b.execCommand('Paste');
	}
	
};
var divPop = {
		mybox : function (theDiv, container, page, wid, hayt, tagetiframe, where) {	
			
			a = Win.pageWithScroll();
			wholeHeight = a[1];
			wholeWidth = a[0];
	
			// screen
			screenWidth = Win.widthSize();
			screenHeight = Win.heightSize();
			
		
			var win_height = B.E(theDiv).style.height = wholeHeight +  "px";	
			if(B.IE) {
				var win_width = B.E(theDiv).style.width = wholeWidth+"px"
			}else {
				var win_width = B.E(theDiv).style.width = "100%"
			}
			B.E(theDiv).style.visibility = "visible"
			if(B.E(theDiv).offsetHeight < screenHeight) { B.E(theDiv).style.height = "100%" }
			
			
			B.E(container).style.visibility = "visible"
			B.E(container).style.width = wid + "px" // given
			B.E(container).style.height = hayt + "px" // given
			
			
			centro = Win.centerdiv(container);
			B.E(theDiv).style.filter = 'alpha(opacity=50)';
			B.E(theDiv).style.opacity = .5;
				
			B.E(container).style.top = centro[0] + "px"	
			B.E(container).style.left = centro[1] + "px"		
			if(where != "") {
				where = '::'+ where;	
			}
			
			$X(page,'page=='+tagetiframe+'::height=='+ (hayt-50) +'::width=='+(wid-50) + where  ,container,'');
				
			
		},
		showme: function (a,b,c,d,e,el, parent) {
		
		yconPos = Win.topPos(parent)// of the the div container
		xconPos = Win.leftPos(parent)
		
		ybutPos = Win.topPos(el)// the button position
		xbutPos = Win.leftPos(el)
		
		B.E(e).style.visibility = 'visible'	
		
		
		if(B.IE) {// if IE
			B.E(e).style.top = ybutPos-(yconPos-100)+'px' //TOP  -->Y == top
			B.E(e).style.left = xbutPos-(xconPos)+'px' //--> LEFT == X	
			
		}else{
			B.E(e).style.top = ybutPos-yconPos+'px' //TOP  -->Y == top
			B.E(e).style.left = xbutPos-xconPos+'px' //--> LEFT == X
		}

		$X(a,b,c,d)
		
	},
	
	showmeB: function (a,b,c,d,parentPos,btnPos) {
		
		B.E(c).style.visibility = 'visible'	
		
		yconPos = Win.topPos(parentPos) // parant div position
		xconPos = Win.leftPos(parentPos)
		
		ybutPos = Win.topPos(btnPos)// the button position
		xbutPos = Win.leftPos(btnPos)
		valwidth = B.E(c).offsetWidth+20; // width of the element
		
		
		if(B.IE) {// if IE
			B.E(c).style.top = ybutPos-(yconPos-90)+'px' //TOP  -->Y == top
			B.E(c).style.left = 80+'px' //--> LEFT == X	
			
		}else{
			B.E(c).style.top = ybutPos-yconPos+'px' //TOP 
			B.E(c).style.left = (xbutPos-valwidth)-xconPos+'px' // LEFT 
		}
				
		$X(a,b,c,d);
		
	},
	hideme: function (e) {
		var a = e.split(",")
		for(i=0; i<a.length; i++) {
			B.E(a[i]).style.visibility = 'hidden'
		}
	}
	
};

var message = { 
	kompirm: function () {
		var answer = confirm ("Please Read the data carefully before saving! then click yes/ok")
		if (answer)
		alert ("The file will be saved in database!")
		else
		return false;
	},
	validate: function (str) {
		//str = toString(str)
		b = (str).replace(/^\s*|\s*$/g,''); // remove the white spaces
		c= b.split("::")
		
		// format first the text
		vstring = []		
		for(i=0; i<c.length; i++) {
			if(c[i].indexOf("!")== -1) {
				v = c[i]+"!nothing==nothing"
			}else {
				v = c[i]
			}
			vstring.push(v)
		}
		ff = vstring.toString()
		ff = ff.replace(/\!/g, ",")
		ff = ff.split(",")
		var field = []
		var tempval = []
		
		for(i=0; i<ff.length; i++) {
			if(i%2 == 0) {
				field.push(ff[i])
	
			}else {
				tempval.push(ff[i])
			}
		}// end for
		
		tempval = tempval.toString();
		tempval = tempval.replace(/==/g, ",")
		tempV = tempval.split(",")
		var val = []
		var valid = []
		for(k=0; k<tempV.length;k++) {
			if(k%2 == 0) {
			valid.push(tempV[k])
			}else {
			val.push(tempV[k])
			}
		}// end for		
		
		var newstring = []
		
		for(i=0; i<c.length; i++) {
		
			if(valid[i] == "blank") {
				if(B.E(field[i]).value == "") {
					alert("Please don't leave the "+val[i]+" blank")
					B.E(field[i]).focus()
					return 
				}

			}else if(valid[i] == "email") {
				if((B.E(field[i]).value.indexOf('.')== -1)) {
					alert("Please provide a valid email address")
					B.E(field[i]).focus()
					return
				}
				if((B.E(field[i]).value.indexOf('@')== -1)) {
					alert("Please provide a valid email address")
					B.E(field[i]).focus()
					return
				}
				
	
			}else if(valid[i] == "match") {
				if(B.E(field[i]).value != B.E(field([i]+'1')).value) {
					alert("The "+ val[i] + " did not match!")
					B.E(field[i]).focus()
					return
				}
			}else if(valid[i] == "blankmatch") {
				//alert(B.E(field[i]+"1").value)
				
				if(B.E(field[i]).value == "") {
					alert("Please don't leave the "+val[i]+" blank")
					B.E(field[i]).focus()
					return 
				}else if(B.E(field[i]+"1").value == "") {
					alert("Please retype "+val[i])
					B.E(field[i]+"1").focus()
					return 
				}else if(B.E(field[i]).value != B.E(field[i]+"1").value) {
					alert("The "+ val[i] + " did not match!")
					B.E(field[i]).focus()
					return
				}
				
			}else if(valid[i] == "blanknotmatch") {
				//alert(B.E(field[i]+"1").value)
				
				if(B.E(field[i]).value == "") {
					alert("Please don't leave the "+val[i]+" blank")
					B.E(field[i]).focus()
					return 
				}else if(B.E(field[i]+"1").value == "") {
					alert("Please retype "+val[i])
					B.E(field[i]+"1").focus()
					return 
				}else if(B.E(field[i]).value == B.E(field[i]+"1").value) {
					alert("Please select "+ val[i] + " with different value!")
					B.E(field[i]).focus()
					return
				}
				
			}else if(valid[i] == "number") {
				if(isNaN(B.E(field[i]).value) == true) {
					alert("The data must be numeric or number")
					B.E(field[i]).focus();
					return	
				}	
			}else if(valid[i] == "string") {
				var newValue = B.E(field[i]).value
				var newLength = newValue.length;
				var extraChars=". -,";
				var search;
				   for(var i = 0; i != newLength; i++) {
					  aChar = newValue.substring(i,i+1);
					  aChar = aChar.toUpperCase();
					  search = extraChars.indexOf(aChar);
					  if(search == -1 && (aChar < "A" || aChar > "Z") ) {
						 alert("The data must not have wild characters!")
							B.E(field[i]).focus();
							return	
					  }
				   }					
			}// end if
			newstring.push((field[i]))			
		}// end for
		return 	newstring				
	}
	
};



//-------------------------------------------------------------------------------//

// a = targer file
// b = get value parameters
// c = target html object reference by id, this is where the ajax loads
// d = clear the div
function $X(a, b, c, d) { 
var X
X=GetXmlHttpObject()

	if (X==null){
 		alert("Browser does not support HTTP Request")
 	return
 	}
var url=a

var newb = message.validate(b)
if(newb == null) {
	return
}
//alert(newb)
b = newb.toString()
b = b.replace(/,/g,"::")

// lets clear other div if exist
if(d.length != 0) {
d = (d).replace(/^\s*|\s*$/g,''); // remove the white spaces
tempdiv = d.split("::")	

	for(j=0; j<tempdiv.length; j++) {
		B.E(tempdiv[j]).innerHTML="";
	}

}

b = (b).replace(/^\s*|\s*$/g,''); // remove the white spaces
if(b.length == 0) {
	url=url // if the get value parameters is empty 
}else {
	if(b.indexOf('==') == -1) { 
		alert("Error get parameters");
		return 
	}else {
	tempval = b.split("::")	
		for(i=0; i<tempval.length; i++) {
			chk = tempval[i].split("==")
			if(chk[0].length == 0) {
				alert("The value! [ " + chk[1] + " ] has no name")	
				return false
			}else if(chk[1].length == 0) {
				alert("The name [ " + chk[0] + " ] has no value")	
				return 
			}
			
		}
	
	temp = b.replace(/::/g,"&"); // replace all the delimeter
	urlb = temp.replace(/==/g, "="); // replace all the delimeter	
	
	url=url+"?"+urlb	
	}
}

X.onreadystatechange=state


function state() { // call back function 

	if (X.readyState==4 || X.readyState=="complete") { 
 		B.E(c).innerHTML=X.responseText	
		
 	} else if (X.readyState==1 || X.readyState== 2) { 
 		B.E(c).innerHTML=loaderko	
 	} 
}

X.open("GET",url,true)
X.send(null)

}

var $X1 = {}; $X1.get = $X;
// -------------------------------------------------------------

// a = targer file
// b = post value parameters( name of the object only)
// c = target html object reference by id, this is where the ajax loads
// d = clear the div

function $Y(a, b, c, d) { 
var Y
Y=GetXmlHttpObject()
	if (Y==null){
 		alert("Browser does not support HTTP Request")
 	return
 	}
var url=a
newb = message.validate(b)
b = newb.toString()
b = b.replace(/,/g,"::")


// lets clear other div if exist
if(d.length != 0) {
d = (d).replace(/^\s*|\s*$/g,''); // remove the white spaces
tempdiv = d.split("::")	

	for(j=0; j<tempdiv.length; j++) {
		B.E(tempdiv[j]).innerHTML="";
	}

}

b = (b).replace(/^\s*|\s*$/g,''); // remove the white spaces
tempval = b.split("::")

		ttemp = ""; 
		// set some error trapment when the html objects will not exist
		for(i=0; i<tempval.length; i++) { // if the post object do not exist the post will be canve		
			ttemp += tempval[i] +'='+ encodeURI(B.E(tempval[i]).value)+"&"
		}


ttemp = ttemp.replace(/\+/g, '****') // fix this in php replace
var postme = ttemp

Y.onreadystatechange=state
function state() { // call back function 

	if (Y.readyState==4 || Y.readyState=="complete") { 
		B.E(c).innerHTML=Y.responseText	;		
	
	} else if (Y.readyState==1 || Y.readyState== 2) { 
		B.E(c).innerHTML=loaderko	
	} 
}

Y.open('POST', url, true);
Y.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
Y.setRequestHeader("Content-length", postme.length);
Y.setRequestHeader("Connection", "close");
Y.send(postme);

}

var $Y1 = {}; $Y1.post = $Y;

//  0000000000000000000000000000000000000000000000000000000000000000 //

function GetXmlHttpObject() {
var xmlHttp=null;
	try { // Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e) { //Internet Explorer
 		try{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e){
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}
//  0000000000000000000000000000000000000000000000000000000000000000 //
/*
function selectCode(a)
{
	// Get ID of code block
	var e = a.parentNode.parentNode.getElementsByTagName('PRE')[0];

	// Not IE
	if (window.getSelection)
	{
		var s = window.getSelection();
		// Safari
		if (s.setBaseAndExtent)
		{
			s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
		}
		// Firefox and Opera
		else
		{
			var r = document.createRange();
			r.selectNodeContents(e.firstChild);
			s.removeAllRanges();
			s.addRange(r);
		}
	}
	// Some older browsers
	else if (document.getSelection)
	{
		var s = document.getSelection();
		var r = document.createRange();
		r.selectNodeContents(e);
		s.removeAllRanges();
		s.addRange(r);
	}
	// IE
	else if (document.selection)
	{
		var r = document.body.createTextRange();
		r.moveToElementText(e);
		r.select();
	}
}

*/
