/***
File name: common.js
Version: 3.0
Copyright (c) 1999~2007, Super Ease Development Limited, all rights reserved.
*/

/**
* Cookie functions
*/
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

/**
Date time manipulation functions
Return true if the given date is valid, return false if not.
@param obj string
@return boolean
*/
function isDate(obj) {
//  var result=obj.match(/(\d{1,2})[\/|-](\d{1,2})[\/|-](\d{2,4})/); //mm-dd-yyyy
  var result=obj.match(/(\d{2,4})[\/|-](\d{1,2})[\/|-](\d{1,2})/); // yyyy-mm-dd
  if (result!=null) {
	intDay=result[3];
	intMonth=result[2];
	intYear=result[1];
	intDay=Math.floor(intDay);
	intMonth=Math.floor(intMonth);
	intYear=Math.floor(intYear);
    if(isLeapYear(intYear)) { //leap year
	  switch(intMonth)  {
	    case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: 
		  if (intDay<1 || intDay>31) {
			return false;
		  } else {
		    return true

		  }
		  break;
		case 4:
		case 6:
		case 9:
		case 11:
		  if (intDay<1 || intDay>30) {
			return false;
		  } else {
		    return true
		  }
		  break;
		case 2:
		  if (intDay<1 || intDay>29) {
			return false;
		  } else {
		    return true
		  }
		  break;
		default:
    		return false;
	  }
	} else { // not leap year
	  switch(intMonth)  {
	    case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: 
		  if (intDay<1 || intDay>31) {
			return false;
		  } else {
		    return true
		  }
		  break;
		case 4:
		case 6:
		case 9:
		case 11:
		  if (intDay<1 || intDay>30) {
			return false;
		  } else {
		    return true
		  }
		  break;
		case 2:
		  if (intDay<1 || intDay>28) {
			return false;
		  } else {
		    return true
		  }
		  break;
		default:
			return false;
	  }
	}
  } else { //not match
	return false;
  }
}

/*********
To determine the given year is leap year or not.
@param intYear, integer
@return boolean
**********/
function isLeapYear(intYear) {
  if (intYear<100 && intYear>=0) {
    intYear+=2000;
  }
  if (intYear%400==0) {
    return true;
  } else if (intYear%100==0) {
    return false;
  } else if (intYear%4==0) {
    return true;
  } else {
    return false;
  }
}

function popup(i, max) {
	if (top.main) {	
		var a=top.main.document.getElementById('popup');
		var aa='main';
	} else if (top.rightframe) {
		var a=top.rightframe.document.getElementById('popup');
		var aa='rightframe';
	} else {
		var a=document.getElementById('popup');
		var aa='_self';
	}

	var p=i.lastIndexOf('/')+1;
	var path=i.substr(0,p);
	var current=i.substr(p);
	var currentInt=parseInt(current);
	if (max>0) {
		var strNav="| ";
		for (j=1; j<=max; j++) {
			if (j==currentInt) {
				strNav+="[<a href=\"#\" onclick=\"document.getElementById('popupiframe').src='"+path+arrImage[j-1]+"';document.getElementById('urltext').value='http://"+window.location.hostname+path+arrImage[j-1]+"';\">"+j+"</a>] | ";
			} else {
				strNav+="<a href=\"#\" onclick=\"document.getElementById('popupiframe').src='"+path+arrImage[j-1]+"';document.getElementById('urltext').value='http://"+window.location.hostname+path+arrImage[j-1]+"';\">"+j+"</a> | ";
			}
			if (j%20==19)
			{ 
				strNav+="<br />";
			}
		}
	}
	strNav+="<br />URL: <input type=\"text\" id=\"urltext\" size=\"50\" value=\"http://"+window.location.hostname+i+"\" onfocus=\"this.select();\"/>"
	var b="<div class=\"popup\" style=\"background-color:#FFDDDD\"><div><input type=\"button\" value=\"X\" onclick=\"javascript:popupclose()\">&nbsp;&nbsp;"+strNav+"</div><iframe src=\""+i+"\" frameborder=\"0\" name=\"popupiframe\" id=\"popupiframe\" width=\"820\" height=\"830\" border=\"0\" ></iframe></div>";
	a.innerHTML=b;
	if (top.main) {
		top.main.scrollTo(0,0);
	} else if (top.rightframe) {
		top.rightframe.scrollTo(0,0);
	} else {
		window.scrollTo(0,0);
	}
}


function popupclose() {
	var myElement=document.getElementById('popup');
	if (myElement!=null) {myElement.innerHTML='';}
}


function hd_popup(i, max) {
	if (top.main) {	
		var myElement=top.main.document.getElementById('popup');
		var aa='main';
	} else if (top.rightframe) {
		var myElement=top.rightframe.document.getElementById('popup');
		var aa='rightframe';
	} else {
		var myElement=document.getElementById('popup');
		var aa='_self';
	}

	var p=i.lastIndexOf('/')+1;
	var path=i.substr(0,p);
	var current=i.substr(p);
	if (max>0) {
		var strNav="| ";
		for (j=1; j<=max; j++) {
			if (arrHDImage[j-1]==current) {
				strNav+="<b>["+j+"]</b> | ";
			} else {
				strNav+="<a href=\"javascript:hd_popup('"+path+arrHDImage[j-1]+"', "+max+");\">"+j+"</a> | ";
			}
			if (j%20==19)
			{ 
				strNav+="<br />";
			}
		}
	}
	var b="<div class=\"popup\" style=\"background-color:#DDFFFF\"><div><input type=\"button\" value=\"X\" onclick=\"javascript:hd_popupclose()\">&nbsp;&nbsp;"+strNav+"</div><iframe src=\"hdphotoviewer.php?f="+i+"\" frameborder=\"0\" name=\"popupiframe\" id=\"popupiframe\" width=\"820\" height=\"830\" border=\"0\" ></iframe></div>";

	myElement.innerHTML=b;
	if (top.main) {
		top.main.scrollTo(0,0);
	} else if (top.rightframe) {
		top.rightframe.scrollTo(0,0);
	} else {
		window.scrollTo(0,0);
	}
}


function hd_popupclose() {
	var myElement=document.getElementById('popup');
	if (myElement!=null) {myElement.innerHTML='';}
}

function comingsoon() {
	alert('Coming soon!');
}

function showLocationPicker(id) {
	//var myLoc=document.getElementById('location_1');
	var location_2=document.getElementById('location_2');
	location_2.innerHTML=myLoc2[id];
}

function joinMember(lang) {
	var joinwin=window.open('action/apply_freeMembership.php?lang='+lang,'','width=650,height=555,resizable=1,toolbar=0,menubar=0,scrollbars=1');
}

function validateMobile(country_code,mobile) {
	switch (country_code)
	{
		case "852":
			return validateMobileHongKong(mobile);
			break;
		case "853":
			return validateMobileMacau(mobile);
			break;
		case "86":
			return validateMobileChina(mobile);
			break;
		case "886":
			return validateMobileTaiwan(mobile);
			break;
		default:
			return validateMobileOther(mobile);
			break;
	}
}

function validateMobileHongKong(mobile) {
	return (/^[5|6|9]\d{7}$/.test(mobile));
}

function validateMobileChina(mobile) {
	return (/^1[3|5|8][0-9]\d{4,8}$/.test(mobile));
}

function validateMobileMacau(mobile) {
	return (/^6\d{7}$/.test(mobile));
}

function validateMobileTaiwan(mobile) {
	return (/^[0|9]\d{9}$/.test(mobile));
}

function validateMobileOther(mobile) {
	return (/^\d{6,20}$/.test(mobile));
}

function validateEmail(email) {
	var filter = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;
	return (filter.test(email)); 
}

function dec2hex(dec) {
    dec = parseInt(dec, 10);
    if (!isNaN(dec)) {
        hexChars = "0123456789ABCDEF";
        if (dec > 255) {
            return "Out of Range";
        }
        var i = dec % 16;
        var j = (dec - i) / 16;
//        result = "0x";
        result = hexChars.charAt(j) + hexChars.charAt(i);
        return result;
    } else {
        return NaN;
    }
}
