var ALLOWED_EXT = "jpg,jpeg,gif,bmp,png";


function closeWindow() {
	window.close();
}

function clearWindow() {
	location.reload();
}

function imageViewer(vImg, vId) {
	window.open("../../common/viewer/image_viewer.php?picture=" + vImg, "img_viewer", "top=20px,left=20px,width=400px,height=400px,status=yes,scrollbars=yes");
}


function mapViewer(vAddress) {
	window.open("http://maps.google.com/maps?ie=UTF-8&oe=UTF-8&hl=en&tab=wl&q=" + vAddress);
}


function initPicture() {
	resizePicture(prodPicture, 310, 230)
}


function resizePicture(tgtImg, w_stand, h_stand) {
	var w = tgtImg.width;
	var h = tgtImg.height;

	if (w > w_stand || h > h_stand) {
		var w_m = w - w_stand;
		var h_m = h - h_stand;

		if (w_m > h_m) {
			tgtImg.width = w_stand;
			tgtImg.height = w * w_stand / h;
		} else if (w_m < h_m) {
			tgtImg.height = h_stand;
			tgtImg.width = h * h_stand / w;
		}
	}
}


function resizePicture2(tgtImg, w_stand, h_stand) {
	var w = tgtImg.width;
	var h = tgtImg.height;

	if (w > w_stand || h > h_stand) {
		var w_m = w_stand - w;
		var h_m = h_stand - h;

		if (w_m < h_m) {
			tgtImg.width = w_stand;
		} else if (w_m > h_m) {
			tgtImg.height = h_stand;
		}
	}
}


function switchPicture(objTarget, objImg) {
	objTarget.src = objImg.src;
	resizePicture(objTarget, 310, 230);
	prodTitle.innerHTML = objImg.title;
}


function adjustScreen() {
	var vWidth = imgViewer.width+30;
	var vHeight = imgViewer.height+65;
	var w = window.screen.width - 50;
	var h = window.screen.height - 50;
	
	if (vWidth > w) {
		vWidth = w;
	}
	if (vHeight > h) {
		vHeight = h;
	}
	if (navigator.appName=="Netscape") {
		top.outerWidth = vWidth;
		top.outerHeight = vHeight;
	} else {
		window.resizeTo(vWidth, vHeight);
	}
	window.moveTo(20,20);
}


function validateString(s, reg) {
	if (s == null) return 0;
	if (reg == null) return 0;
	s = s.toLowerCase();
	for (i = 0; i < s.length; i++) {
		if (reg.indexOf(s.charAt(i)) < 0) {
			return 0;
		}
	}
	return 1;
}

function isEngNumOnly(s) {
	var reg = "0123456789abcdefghijklmnopqrstuvwxyz-_";
	return validateString(s, reg);
}

function isValidUserId(s) {
	var reg = "0123456789abcdefghijklmnopqrstuvwxyz";
	return validateString(s, reg);
}

function isEngOnly(s) {
	var reg = "abcdefghijklmnopqrstuvwxyz";
	return validateString(s, reg);
}

function isPhoneNumber(s) {
	var reg = "0123456789()- ";
	return validateString(s, reg);
}

function isZipcode(s) {
	var reg = "0123456789";
	if (s == null) return 0;
	if (s.length != 5) return 0;
	return validateString(s, reg);
}

function isUrl(s) {
	if (s == null) return 0;
	var v = new RegExp(); 
	//v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");	
	v.compile("[A-Za-z0-9-_]+\\.[A-Za-z0-9-_~%&\?\/.=]+$");
	
	if (v.test(s)) { 
		return 1; 
	} else {
		return 0;
	}
}

function isEmail(s) {
	if (s == null) return 0;
	
	v = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (v.test(s)) {
		return 1;
	} else {
		return 0;
	}
}

function isNumber(s) {
	if (s == null) return 0;
	if (isNaN(s)) {
		return 0;
	} else {
		return 1;
	}
}

function setOnlyNumber(obj) {
	if (obj.value == "") 
		obj.value = "";
	if (isNaN(obj.value)) {
		obj.value = "";
	}
}

function setOnlyInteger(obj) {
	if (obj.value == "") 
		obj.value = "";
	if (isNaN(obj.value)) {
		obj.value = "";
	} else {
		obj.value = Math.floor(obj.value);
		obj.value = (obj.value == 0 ? 1 : obj.value);
	}
}

function setOnlyIntegerNull(obj) {
	if (obj.value == "") 
		obj.value = "";
	if (isNaN(obj.value)) {
		obj.value = "";
	} else {
		obj.value = Math.floor(obj.value);
		obj.value = (obj.value == 0 ? "" : obj.value);
	}
}

function getByteLength(s) {
	var len = 0;
	if (s == null) return 0;
	
	for(var i = 0; i < s.length; i++){
		var c = escape(s.charAt(i));
		if (c.length == 1) len++;
		else if (c.indexOf("%u") != -1) len += 2;
		else if (c.indexOf("%") != -1) len += c.length/3;
	}
	return len;
}

String.prototype.trim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"");
}

String.prototype.fulltrim = function() {
	return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");
}


/*
* Cookie
*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function changeClassName(obj, str) {
	obj.className = str;
}

function toggle(toggler, obj) {	
	if (obj != undefined) {
		if (obj.length == null) {
			obj.checked = toggler.checked;
		} else {
			for (i = 0; i < obj.length; i++) {
				obj[i].checked = toggler.checked;
			}
		}
	}
}


function toggleCheckbox(objForm, toggler) {	
	for (i = 0; i < objForm.elements.length; i++) {	
		if (objForm.elements[i].type == "checkbox") {
			objForm.elements[i].checked = toggler.checked;
		}
	}
}


function toggleCheckboxByValue(objForm, value) {	
	for (i = 0; i < objForm.elements.length; i++) {	
		if (objForm.elements[i].type == "checkbox") {
			objForm.elements[i].checked = value;
		}
	}
}


function loginAlert() {
	alert("Please sign in.");
}


function activeOptionsDisabled(){
    var sels = document.getElementsByTagName('select');
    for(var i=0; i < sels.length; i++){
        sels[i].onchange= function(){
            if(this.options[this.selectedIndex].disabled){
                this.selectedIndex = 0;
                /*
                if(this.options.length<=1){
                    this.selectedIndex = -1;
                }else if(this.selectedIndex < this.options.length - 1){
                    this.selectedIndex++;
                }else{
                    this.selectedIndex--;
                }
                */
            }
        }
        if(sels[i].options[sels[i].selectedIndex].disabled){
            sels[i].onchange();
        }    
        for(var j=0; j < sels[i].options.length; j++){ 
            if(sels[i].options[j].disabled){
                sels[i].options[j].style.color = '#999999';
            }
        }
    }
}



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;
}

