var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;

function pop_err(fieldname,err_msg)
{
	fieldname.focus();
	alert(err_msg);
}

function change_text(div_element, string){
	document.getElementById(div_element).innerHTML = string;
}

function textLimit(field, maxlen) {
	if (field.value.length > maxlen){
		alert('Your input will be truncated because content must be ' + maxlen + ' characters or less!');
		field.value = field.value.substring(0, maxlen);
	}
}

function doSubmit(event){
	if( window.event ) { // IE
		if(window.event.keyCode == 13) 
			doLogin();
	} else { //Netscape/Firefox/Opera
		if( typeof(event.which ) == 'number' ) { 
			if(event.which == 13) 
				doLogin();
		}
	} 
}

function doSearch(type, event){
	var process_search = 0;
	if(type == 1){
		if( window.event ) { // IE
			if(window.event.keyCode == 13) 
				process_search = 1;
		} else { //Netscape/Firefox/Opera
			if( typeof(event.which ) == 'number' ) { 
				if(event.which == 13) 
					process_search = 1;
			}
		}
	}else if(type == 2){
		process_search = 1;
	}
	if(process_search == 1){
		var doc = document.frm;
		if(doc.ven.value == "" && doc.keyword.value == "" && doc.zipcode_search.value == ""){
			alert("Please select at least one condition to search!");
			return false;
		}
		doc.act.value = "Search";
		doc.action='default.asp';
		doc.submit();
	}
}

function doForgotPw(){
	var doc = document.frm;
	if (doc.email.value == "") {
		pop_err(doc.email, "Please enter your email address!");
		return false;
	}
	doc.act.value = "send_pw";
	doc.submit();
}

function doRegistration(type){
	var doc = document.frm;
	var err_str = "";
	
	if (doc.email.value == "") {
		err_str += "- E-mail must not be blank!\n";
	}else if(!isemailaddress(doc.email.value)){
		err_str += "- E-mail is not valid. Please try again!\n";
	}
	if(type == 1){
		if(doc.password.value == ""){
			err_str += "- Password must not be blank!\n";
		}else{
			if(doc.password.value.length < 5){
				err_str += "- Password must have at least 5 characters!\n";
			}else	if(doc.password.value != doc.confirm_password.value){
				err_str += "- Password and Confirm Password do not match. Please try again!\n";
			}
		}
	}
	if (doc.vendor.value == "") {
		err_str += "- Vendor must not be blank!\n";
	}else if(!checkKeyword(doc.vendor.value)){
		err_str += "- Vendor must not contain keyword '<script>' or '</script>!\n";
	}
	if (doc.contactname.value == "") {
		err_str += "- Contact name must not be blank!\n";
	}else if(!checkKeyword(doc.contactname.value)){
		err_str += "- Contact name must not contain keyword '<script>' or '</script>!\n";
	}
	if (doc.website.value == "") {
		err_str += "- Website must not be blank!\n";
	}else if(!checkKeyword(doc.website.value)){
		err_str += "- Website must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.address.value != "" && !checkKeyword(doc.address.value)){
		err_str += "- Address must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.city.value != "" && !checkKeyword(doc.city.value)){
		err_str += "- City must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.zipcode.value != "" && !checkKeyword(doc.zipcode.value)){
		err_str += "- Zip code must not contain keyword '<script>' or '</script>!\n";
	}
	if(type == 1){
		if (doc.security_code.value == "") {
			err_str += "- Please enter the code in the image.!\n";
		}
	}
	if(err_str != ""){
		alert(err_str);
		return false;
	}
	doc.act.value = "save";
	doc.submit();
}

// template_type = 0: select template from popup window when edit coupon
// template_type > 0: select template when create coupon
function selectTemplate(type, template_type){
	if(type == 1){
		document.frm.template_type.value = template_type;
		document.frm.submit();
	}else{
		window.close();
		window.opener.document.frm.template_type.value = template_type;
		window.opener.document.frm.submit();
	}
}

function changeTemplate(template_type){
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
	winH = (ns4)? window.innerHeight : document.body.offsetHeight

	winTop = 95;
	winLeft = 115;
	szpage = "coupon_design_change_template.asp";
	sPro = "WIDTH=600,HEIGHT=600,scrollbars=no,resize=no,menubar=no,resizable=no,left=" + winLeft + ",top=" + winTop
	window.open(szpage,"print",sPro,true)
}

// show image coupon at .Net site if template_type <> 6
function showCoupon(coupon_id, image_name_print, template_type){
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
	winH = (ns4)? window.innerHeight : document.body.offsetHeight

	winTop = 95;
	winLeft = 115;
	if(template_type == 4){
		var width = 285;
		var height = 460; // 420
	}else{
		var width = 500;
		var height = 340; // 305
	}
	if(template_type == 6){
		var scroll_bar_allow = "yes";
		szpage = "coupon_view.asp?cid=" + coupon_id;
	}else{
		var scroll_bar_allow = "no";
		if(template_type == 4){
			szpage = "http://image.freecoupontools.com/view.aspx?strname=" + image_name_print;
		}else{
			szpage = "http://image500320.freecoupontools.com/view.aspx?strname=" + image_name_print;
		}
	}
	sPro = "WIDTH=" + width + ",HEIGHT=" + height + ",scrollbars=" + scroll_bar_allow + ",resize=no,menubar=no,resizable=no,left=" + winLeft + ",top=" + winTop
	window.open(szpage,"print",sPro,true)
}

function tellFriend(coupon_id){
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
	winH = (ns4)? window.innerHeight : document.body.offsetHeight

	winTop = 95;
	winLeft = 115;
	szpage = "coupon_tellfriend.asp?cid=" + coupon_id;
	sPro = "WIDTH=400,HEIGHT=300,scrollbars=no,resize=no,menubar=no,resizable=no,left=" + winLeft + ",top=" + winTop
	window.open(szpage,"print",sPro,true)
}

function send_email(){
	var doc = document.frm;
	var m = "";
	var f_email = 0;
	if(doc.sender_name.value == ""){
		m += "- Please enter your name.\n";
	}else if(!checkKeyword(doc.sender_name.value)){
		m += "- Your name must not contain keyword '<script>' or '</script>!\n";
	}
	if(!isemailaddress(doc.sender_email.value)){
		m += "- Please enter your valid email address!\n";
	}
	for(var i=0; i<doc.length; i++){
		if(doc.elements[i].name == "friend_email"){
			if(doc.elements[i].value != ""){
				f_email = f_email + 1;
				if(!isemailaddress(doc.elements[i].value)){
					m += "- Please enter your friend's valid email address!\n";
					i = doc.length;
				}
			}
		}
	}
	if(f_email == 0){
		m += "- Please enter at least 1 your friend's email address!\n";	
	}
	if(m) {
		alert(m);
		return false;
	}else{
		doc.act.value = "tell_friend";
		doc.submit();
	}
}

function request_category(){
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
	winH = (ns4)? window.innerHeight : document.body.offsetHeight

	winTop = 95;
	winLeft = 115;
	szpage = "request_new_category.asp";
	sPro = "WIDTH=400,HEIGHT=300,scrollbars=no,resize=no,menubar=no,resizable=no,left=" + winLeft + ",top=" + winTop
	window.open(szpage,"print",sPro,true)
}

function validate_request_category(){
	var doc = document.frm;
	var m = "";
	var f_email = 0;
	if(doc.sender_name.value == ""){
		m += "- Please enter your name.\n";
	}else if(!checkKeyword(doc.sender_name.value)){
		m += "- Your name must not contain keyword '<script>' or '</script>!\n";
	}
	if(!isemailaddress(doc.sender_email.value)){
		m += "- Please enter your valid email address!\n";
	}
	var cate_input = 0;
	for(var i=1; i<=5; i++){
		var cate_element = "category" + i;
		if(document.getElementById(cate_element).value != ""){
			cate_input = 1;
			i = 5;
		}
	}
	if(cate_input == 0){
		m += "- Please enter at least 1 category!\n";	
	}
	if(m) {
		alert(m);
		return false;
	}else{
		doc.act.value = "request_category";
		doc.submit();
	}
}

function report_abuse(coupon_code){
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
	winH = (ns4)? window.innerHeight : document.body.offsetHeight

	winTop = 95;
	winLeft = 115;
	szpage = "coupon_report_abuse.asp?coupon_code=" + coupon_code;
	sPro = "WIDTH=400,HEIGHT=340,scrollbars=no,resize=no,menubar=no,resizable=no,left=" + winLeft + ",top=" + winTop
	window.open(szpage,"print",sPro,true)
}

function validate_report_abuse(){
	var doc = document.frm;
	var m = "";
	var f_email = 0;
	if(doc.sender_name.value == ""){
		m += "- Please enter your name.\n";
	}else if(!checkKeyword(doc.sender_name.value)){
		m += "- Your name must not contain keyword '<script>' or '</script>!\n";
	}
	if(!isemailaddress(doc.sender_email.value)){
		m += "- Please enter your valid email address!\n";
	}
	var coupon_input = 0;
	for(var i=1; i<=5; i++){
		var coupon_element = "coupon" + i;
		if(document.getElementById(coupon_element).value != ""){
			coupon_input = 1;
			i = 5;
		}
	}
	if(coupon_input == 0){
		m += "- Please enter at least 1 coupon code or URL!\n";	
	}
	if(m) {
		alert(m);
		return false;
	}else{
		doc.act.value = "report_abuse";
		doc.submit();
	}
}

function updateCoupon(){
	var doc = document.frm;
	var is_check = 0;
	for(var i=0; i<doc.length; i++){
		if(doc.elements[i].type == "checkbox" && doc.elements[i].checked == true && doc.elements[i].name == "chkitem"){
			page_direct = "coupon_design.asp?cid=" + doc.elements[i].value;
			window.location.replace(page_direct);
		}
	}
}

function deleteCoupon(type){
	var doc = document.frm;
	if(!confirm("Are you sure you want to delete selected coupon(s)?")){
		return false;
	}else{
		doc.act.value = "del_coupon";
		doc.submit();
	}
}

function genCouponCode(){
	var doc = document.frm;
	var auto_couponcode = doc.auto_couponcode.value;	
	doc.couponcode.value = auto_couponcode;
}

function zoomin(object){
	object.height *= 2;
}
	
function validateCoupon(){
	var doc = document.frm;
	var template_type = doc.template_type.value;
	var err_str = "";
	
	if(template_type == 4){
		if(doc.subtitle.value != "" && !checkKeyword(doc.subtitle.value)){
			err_str += "- This info must not contain keyword '<script>' or '</script>!\n";
		}
	}
	if(doc.couponcode.value == ""){
		err_str += "- Please enter Coupon Code!\n";
	}else if(!checkKeyword(doc.couponcode.value)){
		err_str += "- Coupon Code must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.title.value == ""){
		err_str += "- Please enter Title!\n";
	}else if(!checkKeyword(doc.title.value)){
		err_str += "- Title must not contain keyword '<script>' or '</script>!\n";
	}
	if(template_type == 1 || template_type == 3){
		if(doc.subtitle.value != "" && !checkKeyword(doc.subtitle.value)){
			err_str += "- Sub Title must not contain keyword '<script>' or '</script>!\n";
		}
	}
	// assign value for discount amount from selected image
	if(template_type == 2){
		var value_image_discount1 = doc.image_discount1.value.substr(0,1);
		var value_image_discount2 = doc.image_discount2.value.substr(0,1);
		var discount_amount = value_image_discount1 + "" + value_image_discount2;
		doc.discountamount.value = discount_amount;
	}
	if(typeof(doc.discountamount) == "object"){
		if(doc.discountamount.value == ""){
			err_str += "- Please enter Discount Amount!\n";
		}else if (!isNumeric(doc.discountamount.value)){
			err_str += "- Discount Amount must be numerical!\n";
		}
	}
	if(doc.coupondesc.value != "" && !checkKeyword(doc.coupondesc.value)){
		err_str += "- Description must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.catid.value == ""){
		err_str += "- Please select Category for Coupon!\n";
	}
	if(doc.expiredate.value == ""){
		err_str += "- Please enter Expire Date!";
	}else if(!checkKeyword(doc.expiredate.value)){
		err_str += "- Expire Date must not contain keyword '<script>' or '</script>!\n";
	}

	if(err_str != ""){
		alert(err_str);
	}else{
		doc.act.value = "save_coupon";
		doc.submit();
	}
}

function setBackgroundCopon(){
	document.getElementById("t1_bg_body").style.backgroundColor = document.getElementById("bg_coupon").value;
}

function gotoPage(number){
	var doc = document.frm;
	doc.page.value = number;
	doc.submit();
}

function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function upload_adimage(template_type){
	ns4 = (document.layers)? true:false
	ie4 = (document.all)? true:false

	winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
	winH = (ns4)? window.innerHeight : document.body.offsetHeight

	winTop = 95;
	winLeft = 115;
	szpage = "upload_adimage.asp?template_type=" + template_type;
	sPro = "WIDTH=440,HEIGHT=115,scrollbars=no,resize=no,menubar=no,resizable=no,left=" + winLeft + ",top=" + winTop
	window.open(szpage,"uploadfile",sPro,true)
}

function chkfile_upload(){
	var doc = document.frm;
	var input_file = 0;
	for(var i=0; i<doc.length;i++){
		if(doc.elements[i].type=="file"){
			if(doc.elements[i].value != ""){
				input_file = 1;
				szfile = doc.elements[i].value;	
				var szlen = szfile.length;
				var szfirstlen = szlen - 3;
				var strend = trim(szfile.substr(szfirstlen,szlen));
				if(strend.toLowerCase() != 'gif' && strend.toLowerCase() != 'jpg' && strend.toLowerCase() != 'jpeg' && strend.toLowerCase() != 'png'){
					pop_err(doc.elements[i],"File upload is invalid. Please upload gif, jpg or png!");
					return false;
				}
			}
		}
	}
	if(input_file == 0){
		alert("Please Browse to select file for upload!");
		return false;
	}
	return true;
}

function docheckall()
{
	var doc = document.frm;
	var is_check = document.frm.chkall.checked;
	for(var i=0; i<doc.length; i++){
		if(doc.elements[i].type == "checkbox")
			doc.elements[i].checked = is_check;
	}	
	if(is_check == true){
		doc.cmdDelete.disabled = false;
		if(doc.total_item.value == 1){
			doc.cmdUpdate.disabled = false;
		}else{
			doc.cmdUpdate.disabled = true;
		}
	}else{
		doc.cmdDelete.disabled = true;
		doc.cmdUpdate.disabled = true;
	}
}

function selectAll(is_check){
	var doc = document.frm;
	for(var i=0; i<doc.length; i++){
		if(doc.elements[i].type == "checkbox")
			doc.elements[i].checked = is_check;
	}
	if(is_check == true){
		doc.cmdDelete.disabled = false;
	}else{
		doc.cmdDelete.disabled = true;
	}
}

function docheckitem()
{
	var doc = document.frm;
	var is_check = 0;
	for(var i=0; i<doc.length; i++){
		if(doc.elements[i].type == "checkbox" && doc.elements[i].checked == true && doc.elements[i].name == "chkitem")
			is_check = is_check + 1;
	}
	if(is_check > 0){
		doc.cmdDelete.disabled = false;
	}else{
		doc.cmdDelete.disabled = true;
	}
	if(typeof(doc.cmdUpdate) == "object"){
		if(is_check == 1)
			doc.cmdUpdate.disabled = false;
		else
			doc.cmdUpdate.disabled = true;
	}
	if(typeof(doc.chkall) == "object"){
		if(is_check == doc.total_item.value)
			doc.chkall.checked = true;
		else
			doc.chkall.checked = false;
	}
}

function deleteImage(){
	var doc = document.frm;
	if(!confirm("Are you sure you want to delete selected image(s)?")){
		return false;
	}else{
		doc.act.value = "del_image";
		doc.submit();
	}
}

function getRequestBody(oForm)
{
	 var aParams = new Array();            
     for (var i=0 ; i < oForm.elements.length; i++) {
         var sParam = encodeURIComponent(oForm.elements[i].name);
         sParam += "=";
         sParam += encodeURIComponent(oForm.elements[i].value);
         aParams.push(sParam);
     }      
     return aParams.join("&"); 
}

function doLogin()
{
	var doc = document.frm;
	if(doc.username.value == ""){
		pop_err(doc.username, "Please enter Email!");
		return false;
	}
	if(doc.password.value == ""){
		pop_err(doc.password, "Please enter Password!");
		return false;
	}
	doc.act.value = "login";
	doc.submit();
}

function changePassword(){
	var doc = document.frm;
	if(doc.password.value == ""){
		pop_err(doc.password, "Please enter password!");
		return false;
	}
	if(doc.new_password.value == ""){
		pop_err(doc.new_password, "Please enter new password!");
		return false;
	}else{
		if(doc.new_password.value != doc.confirm_password.value){
			pop_err(doc.new_password, "New password and Confirm password do not match. Please try again!");
			return false;
		}
	}
	doc.act.value = "change_password";
	doc.submit();
}

function validateContact(){
	var doc = document.frm;
	var err_str = ""
	if(doc.first_name.value == ""){
		err_str += "- First Name must not be blank!\n";
	}else if(!checkKeyword(doc.first_name.value)){
		err_str += "- First Name must not contain keyword '<script>' or '</script>!\n";
	}
	if (doc.last_name.value == ""){
		err_str += "- Last Name must not be blank!\n";
	}else if(!checkKeyword(doc.last_name.value)){
		err_str += "- Last Name must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.company.value!= "" && !checkKeyword(doc.company.value)){
		err_str += "- Company must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.address.value!= "" && !checkKeyword(doc.address.value)){
		err_str += "- Address must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.city.value != "" && !checkKeyword(doc.city.value)){
		err_str += "- City must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.zipcode.value != "" && !checkKeyword(doc.zipcode.value)){
		err_str += "- Zip code must not contain keyword '<script>' or '</script>!\n";
	}
	if(doc.phone.value == ""){
		err_str += "- Phone must not be blank!\n";
	}else if(!checkKeyword(doc.phone.value)){
		err_str += "- Phone must not contain keyword '<script>' or '</script>!\n";
	}
	if (doc.email.value == ""){
		err_str += "- E-mail must not be blank!\n";
	}else if(!isemailaddress(doc.email.value)){
		err_str += "- E-mail is not valid. Please try again!\n";
	}
	if (doc.content.value == ""){
		err_str += "- Content must not be blank!\n";
	}else if(!checkKeyword(doc.content.value)){
		err_str += "- Content must not contain keyword '<script>' or '</script>!\n";
	}
	if (doc.security_code.value == "") {
		err_str += "- Please enter the code in the image.!\n";
	}
	
	if(err_str != ""){
		alert(err_str);
		return false;
	}

	doc.act.value = "contact";	
	doc.submit();
	return true;
}

function loadPublicImage(cat_image){
	var doc = document.frm;
	doc.act.value = "load_public_image";
	var page_action = "ajax_actions.asp?cat_image=" + cat_image;
	//doc.action = page_action;
	//doc.submit();
	document.getElementById("div_public_image").innerHTML = "<img src='../../images/spinner.gif'> Please wait...";
	var sBody = getRequestBody(doc);
	var oXmlHttp = zXmlHttp.createRequest();
	oXmlHttp.open("post", page_action, true);
	oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oXmlHttp.onreadystatechange = function handleHttpResponse(){
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var result_str = oXmlHttp.responseText;
				document.getElementById("div_public_image").innerHTML = result_str;
			} else {
				alert("An error occurred: " + oXmlHttp.statusText);
			}
		}
	}
	oXmlHttp.send(sBody);
}

// Count the appearance of substring in string
function countString(pstring, sub_string) {
	var temp = pstring; // temporary holder
	var result = 0;
	while (temp.indexOf(sub_string) != -1) {
		temp = temp.replace(sub_string, 'AAAAAAAAAA');
		result = result + 1;
	}
	return result;
}

function checkKeyword(string){
	if(string.toLowerCase().indexOf("<script") != -1 || string.toLowerCase().indexOf("</script") != -1
			|| string.toLowerCase().indexOf("&lt;script") != -1 || string.toLowerCase().indexOf("&lt;/script") != -1){
		return false;
	}
	return true;
}

//check numeric
function isInteger(num){
	var re = /^[0-9]*$/;
	num = trim(num);
	var pos = num.search(re);
	if(pos == -1){
		return false;
	}else{
		return true;
	}
}

function trim(st)
{
	index = 0;
	for (var i = 0; i < st.length; i++)
	{
		ch = st.charAt(i);
		if (ch == ' ')
			index++;
		else 
			break;
	}
	st = st.substring(index, st.length);
	index = st.length;
	for (i = index-1; i > 0; i--)
	{
		ch = st.charAt(i);
		if (ch==' ')
			index--;
		else
			break;
	}
	st = st.substring(0, index);
	return st;
}

function check_url(str_url)
{
	var arr = new Array();
	var re = /[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*:\/\/([0-9a-zA-Z-_`!@$&*+|\]\[:;?,~=]+)+(\.[0-9a-zA-Z-_`!@$&*+|\]\[:;?,~=]+)*(\/[0-9a-z A-Z-_`!@$&*+|\]\[:;?,~.=]*)*$/;
	str_url = trim(str_url);
	if(str_url.search(re)){
		return false;
	} else{
		pos_pointslash = str_url.indexOf("./");
		pos_slashpoint = str_url.indexOf("/.");
		if(pos_pointslash != -1 || pos_slashpoint != -1)
			return false;
		arr = str_url.split("//");
		if(arr.length > 2)
			return false;
		return true;
	}
}

function isNumeric(num){
	return !isNaN(num);
}

function isemailaddress(str_email){
	var re = /^[0-9a-z-A-Z-_~=:,|\/\\\'\"]+(\.[0-9a-z-A-Z-_~=:,|\/\\\'\"]+)*@[0-9a-z-A-Z-_~=:,|\/\\\'\"]+(\.[0-9a-z-A-Z-_~=:,|\/\\\'\"]+)+(\.[0-9a-z-A-Z-_~=:,|\/\\\'\"]+)*$/;
	var pos = str_email.search(re);
	if(pos == -1){
		return false;
	}else{
		return true;
	}
}
