// JavaScript Document

//VERSION 1.0.0.1
function $(element_id) {
	if( document.getElementById ) {
		return document.getElementById(element_id);
	} else if( document.all ) {
		return document.all[element_id];
	} else if( document.layers ) {
		return document.layers[element_id];
	} else {
		return null;
	}
}

function LTrim(string) {
	return s.replace(/^\s*/, "");
}

function RTrim(string) {
	return s.replace(/^\s*/, "");
}

function trim(string) {
	return string.replace(/^\s*/, "").replace(/\s*$/, "");
}

function replaceSpace(string) {
	return string.replace( /\s/g, "").replace(/\u3000/g, "");
}

function str_replace(str_search, str_replace, str_subject) {
	if( str_length(str_subject) ) {
		return str_subject.replace(str_search, str_replace);
	}
}

function str_length(string) {
	return string.replace(/[^x00-\xff]/g, "**").length;
}

function getRand(a, b) {
	if( a ) {
		b = b ? b:0;
		return Math.floor(Math.random()*a + b);
	} else {
		a = new Date();
		return a.getTime();
	}
}

function IsNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function IsDecimal(strString) {
	var strPoint = ".";
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	var pointNums = 0;
	
	//point nums
	for (i = 0; i < strString.length; i++) {
		strChar = strString.charAt(i);
		if( strChar == strPoint ) {
			pointNums++;
		}
	}
	if( pointNums != 1 ) {
		return false;
	}
	//first char
	if( strString.charAt(0) == strPoint ) {
		strString = "0" + strPoint;
	}
	//valid char
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function getOffsetLeft(el) {
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null)
		ol += el.offsetLeft;
	return ol;
}


function getOffsetTop(el) {
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null)
		ot += el.offsetTop;
	return ot;
}

function setfocusStyle(div_id, focus_class) {
	if( typeof focus_class == 'undefined' ) {
		focus_class = "focusElement";
	}
	$(div_id).className = focus_class;
}

function setblurStyle(div_id, blur_class) {
	if( typeof blur_class == 'undefined' ) {
		blur_class = "blurElement";
	}
	$(div_id).className = blur_class;
}

function setfocus(div_id, inner_html, focus_class) {
	if( typeof focus_class == 'undefined' ) {
		focus_class = "focusElement";
	}
	$(div_id).className = focus_class;
	$(div_id).innerHTML = inner_html;
}

function setblur(div_id , blur_class) {
	if( typeof blur_class == "undefined" ) {
		blur_class = "blurElement";
	}
	$(div_id).className = blur_class;
}

function validator_form() {
	var has_error = false;
	var has_warning = false;
	var error_startobj = null;
	if(arguments.length > 0 ) {
		var formname = arguments[0];
		if( document.forms[formname] ) {
			var form = document.forms[formname];
			var i;
			for(i=1; i<arguments.length; i++) {
				if( form.elements[arguments[i]] && form.elements[arguments[i]].value == "" ) {
					if( error_startobj == null ) {
						error_startobj = form.elements[arguments[i]];
					}
					var msg = arguments[i] + "_msg";
					if( $(msg) ) {
						$(msg).style.display = "";
						has_warning = true;
					}
					has_error = true;
				} else if( form.elements[arguments[i]] && form.elements[arguments[i]].value != "" ) {
					var msg = arguments[i] + "_msg";
					if( $(msg) ) {
						$(msg).style.display = "none";
					}
				}
			}
			
			if( has_error ) {
				if( has_warning == false ) {
					alert("Please check the form error!");
				}
				if( error_startobj != null && error_startobj.focus ) {
					error_startobj.focus();
				}
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
		
	} else {
		return true;
	}
	return true;
}

function validator_blur( element_id ) {
	//alert(element_id);
	if( $(element_id).value == "" ) {
		var msg = element_id + "_msg";
		if( $(msg) ) {
			$(msg).style.display = "";
		}
	} else {
		var msg = element_id + "_msg";
		if( $(msg) ) {
			$(msg).style.display = "none";
		}
	}
}

function validator_null( element_id ) {
	if( $(element_id) && $(element_id).value == "" ) {
		return true;
	}
	return false;
}

function showHide( target_id ) {
	if( $(target_id).style.display == "" ) {
		$(target_id).style.display = "none";
	} else if( $(target_id).style.display == "none" ) {
		$(target_id).style.display = ""
	}
}

function toggleElement( element_id ) {
	var element = $(element_id);
	if( element.style.display == "none" ) {
		element.style.display = "block";
	} else {
		element.style.display = "none";
	}
	
}

function isWindows() {
	if( navigator.appName.indexOf("Microsoft") != -1 ) return true;
	return false;
}
function isNetscape() {
	if( navigator.appName.indexOf("Netscape") != -1 ) return true;
	return false;
}
	
function getWindowSize() {
	if( parseInt(navigator.appVersion) > 3 ) {
		if( navigator.appName == "Netscape" ) {
			windowWidth = window.innerWidth;
			windowHeight = window.innerHeight;
		} else if( navigator.appName.indexOf("Microsoft") != -1 ) {
			windowWidth = document.body.offsetWidth;
			windowHeight = document.body.offsetHeight;
		}
	}
	//scrollTop
}

//jump to new location
function goToLink(url) {
	document.location.href = url;
}

function goToWorkingLink(url) {
	alert("Your requested url: " + url);
}

//img
var pswapImg = new Object();
pswapImg.oImg = "";
pswapImg.over = function(obj, newImg) {
	pswapImg.oImg = obj.src;
	obj.src = newImg;
}
pswapImg.out = function(obj) {
	obj.src = pswapImg.oImg;
}

//xml
function createXMLHttp() {
	var req = null;
	try {
		req = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {
			req = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (ee) {
			req = null;
		}
	}
	if (!req && typeof XMLHttpRequest != 'undefined') req = new XMLHttpRequest();
	return req;
}

function loadXmlToElement(fragment_url, element_id, mode, de) {
	var element = $(element_id);
	var xmlhttp = createXMLHttp();
	var str;
	
	xmlhttp.open("GET", fragment_url);
	xmlhttp.onreadystatechange = function() {
		if( xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			if( !mode ) {
				str = xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue;
				if( !element.innerHTML ) {
					try {
						str = xmlhttp.responseXML.firstChild.childNodes[1].nodeValue;
					} catch(e) {}
				}
			} else if(mode == 'txt') {
				str = xmlhttp.responseText;
			}
			if( de ) str = unescape(str);
			element.innerHTML = str;
		}
	}
	xmlhttp.send(null);	
}

function CopytoBoard(url, content) {
	content = content ? content : 'Copy the content successfully!';
	window.clipboardData.setData('text', url);
	alert(content);
}

function checkEmail(val) {
	var emailPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if( emailPattern.test(val) == false ) {
		return false;
	} else {
		return true;
	}
}

function popupWin(url, features, features_add, title) {
	if( typeof features == 'undefined' ) {
		features = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no';
	}
	if( typeof features_add == 'undefined' ) {
		features_add = ', width=400, height=400, screenX=50, screenY=50, top=50, left=50';
	} else if( features_add != false ) {
		
	}
	features = features + features_add;
	if( typeof title == 'undefined' ) {
		title = 'popupWindow';
	}
	var win = window.open(url, title, features);
	win.focus();
}

function popupPrintInfo(url) {
	popupWin(url, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=600, height=600, screenX=50, screenY=50, top=50, left=50', false)
}

function popupWindow(url) {
	var win = window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=280,screenX=150,screenY=150,top=150,left=150')
	win.focus();
}



//Window Event
function window_prototype_get_event() {
	var obj = arguments.callee.caller;
	var eventx;
	while( obj != null ) {
		eventx = obj.arguments[0];
		if( eventx && (eventx.constructor == Event || eventx.constructor == MouseEvent) ) {
			return eventx;
		}
		obj = obj.caller;
	}
	return null;
}

if( window.addEventListener ) {
	HTMLElement.prototype.__defineGetter__("runtimeStyle", element_prototype_get_runtimeStyle);
	//window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
	Event.prototype.__defineGetter__("srcElement", event_prototype_get_srcElement);
}

function element_prototype_get_runtimeStyle() {
	return this.style;
}

function event_prototype_get_srcElement() {
	return this.target;
}



////PJS
var PJS = new Object();
PJS.VERSION = "1.0.0.1";
PJS.AUTHOR = "jianyu";
PJS.COPYRIGHT = "JCWINDOWFASHIONS.COM";
PJS.BASEURL = "http://www.jcwindowfashions.com/";
PJS.USER_AGENT = window.navigator.userAgent.toLowerCase(); 
PJS.IS_OPERA = (PJS.USER_AGENT.indexOf("opera")!=-1); 
PJS.IS_IE = ((PJS.USER_AGENT.indexOf("msie")!=-1)&&((document.all&&!PJS.IS_OPERA))); 
PJS.IS_MOZILLA = (PJS.USER_AGENT.indexOf("gecko")!=-1); //is that right?
PJS.IS_NS = (navigator.appName == "Netscape") ? 1 : 0;
//Mouse Event
if( navigator.appName == "Netscape" ) {
	document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
}
function mouse_prototype_right_mischandler() {
	return false;
}
function mouse_prototype_left_mousehandler(e) {
	//var eventx = e:event;
	var eventx = event;
	var eventbutton;
	if( PJS.IS_NS ) {
		eventbutton = eventx.which;
	} else {
		eventbutton = eventx.button;
	}
	if( (eventbutton == 2) || (eventbutton == 3) ) {
		return false;
	}
}
//document.oncontextmenu = mouse_prototype_right_mischandler;
document.onmousedown = mouse_prototype_left_mousehandler;
document.onmouseup = mouse_prototype_left_mousehandler;


//METHODS
PJS.LTrim = function(string) {
	return string.replace(/(^[\s]*)/g, "");
};
PJS.RTrim = function(string) {
	return string.replace(/([\s]*$)/g, "");
};
PJS.Trim = function(string) {
	return string.replace(/(^[\s]*)|([\s]*$)/g, "");
};

//translate string to unicode list
PJS.EncodeToArray = function(string) {
	if( string == null || string.length == 0 ) {
		return null;
	}
	var strlen = string.length;
	var stringlist = Array();
	for(var i=0; i<strlen; i++) {
		stringlist[i] = string.charCodeAt(i);
	}
	
	return stringlist;
};

//translate unicode list to string 
PJS.DecodeToString = function(stringlist) {
	if( stringlist == null || stringlist.length == 0 ) {
		return null;
	}
	var string = "";
	var listlen = stringlist.length;
	for(var i=0; i<listlen; i++) {
		string += String.fromCharCode(stringlist[i]);
	}
	return string;
};
PJS.AttachEvent = function(obj, xEvent, handler){
	if(PJS.IS_IE){
		obj.attachEvent("on"+xEvent,handler);
	}else{
		obj.addEventListener(xEvent,handler,false);
	}
	if(xEvent == "click") obj.style.cursor = "pointer";
};
PJS.AppendDIV = function(pObj, divId, className, text){
	var divObj = document.createElement("DIV");
	if(className) divObj.className = className;
	//divObj.appendChild(document.createTextNode(text));
	if( PJS.IS_IE == true ) {
		divObj.id = divId;
	} else {
		divObj.setAttribute("id", divId);
	}
	with(divObj.style) {
		display = 'none';
		position = 'absolute';
	}
	divObj.innerHTML = text;
	
	pObj.appendChild(divObj);
};
PJS.AppendDIVHTML = function(divId, innerHTML, className) {
	var divObj = document.createElement('DIV');
	if( PJS.IS_IE == true ) {
		divObj.id = divId;
	} else {
		divObj.setAttribute("id", divId);
	}
	//divObj.id = divId;
	with(divObj.style) {
		display = 'none';
		position = 'absolute';
	}
	divObj.innerHTML = innerHTML;
	document.body.appendChild(divObj);
};
PJS.toggleElement = function(element) {
	if( element.style.display == "none" ) {
		element.style.display = "";
	} else {
		element.style.display = "none";
	}
};

PJS.getX = 0;
PJS.getY = 0;
PJS.getPosition = function() {
	var position = new Array();
	var eventx;
	if( PJS.IS_IE == true ) {
		eventx = window.event;
	} else {
		//eventx = arguments[0];
		try {
			window.constructor.prototype.__defineGetter__("event", window_prototype_get_event);
		} catch(e) {}
		eventx = event;
	}
	
	if( eventx.pageX || eventx.pageY ) {
		PJS.getX = eventx.pageX;
		PJS.getY = eventx.pageY;
	} else {
		if( eventx.clientX || eventx.clientY ) {
			if( document.body.scrollLeft > document.documentElement.scrollLeft ) {
				PJS.getX = eventx.clientX + document.body.scrollLeft;
			} else {
				PJS.getX = eventx.clientX + document.documentElement.scrollLeft;
			}
			
			if( document.body.scrollTop > document.documentElement.scrollTop ) {
				PJS.getY = eventx.clientY + document.body.scrollTop;
			} else {
				PJS.getY = eventx.clientY + document.documentElement.scrollTop;
			}
		}
	}
	position[0] = PJS.getX;
	position[1] = PJS.getY;
	return position;
};
PJS.getOffsetLeft = function(el) {
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null)
		ol += el.offsetLeft;
	return ol;
};
PJS.getOffsetTop = function(el) {
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null)
		ot += el.offsetTop;
	return ot;
};
//
PJS.Mask = function(divId, width, height) {
	var element = $(divId);
	if( element.innerHTML == "" ) {
		return;
	}
	var objBrother = element.childNodes[0];
	var divObj = document.createElement('DIV');
	var divObjInnerHTML = '<div style="text-align:center; margin-top:40px;">Loading....</div>';
	divObj.setAttribute("id", "div_mask");
	divObj.style.textAlign = "center";
	divObj.style.position = "absolute";
	divObj.style.opacity = (80 / 100);
	divObj.style.MozOpacity = (80 / 100);
	divObj.style.KhtmlOpacity = (80 / 100);
	divObj.style.filter = "alpha(opacity=80)";
	divObj.style.height = height + "px";
	divObj.style.width = width + "px";
	divObj.style.margin = "0px";
	divObj.style.backgroundColor = "#FFFFFF";
	
	element.insertBefore(divObj, objBrother);
	divObj.innerHTML = divObjInnerHTML;
};
//
PJS.p_timeout = null;
PJS.p_init = function() {
	if( $("p_m_box") && str_length($("p_m_box").innerHTML) > 20 ) {
		//noop
	} else {
		if( !$("p_m_box") ) {
			PJS.AppendDIVHTML("p_m_box", "", "p_m_box");
		}
		//CONTNET
		loadXmlToElement(PJS.BASEURL + "services/getPM", "p_m_box", 'txt');
	}
	if( $("p_m") ) {
		PJS.AttachEvent($("p_m"), "mouseover", PJS.p_show);
		PJS.AttachEvent($("p_m"), "mouseout", PJS.p_hide);
	}
	if( $("p_m_box") ) {
		PJS.AttachEvent($("p_m_box"), "mouseover", PJS.p_show);
		PJS.AttachEvent($("p_m_box"), "mouseout", PJS.p_hide);
	}
};
PJS.p_show = function() {
	var left = PJS.getOffsetLeft($("p_m"));
	var top = PJS.getOffsetTop($("p_m")) + 20;
	//if( top > 400 ) {
	//	left = left + 40;
	//	top = top - 100;
	//}
	
	$("p_m_box").style.left = left + "px";
	$("p_m_box").style.top = top + "px";
	$("p_m_box").style.display = "";
	if( PJS.p_timeout ) {
		clearTimeout(PJS.p_timeout);
	}
};
PJS.p_hide = function() {
	PJS.p_timeout = setTimeout("PJS.p_close()", 500);
};
PJS.p_close = function() {
	$("p_m_box").style.display = "none";
	if( PJS.p_timeout ) {
		clearTimeout(PJS.p_timeout);
	}
};

//Facade
var fadeObjects = new Object();
var fadeTimers = new Object();
function jcfade(object, destOp, rate, delta){
	if( !document.all ) {
		return;
	}
	if( typeof(object) != "object" ){
		return;
	}
	if( fadeTimers != null && fadeTimers[object.sourceIndex] ) {
		clearTimeout(fadeTimers[object.sourceIndex]);
	}
	diff = destOp - object.filters.alpha.opacity;
	direction = 1;
	if( object.filters.alpha.opacity > destOp){
		direction = -1;
	}
	delta = Math.min(direction*diff,delta);
	object.filters.alpha.opacity += direction*delta;
	if( object.filters.alpha.opacity != destOp ){
		fadeObjects[object.sourceIndex] = object;
		fadeTimers[object.sourceIndex] = setTimeout("jcfade(fadeObjects["+object.sourceIndex+"],"+destOp+","+rate+","+delta+")",rate);
	}
}

/////////////////////////////////////////////////////////////////////////////
//onload
window.onload = function() {
	
	PJS.p_init();
};
/////////////////////////////////////////////////////////////////////////////