//-----------------ADV--------------------------------------------------------//


//--------------------------------------------------------------------------//
function openPopup(url,window_title,width,height){

	window.open(url,window_title,'width=' + width + ',height=' + height + ',toolbar=no,menubar=no,status=no,location=no');

}

function changePic(divId, picPath, bigPicPath) {
	document.getElementById(divId).innerHTML = "<a href=\"javascript: openPopup('"
												+ bigPicPath
												+ "','SP',470,600)\">"
												+ "<img src=\""
												+ picPath
												+ "\" width=\"150\" height=\"189\" hspace=\"5\" vspace=\"5\" border=\"0\" /></a>";
}

function maxLength(name,len, val){
	if(name.value.length>len){
		name.value=name.value.substr(0,len);
	}
	document.getElementById(val).innerHTML = len - name.value.length;
}

function isNumbers(e)
{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	keychar = String.fromCharCode(keynum);
	if(keychar == '\b')
		return true;
	numcheck = /\d/;
	return numcheck.test(keychar);
}

function resetSearch(val)
{
	document.getElementById(val).value='';
	return false;
}

function isUser(text, val, err, url)
{
	jQuery(document).ready(function(){
		jQuery("#" + text).ajaxStart(function(){
		   jQuery('#' + err ).html("<img src='" + url + "public/images/progress.gif' alt='Đang kiểm tra ...' />");
		 });
		if(jQuery("#" + val).val() == "")
		{
			jQuery('#' + err).html("<div class='error-message'>Giá trị không hợp lệ</div>");
			return;
		}
		jQuery.ajax({
		    type: "GET",
		    url: 'checkuser/' + jQuery("#" + val).val(),
			success: function(msg){
		    	jQuery('#' + err).html(msg);
		    }
		});
	});
}
function register()
{
		
		jQuery(document).ready(function(){
										
		jQuery("#go").ajaxStart(function(){
										
		   jQuery('#err' ).html("<img src='public/images/progress.gif' alt='Đang kiểm tra ...' />");
		  
		 });
		
		jQuery.ajax({
		    type: "GET",
		    url: '/hangdoc/frontend/contact/register/'+ jQuery("#mail").val(),
			success: function(msg){
				 
		    	jQuery('#err').html(msg);
		    }
		});
	});
		 alert('asd');
}
function isEmail(text, val, error, err)
{
	document.getElementById(error).style.display = "none";
	regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if(!regex.test(document.getElementById(val).value))
	{
		document.getElementById(error).style.display = "";
		document.getElementById(err).innerHTML = "";
		return false;
	}
	else
	{
		jQuery(document).ready(function(){
			jQuery("#" + text).ajaxStart(function(){
			   jQuery('#' + err ).html("<img src='../images/progress.gif' alt='Đang kiểm tra ...' />");
			 });
			jQuery.ajax({
			    type: "GET",
			    url: 'checkemail/' + jQuery("#" + val).val(),
				success: function(msg){
			    	jQuery('#' + err).html(msg);
			    }
			});
		});
	}

	return true;
}

function validateContact()
{
	
	document.getElementById("error1").innerHTML = "";
	document.getElementById("error2").innerHTML = "";
	document.getElementById("error3").innerHTML = "";
	document.getElementById("error4").innerHTML = "";
	var flag = 0;
	if(document.getElementById('fullName').value == "")
	{	document.getElementById("error1").innerHTML = "Họ tên không được trống";
		flag = 1;
	}

	regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if(!regex.test(document.getElementById('from_add').value)) {
		document.getElementById("error2").innerHTML = "Email không hợp lệ";
		flag = 2;
	}
	
	if(document.getElementById('content').value == "")
	{
		document.getElementById("error3").innerHTML = "Nội dung không được phép trống";
		flag = 3;
	}
	if(document.getElementById('subject').value == "")
	{
		document.getElementById("error4").innerHTML = "Nội dung không được phép trống";
		flag = 4;
	}
	if(flag != 0)
		return false;
	document.adminForm.submit();
	return true;
}

function validateForm()
{
	var flag = 0;
	regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	if(!regex.test(document.getElementById('for_email').value)) {

		flag = 1;
	}
	var pass1 = document.getElementById("for_password").value;
	if(pass1 == "")
	{
		flag = 2;
	}
	var pass2 = document.getElementById("re-password").value;
	if(pass2 == "")
	{
		flag = 3;
	}

	if(pass1 != pass2)
	{

		flag = 4;
	}
	switch(flag)
	{
		case 1:
			document.getElementById("error_email").innerHTML = "(*)";
			document.myform.email.focus();
			break;
		case 2:
			document.getElementById("error_pass1").innerHTML = "(*)";
			break;
		case 3:
			document.getElementById("error_pass2").innerHTML = "(*)";
			break;
		case 4:
			document.getElementById("error_pass1").innerHTML = "(*)";
			document.getElementById("error_pass2").innerHTML = "(*)";
			break;
	}
	if(flag != 0)
		return false;
	adminForm.submit();
	return true;
}

function selectAll()
{
	var data = document.getElementById('checkall').checked;
	var field = document.adminform.list;

	if(field.length > 0)
	{
		for (i = 0; i < field.length; i++)
			field[i].checked = data ;
	}
	else
	{
		field.checked = data;
	}
}

function checkBox(list, order)
{
	var field = document.adminform.list;
	var data = document.adminform.order;
	if(data.length > 0)
	{
		for (i = 0; i < data.length; i++)
		{
			field[i].checked = true;
		}
	}
}

function chooseCheckBox()
{
	var field = document.adminform.list;
	var flag = 0;
	if(field.length > 0)
	{
		for(i=0;flag == 0 && i < field.length;i++)
			if(field[i].checked == true)
				flag = 1;
	}
	else
	{
		if(field.checked == true)
			flag = 1;
	}

	if(flag == 0)
	{
		alert("Vui lòng chọn 1 record từ danh sách");
		return false;
	}
	return true;
}

function getweather() {
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
	}
	var url="http://www.cpvdesign.com/thongtinhot/home/weather/"+document.getElementById("cboWeather").value;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange=function()
	{	
		if(xmlHttp.readyState==3)
		{
			document.getElementById("showweather").innerHTML='<img src="http://www.cpvdesign.com/thongtinhot/public/images/loading.gif" alt="Đang tải" />';
			
		}
		if (xmlHttp.readyState==4|| xmlHttp.readyState=="complete")
		{
			document.getElementById("showweather").innerHTML=xmlHttp.responseText;
		}
	}
}
function GetXmlHttpObject(){
    var objXMLHttp = null;
    
    if (window.XMLHttpRequest){
        try{
            objXMLHttp = new XMLHttpRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.createRequest){
        try{
            objXMLHttp = new window.createRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.ActiveXObject){
        try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                objXMLHttp = false;
            }
        }
    }
    
    return objXMLHttp;
}

/*calendar */
function showCalendar(id, format, showsTime, showsOtherMonths) {
	
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}
var oldLink = null;
// code to change the active stylesheet


// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}

