function openWindow(url, winTitle, winParams) {
    winName = window.open(url, winTitle, winParams);
    winName.focus();
}

function showBlock(id) {
  if (document.getElementById) {
    itm = document.getElementById(id);
  } else if (document.all){
    itm = document.all[id];
  } else if (document.layers){
    itm = document.layers[id];
  }

  if (itm) {
    itm.style.display = "block";
  }
}

function hideBlock(id) {
  if (document.getElementById) {
    itm = document.getElementById(id);
  } else if (document.all){
    itm = document.all[id];
  } else if (document.layers){
    itm = document.layers[id];
  }

  if (itm) {
    itm.style.display = "none";
  }
}

function checklength(nextfield, chars, currfield) {
    x = document.phone[currfield.name].value.length
    if (x == chars) {
        eval('document.phone.' + nextfield + '.focus();');
    }
}

function displayOnOff(o) {
    var obj = document.getElementById(o);
    if (obj.style.display == "") {
        obj.style.display = "none";
    } else {
        obj.style.display = "";
    }
}

function acceptTerms(o) {
    var obj = document.getElementById(o);
    if (obj.checked) {
        return true;
    } else {
        alert("The following information is missing or invalid:\n\n- Terms & Conditions Acceptance.\n\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }
}

function acceptTerms1(obj) {
	//alert(obj.elements['msisdn.areacode']);
	//alert(obj.elements['msisdn.prefix'].value);
	//alert(obj.elements['msisdn.suffix'].value);
	//alert(obj.terms.value);
	
	if (obj.elements['msisdn.areacode'] == undefined) {
	    var msg = "Are you sure you want to signup this campaign?";
	    ans = confirm(msg);
    	if (ans) {
	        return true;
    	} else {
	        return false;
    	}
	}
	
	if (obj.elements['msisdn.areacode'].value != "" || obj.elements['msisdn.prefix'].value != "" || obj.elements['msisdn.suffix'].value != "") {
	    if (obj.terms.checked) {
	        return true;
	    } else {
	        alert("The following information is missing or invalid:\n\n- Terms & Conditions Acceptance.\n\nPlease make sure you have provided the above information correctly before submitting the data.");
	        return false;
	    }
    }
}

function acceptTerms2(obj) {
    var error = false;
    var msg = '';
    if (!obj.terms.checked) {
        msg = msg + "- Terms & Conditions Acceptance.\n"
        error = true;
    }
    if (document.getElementById("horo12") != null) {
            if (obj.horo12.value == -1) {
                msg = msg + "- " + "Horoscope" + "\n"
                error = true;
            }
    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }
    else {
    	document.getElementById('submitbtn').disabled=true;
	return true;
    }
}

function confirmSignUp() {
    var msg = "Are you sure you want to signup this campaign?";
    ans = confirm(msg);
    if (ans) {
        return true;
    } else {
        return false;
    }
}

function checkPhone(obj) {
    var a = obj.elements['msisdn.areacode'].value.length;
    var p = obj.elements['msisdn.prefix'].value.length;
    var s = obj.elements['msisdn.suffix'].value.length;

    if(a == 3 && p == 3 && (s == 4 || s== 5)){
        return true;
    }
    return false;
}

function checkPhone2(obj) {
    if(obj.elements['msisdn.areacode'].value.charAt(0) != 0) {
        return true;
    }
    return false;
}

function disableClickButton(obj){
    var error = false;
    if ( ((obj.elements['msisdn.areacode'] !=undefined && obj.elements['msisdn.prefix'] !=undefined && obj.elements['msisdn.suffix'] !=undefined) && (!checkPhone(obj) || !checkPhone2(obj))) ||!obj.terms.checked) {
        if (obj.elements['msisdn.areacode'] !=undefined && obj.elements['msisdn.prefix'] !=undefined && obj.elements['msisdn.suffix'] !=undefined && !checkPhone(obj)) {
            error = true;
        }
        if(obj.elements['msisdn.areacode'] !=undefined && obj.elements['msisdn.prefix'] !=undefined && obj.elements['msisdn.suffix'] !=undefined && !checkPhone2(obj)) {
            error = true;
        }
        if (!obj.terms.checked) {           
            error = true;
        }
    }
    if (!error) {
    	var inputs = obj.getElementsByTagName('INPUT');
    	for(var i =0 ; i<inputs.length; i++) {
    		if (inputs[i].name == "submitbtn" ) {
    			inputs[i].disabled = true;
//    			alert(inputs[i].name);
    		}
    	}
 
    }
}

function stepKey(nextfield, chars, currfield){
	x = currfield.value.length;
	if (x == chars) {
		o = document.getElementById(currfield.form.id + '_' + nextfield);
		if ( o != null)	o.focus();
	}
}

function confirmOff(){
    var msg = "This confirms that you will be unsubscribed from this one?"
    var ans = confirm(msg);
    return ans?true:false;
}

function confirmLapse(){
    var msg = "This confirms that you will be lapse this campaign?"
    var ans = confirm(msg);
    return ans?true:false;

}

function confirmRevive(){
    var msg = "This confirms that you will be revive this campaign?"
    var ans = confirm(msg);
    return ans?true:false;

}

function checkRegister(obj) {
    var error = false;
    var msg = '';
    if (!checkPhone(obj) || !checkPhone2(obj) ||!obj.terms.checked || ((document.getElementById("horo12") != null) && (obj.horo12.value == -1))) {
        if (!checkPhone(obj)) {
            msg = msg + "- Mobile Number.\n";
            error = true;
        }
        if(!checkPhone2(obj)) {
            msg = msg + "- Do not include 0 as the first digit of cell phone number.\n"
            error = true;
        }
        if (!obj.terms.checked) {
            msg = msg + "- Terms & Conditions Acceptance.\n"
            error = true;
        }
        if (document.getElementById("horo12") != null) {
            if (obj.horo12.value == -1) {
                msg = msg + "- " + "Horoscope" + "\n"
                error = true;
            }
        }
    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }else{
    	document.getElementById('submitbtn').disabled=true;
    	return true;
    }
}

function checkRegisterWithHandSet(obj) {
    var error = false;
    var msg = '';
    if (!checkPhone(obj) || !checkPhone2(obj) ||!obj.terms.checked || ( !(obj.useragentId.value > 0) ) ) {
        if (!checkPhone(obj)) {
            msg = msg + "- Mobile Number.\n";
            error = true;
        }
        if(!checkPhone2(obj)) {
            msg = msg + "- Do not include 0 as the first digit of cell phone number.\n"
            error = true;
        }
        if (!obj.terms.checked) {
            msg = msg + "- Terms & Conditions Acceptance.\n"
            error = true;
        }
        if (!(obj.useragentId.value > 0)) {
        	msg = msg + "- HandSet.\n"
            error = true;
        }
    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }else{    	
    	document.getElementById('btn_register').disabled=true;
    	return true;
    }
}

function checkPassword2(pwd, passwordMsg, header, footer) {
    var pwd_pattern = /\d{4}/;
    if (!pwd_pattern.test(pwd)) {
        alert(header + "\n\n-" + passwordMsg + "\n\n" + footer);
        return false;
    }
    return true;
}

function checkConfirmPassword(pwd1, pwd2, passwordMsg, header, footer) {
    if (pwd1 != pwd2) {
        alert(header + "\n\n-" + passwordMsg + "\n\n" + footer);
        return false;
    }
    return true;
}

function checkPassword(obj) {
    var error = false;
    var msg = '';
	var pwField = document.getElementById( obj.id + '_password');
    if ( pwField.value == '') {
        msg = msg + "- Password.\n";
        error = true;

    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }else{
    	document.getElementById('submitButton').disabled=true;
    	return true;
    }
}

function checkLogin(obj) {
    var error = false;
    var msg = '';
    var pwField = document.getElementById( obj.id + '_password');
    if (!checkPhone(obj) || (pwField.value == '')) {
        if (!checkPhone(obj)) {
            msg = msg + "- Mobile Number.\n";
            error = true;
        }
        if (obj.password.value == '') {
            msg = msg + "- PIN.\n";
            error = true;
        }
    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }
    return true;
}

function checkPhone2(obj) {
    if(document.getElementById( obj.id + '_msisdn_areacode').value.charAt(0) != 0) {
        return true;
    }
    return false;
}

function checkPhone(obj) {
    var a = document.getElementById( obj.id + '_msisdn_areacode').value.length;
    var p = document.getElementById( obj.id + '_msisdn_prefix').value.length;
    var s = document.getElementById( obj.id + '_msisdn_suffix').value.length;
/*    if(obj.prefix.value.charAt(0) == 0) {
        return false;
    }*/
    if(a == 3 && p == 3 && (s == 4 || s== 5)){
        return true;
    }
    return false;
}


function checkAjaxLogin(obj) {
    var error = false;
    var msg = '';
    var pwField = document.getElementById( obj.id + '_password');
    if (!checkPhone3(obj) || (pwField.value == '')) {
        if (!checkPhone3(obj)) {
            msg = msg + "- Mobile Number.\n";
            error = true;
        }
        if (obj.password.value == '') {
            msg = msg + "- Password.\n";
            error = true;
        }
    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }
    return true;
}

function checkPhone3(obj) {
    var a = document.getElementById( obj.id + '_areacode').value.length;
    var p = document.getElementById( obj.id + '_prefix').value.length;
    var s = document.getElementById( obj.id + '_suffix').value.length;
/*    if(obj.prefix.value.charAt(0) == 0) {
        return false;
    }*/
    if(a == 3 && p == 3 && (s == 4 || s== 5)){
        return true;
    }
    return false;
}

function checkMsisdn(obj) {
    var error = false;
    var msg = '';
    if (!checkPhone(obj) || !checkPhone2(obj)) {
        if (!checkPhone(obj)) {
            msg = msg + "- Mobile Number.\n";
            error = true;
        }
        if(!checkPhone2(obj)) {
            msg = msg + "- Do not include 0 as the first digit of cell phone number.\n"
            error = true;
        }  
    }
    if (error) {
        alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }
}

var domTT_styleClass = 'domTTOverlib';
var domTT_maxWidth = false;
try
{
    var winCloseButton = document.createElement('img');
    winCloseButton.src = '/images/pu_closebutton.gif';
	winCloseButton.style.verticalAlign = 'bottom';
}
catch (e)
{
    var winCloseButton = 'X';
}


/* Login Box */
function hideSearch(e) {
	//searchObj.hide();
	document.getElementById('searchC').style.display = 'none';
	dojo.html.removeClass(tabs.search,"tag");
	dojo.html.addClass(tabs.search,"tagoff");
	//loginObj.show();
	document.getElementById('loginC').style.display = '';
	dojo.html.removeClass(tabs.login,"tagoff");
	dojo.html.addClass(tabs.login,"tag");		
}

function hideLogin(e) {
	//loginObj.hide();
	document.getElementById('loginC').style.display = 'none';
	dojo.html.removeClass(tabs.login,"tag");
	dojo.html.addClass(tabs.login,"tagoff");
	//searchObj.show();
	document.getElementById('searchC').style.display = '';
	dojo.html.removeClass(tabs.search,"tagoff");
	dojo.html.addClass(tabs.search,"tag");		
}

function reloadSearch() {
	//alert('reloadSearch()');
	//searchObj = dojo.widget.byId("searchC");
	tabs["welcome"] = dojo.byId("welcome");
	loginObj = dojo.byId("loginC");
	loginObj = dojo.byId("loginT");
	loginObj.hide();
	//alert('loginObj exists');
	dojo.html.removeClass(tabs.login,"tag");
	dojo.html.addClass(tabs.login,"disableLoginDiv");
	//searchObj.show();
	document.getElementById('searchC').style.display = '';
	dojo.html.removeClass(tabs.search,"tagoff");
	dojo.html.addClass(tabs.search,"tag");
	//dojo.html.addClass(tabs.welcome,"showWelcomeDiv");
	document.getElementById('welcome').style.display = '';
}

function ajaxLoginInit(){
	searchObj = dojo.byId("searchC");
	loginObj = dojo.byId("loginC");
	if (loginObj){
		tabs["search"] = dojo.byId("searchT");
		tabs["login"] = dojo.byId("loginT");	
		dojo.event.connect(tabs.login,"onclick", hideSearch);
		dojo.event.connect(tabs.search,"onclick", hideLogin);
	}
}

function searchCheck( frm ){
	
	if ( frm.searchingKeywords.value.length == 0 ){
		alert('Please type a keyword to search.');
		return false;
	} 
	
	return true;
}

function showPreviewBox( obj, evn, pKey, pFile, contentProviderId){
        
	var str = '<div id="prelistenpop" align="center"><div class="content_bgpop">' +
			'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="100" height="80" id="prelisten" align="middle">'+
            '<param name="allowScriptAccess" value="sameDomain" />'+
            '<param name="movie" value="/object/prelisten_popup.swf" />'+
            '<param name="loop" value="false" />'+
            '<param name="menu" value="false" />'+
            '<param name="quality" value="high" />'+
            '<param name="wmode" value="transparent" />'+
            '<param name="flashvars" value="key=' + pKey + '&file=' + pFile + '&contentProviderId=' + contentProviderId + '"/>'+
            '<embed src="/object/prelisten_popup.swf" flashvars="key=' + pKey + '&file=' + pFile + '&contentProviderId=' + contentProviderId + '" loop="false" menu="false" quality="high" wmode="transparent" width="100" height="80" name="prelisten" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
            '</object>'+
            '</div></div>';
	domTT_activate(obj, evn, 'caption', '&nbsp;Prelisten', 'content', str, 'statusText', 'Loading Prelisten...', 'type', 'sticky', 'styleClass', 'domTTWin', 'closeLink', '<img src=/images/prelisten_close.gif>', 'draggable', true, 'closeAction', 'destroy', 'id', 'prelisten')
}


// this function trims the prevailing and trailing spaces
function trim(x) {
	while(''+x.value.charAt(0)==' ') {
		x.value = x.value.substring(1,x.value.length);
	}

	while(''+x.value.charAt(x.value.length-1)==' ') {
		x.value = x.value.substring(0,x.value.length-1);
	}
}

function checkUpdateEmailForm( thisForm ){
	trim( document.getElementById( thisForm.id + "_email") )
	var email = document.getElementById( thisForm.id + "_email").value;
	
	if (email == "" || (email != null && email.length > 0 && email.match(/^\S+@\S+\.(com|net|org|info|edu|mil|gov|biz|ws|us|tv|cc|aero|arpa|coop|int|jobs|museum|name|pro|travel|nato|.{2,2})$/gi) == null)) {		
		return false;
	}	
	return true;
}

function checkUserProfileForm( thisForm ) {
			  		
	var error = false;
	var msg = '';
	var fisrtName = thisForm.firstName.value;
	var lastName = thisForm.lastName.value;
	var address = thisForm.address.value;
	var city = thisForm.city.value;
	var homePhone = thisForm.homePhone.value;
	var zipCode = thisForm.zipCode.value;
	var State = thisForm.state.value;
	var birthDateYear = thisForm.birthDateYear.value;
	var birthDateMonth = thisForm.birthDateMonth.value;
	var birthDateDay = thisForm.birthDateDay.value;	

	var currentDate = new Date();
	var curyear = currentDate.getFullYear();

	if (fisrtName.length < 2 || fisrtName.length > 100) {
         msg = msg + "- First Name.\n";
         error = true;
	}

	if (lastName.length < 2 || lastName.length > 100) {
         msg = msg + "- Last Name.\n";
         error = true;
	}

	if (address.length < 3 || address.length > 255) {
         msg = msg + "- Address.\n";
         error = true;
	}

	if (city.length < 3 || city.length > 100) {
         msg = msg + "- City.\n";
         error = true;
	}

	if (zipCode == null || zipCode.length != 5 || isNaN(zipCode)) {
         msg = msg + "- Zip Code.\n";
         error = true;
	}

	if (State.length==0) {
         msg = msg + "- State.\n";
         error = true;
	}
	
	if (homePhone.length<2) {
        msg = msg + "- Home Phone.\n";
        error = true;
	}
	
	if (isNaN(birthDateYear) || isNaN(birthDateMonth) || isNaN(birthDateDay) ||
		birthDateYear < 1900 || birthDateYear > 1988 ||
		birthDateMonth < 1 || birthDateMonth > 12 ||
		birthDateDay < 1 || birthDateDay > 31 ) {
		msg = msg + "- Birth Date.\n";
        error = true;
	}

	if (error) {
		alert("The following information is missing or invalid:\n\n" + msg + "\nPlease make sure you have provided the above information correctly before submitting the data.");
        return false;
    }

	return true;

}

function unloadPopup(url) {
	//if(document.getElementById('timermsg1') != null || document.getElementById('timermsg2') != null){
	    var Popup = window.open(url,'','height=420,width=760,status=0,toolbar=0,menubar=0,location=0');
	    Popup.focus();
    	//}
}

function disPopup() {
    document.body.onunload = "";
    window.onunload = "";
}